#181732 - 2007-10-17 10:53 PM
Reading a ini file
|
noobness
Fresh Scripter
Registered: 2006-01-05
Posts: 32
|
First, thanks for any help or guidance you can provide.
Background - reworking an old script to wake up all the computers on the network using WOL.
Wrote a php script to pull all IP's and MAC's from Open Audit.
Ini file looks like this (small portion, IP's/MAC's changed to protect innocent)
192.168.159.002,00:AB:AB:A4:C3:C3
192.168.161.146,00:AB:AB:F8:A4:C2
192.168.001.139,00:AB:C3:BB:AB:A4
192.168.155.135,00:AB:C3:EA:C3:4A
Have removed alot of the code that I use for logging, but in short my code is not assigning my variable correctly. Pipe it out to a text file and they are blank. Im sure it something simple for you guys.
Copy "wol.ini" "woltemp.ini" ;temp file
For Each $PC in Split(ReadProfileString('woltemp.ini','',''),Chr(10))
$IP=SubStr($PC,1,12)
$IPNET = '$IP' + '255'
$MAC = Right($PC,17)
If $IP
Run 'D:\wol\wol.exe -b=$IPNET -p=9 $MAC' ;sends out directed broadcast
Sleep 0.25
EndIf
Next
Thanks!
|
Top
|
|
|
|
#181734 - 2007-10-17 11:00 PM
Re: Reading a ini file
[Re: noobness]
|
Witto
MM club member
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
INI Files ALWAYS look like this:
[section1]
key1=string1
key2=string2
key3=string3
[section2]
key4=string4
key5=string5
key6=string6
[section3]
key7=string7
key8=string8
key9=string9
I think you have to find a FreeFileHandle(), Open() the file at least for read access and ReadLine() the lines one by one, maybe Split() the lines with a comma as delimiter.
|
Top
|
|
|
|
#181758 - 2007-10-18 05:59 PM
Re: Reading a ini file
[Re: noobness]
|
noobness
Fresh Scripter
Registered: 2006-01-05
Posts: 32
|
Looks good so far, have it writing to a text file in below code so I can see what I am getting for output
$file = Open(1, "wol.txt", 2)
$line = ReadLine(1)
$log = Open(2, "wol.log",5)
While @ERROR = 0
$line = Split ($line, ",")
$ip = $line[0]
$ipnet = Left($ip,Len($ip)-3)
$ipbroadcast = $ipnet + '255'
$mac = $line[1]
$line = ReadLine(1)
WriteLine(2,"D:\wol\wol.exe -b=" + $ipbroadcast + " -p=9 " + $mac + @CRLF)
Loop
$file = Close(1)
$log = Close(2)
I checked my text file after and i have almost 1000 lines of correctly formatted WOL commands. I do get an error when I run the script for testing though.
Error: array reference out of bounds!
I assume this has something to do with the error checkig so that it knows that the end of file has been reached and it can quit looping? Am I safe to say this can be ignored?
Edited by noobness (2007-10-18 07:22 PM)
|
Top
|
|
|
|
#182010 - 2007-10-26 10:12 PM
Re: Reading a ini file
[Re: Mart]
|
noobness
Fresh Scripter
Registered: 2006-01-05
Posts: 32
|
Sorry about the delay and thanks again for your help.
For testing - all files are in same folder and just writing to a text file to verify that commands are properly formatted then I can pipe it over to run command
Source File (WOL.txt)
192.095.159.002,00:06:5B:A4:98:C2
192.095.161.146,00:14:22:A4:98:C2
192.095.155.135,00:08:74:A4:98:C2
192.095.153.153,00:08:74:A4:98:C2
192.095.159.127,00:08:74:A4:98:C2
192.095.156.003,00:14:22:A4:98:C2
192.095.156.064,00:13:72:A4:98:C2
Current Script
;Open(1,'WOLAll.log',5) ;Log which PC where sent magic packet
$log = Open(1, "wol.log",5)
Copy 'wol.txt' 'woltemp.txt'
$file = Open(2, "woltemp.txt", 2)
$line = ReadLine(1)
While @ERROR = 0
$ip = Left($line,Len($line)-15)
$mac = Right($line,Len($line)-15)
$ipnet = Left($ip,Len($ip)-3)
$ipbroadcast = $ipnet + '255'
WriteLine(5,"D:\wol\wol.exe -b=" + $ipbroadcast + " -p=9 " + $mac + @CRLF)
$line = ReadLine(1)
Loop
$file = Close(1)
$log = Close(2)
|
Top
|
|
|
|
#182016 - 2007-10-27 10:59 PM
Re: Reading a ini file
[Re: Mart]
|
Mart
KiX Supporter
Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
|
A little rewrite of your code. This is untested but should work as far as I can see. The only thing is a file with file handle 5 it is still missing but I guess you open that somewhere else in your code.
;Open(1,'WOLAll.log',5) ;Log which PC where sent magic packet
$log = Open(1, "wol.log",5)
Copy 'wol.txt' 'woltemp.txt'
$file = Open(2, "woltemp.txt", 2)
$line = ReadLine(1)
While @ERROR = 0
If Trim($line) <> ""
$line = Split($line, ",")
$ip = $line[0]
$mac = ($line[1]
$ipnet = Left($ip,Len($ip)-3)
$ipbroadcast = $ipnet + '255'
WriteLine(5,"D:\wol\wol.exe -b=" + $ipbroadcast + " -p=9 " + $mac + @CRLF)
EndIf
$line = ReadLine(1)
Loop
$file = Close(1)
$log = Close(2)
_________________________
Mart
- Chuck Norris once sold ebay to ebay on ebay.
|
Top
|
|
|
|
#182045 - 2007-10-29 03:07 PM
Re: Reading a ini file
[Re: noobness]
|
noobness
Fresh Scripter
Registered: 2006-01-05
Posts: 32
|
Fixed the typo's in file handlers and its working - at least its writing correctly wol.exe formatted commands to a text file. Will modify here in a few and post.
Thanks for your help!!!
$log = Open(1, "C:\wol\wol.log",5)
Copy 'C:\wol\wol.txt' 'C:\wol\woltemp.txt'
$file = Open(2, "C:\wol\woltemp.txt", 2)
$line = ReadLine(2)
While @ERROR = 0
If Trim($line) <> ""
$line = Split($line, ",")
$ip = $line[0]
$mac = $line[1]
$ipnet = Left($ip,Len($ip)-3)
$ipbroadcast = $ipnet + '255'
WriteLine(1,'D:\wol\wol.exe -b=' + $ipbroadcast + ' -p=9 ' + $mac + @CRLF)
EndIf
$line = ReadLine(2)
Loop
$file = Close(2)
$log = Close(1)
|
Top
|
|
|
|
#182052 - 2007-10-29 04:20 PM
Re: Reading a ini file
[Re: Mart]
|
noobness
Fresh Scripter
Registered: 2006-01-05
Posts: 32
|
You're quick - went with a SubStr and it worked out well - Final Code
PHP script that I use to query OpenAudit - this is set up as a chron job on the linux server to run the php script and then copy output to the wol directory on the windows server
PHP Code - wol.php hostname is openaudit server ip / username and pass are a user that has rights to the openaudit database
<?
define ('HOSTNAME', 'xxx.xxx.xxx.xxx');
define ('USERNAME', 'enterusername');
define ('PASSWORD', 'enterpassword');
define ('DATABASE_NAME', 'enterdatabase');
$fp = fopen('/var/wolexport/wol.txt','w');
$db = mysql_connect(HOSTNAME, USERNAME, PASSWORD) or die ('I cannot connect to MySQL.');
mysql_select_db(DATABASE_NAME);
$query = "SELECT net_ip_address,system_uuid FROM system";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$nextline = $row[0] . ',' . $row[1] . "\r\n";
fwrite($fp,$nextline);
}
fclose($fp);
mysql_free_result($result);
mysql_close();
?>
Sample Output Text File
192.094.159.002,00:06:5B:A4:7B:21
192.094.161.146,00:06:5B:F8:7B:21
192.094.155.135,00:06:5B:EA:7B:21
192.094.153.153,00:06:5B:A8:7B:21
192.094.159.127,00:06:5B:A8:7B:21
192.094.156.003,00:06:5B:F7:7B:21
192.094.156.064,00:06:5B:73:7B:21
192.094.156.191,00:06:5B:73:7B:21
192.094.156.076,00:06:5B:73:7B:21
192.094.156.139,00:06:5B:F8:7B:21
Kix Code - Wol Scheduled Task
$log = Open(1, "C:\wol\wol.log",5)
Copy 'C:\wol\wol.txt' 'C:\wol\woltemp.txt'
$file = Open(2, "C:\wol\woltemp.txt", 2)
$line = ReadLine(2)
While @ERROR = 0
If Trim($line) <> ""
$line = Split($line, ",")
$ip = $line[0]
$ipbroadcast=SubStr($ip,1,4)+SubStr($ip,6,7)+'255'
$mac = $line[1]
;$ipnet = Left($ip,Len($ip)-3)
;$ipbroadcast = $ipnet + '255'
Sleep 0.5
Run 'c:\wol\wol.exe -b=$ipbroadcast -p=9 $mac'
WriteLine(1,'C:\wol\wol.exe -b=' + $ipbroadcast + ' -p=9 '+ $mac + @CRLF)
Sleep 0.5
EndIf
$line = ReadLine(2)
Loop
$file = Close(2)
$log = Close(1)
If going over VLAN's directed broadcast has to be enable on your switches. PC's must have WOL enabled on BIOS and in windows NIC configuration
Thanks for your help Mart!
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 382 anonymous users online.
|
|
|