Page 1 of 2 12>
Topic Options
#181732 - 2007-10-17 10:53 PM Reading a ini file
noobness Offline
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)

 Code:
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.

 Code:
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 Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
INI Files ALWAYS look like this:
 Code:
[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
#181735 - 2007-10-17 11:06 PM Re: Reading a ini file [Re: noobness]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4628
Loc: The Netherlands
Just like Witto said you are not using a properly formatted ini file.
In your case readline and splitting the outcome on the comma would do the trick.

Something like this.
 Code:
Break on

$rc = Open(1, "c:\somefile", 2)
$line = ReadLine(1)

While @ERROR = 0
	$line = Split ($line, ",")
	$ip = $line[0]
	$mac = $line[1]
	?$ip
	?$mac
	$line = ReadLine(1)
Loop

$rc = Close(1)

Sleep 5
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#181756 - 2007-10-18 05:24 PM Re: Reading a ini file [Re: Mart]
noobness Offline
Fresh Scripter

Registered: 2006-01-05
Posts: 32
Thanks for the input, will report the outcome as soon as I have a chance to try it.

Will change the ini file to a .txt file.

Thanks!!


Edited by noobness (2007-10-18 05:25 PM)

Top
#181758 - 2007-10-18 05:59 PM Re: Reading a ini file [Re: noobness]
noobness Offline
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

 Code:
 $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
#181766 - 2007-10-18 07:42 PM Re: Reading a ini file [Re: noobness]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4256
Loc: New Jersey
No, that's a fatal error.

After the
$Line = Split(...
you should do something along the lines of:
If UBound($Line)
; ok to process
Else
; bad data - what to do?
Endif

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#181796 - 2007-10-19 10:14 PM Re: Reading a ini file [Re: Glenn Barnas]
noobness Offline
Fresh Scripter

Registered: 2006-01-05
Posts: 32
Hmm something is not right now. Not getting the output to file as I was before with the same script. Not sure what I inadvertantly changed. I am getting the array error still, I did try to add the UBound function as suggested and it gave me errors as well.

Any help would be appreciated.

JD

Top
#181800 - 2007-10-20 02:33 AM Re: Reading a ini file [Re: noobness]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4628
Loc: The Netherlands
Could you post your current script and a sample (just a few lines is enough) of the file containing the source data?

Are there lines in the source file that have missing values? There is an IP but no MAC or something? If this is true then the array could be shorter on some lines so you'll get an array reference out of bounds error because you are referencing to an array element that does not exist.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#182010 - 2007-10-26 10:12 PM Re: Reading a ini file [Re: Mart]
noobness Offline
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

 Code:
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

 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
	$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
#182011 - 2007-10-26 10:15 PM Re: Reading a ini file [Re: noobness]
noobness Offline
Fresh Scripter

Registered: 2006-01-05
Posts: 32
Hopefully when I get this done, other will be able to use it. We use open source OpenAudit for auditing, I have a perl sql query job run every day that pipes the IP and Macs to a text file. Directed broadcast is enable on switches so that wol packets can pass the vlans. Its nice to be able to wake up all computers in the middle of the night to do virus scans, wsus updates and what ever else.

We had a solution before, but the mac plus IP would always get out of date. Using open audit database query should provide current info.

Top
#182015 - 2007-10-27 03:49 PM Re: Reading a ini file [Re: noobness]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4628
Loc: The Netherlands
First off all where is file 5? You are writing to a file with file handle 5 but you never open a file with file handle 5.
Instead of substringing you could split the line on the , using the Split() function.

I would also add a check to see if the line you just read is not empty. Like this.

 Code:
....
While @ERROR = 0 and Trim($line) <> ""
....
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#182016 - 2007-10-27 10:59 PM Re: Reading a ini file [Re: Mart]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4628
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.

 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
#182040 - 2007-10-29 01:59 PM Re: Reading a ini file [Re: Mart]
noobness Offline
Fresh Scripter

Registered: 2006-01-05
Posts: 32
Correct - had 5 commented out in the posted code.
Top
#182043 - 2007-10-29 02:38 PM Re: Reading a ini file [Re: noobness]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4628
Loc: The Netherlands
I donít' see that. Only the first line is commented out but that is also file handle 1.
_________________________
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 Offline
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!!!

 Code:
$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
#182048 - 2007-10-29 03:45 PM Re: Reading a ini file [Re: noobness]
noobness Offline
Fresh Scripter

Registered: 2006-01-05
Posts: 32
Have it semi working now. One more issue I have to fix first. Found that a properly formatted wol command cant accept zero's in the broadcast address. for example

C:\wol.exe -b=141.082.135.255 -p=9 00:00:BA:CA:CA:CA

The 082 causes the command to break

Needs to be for example

C:\wol.exe -b=141.82.135.255 -p=9 00:00:BA:CA:CA:CA

Most of the IP's here have a 2 digit 2nd octets so I will have to code removing the leading zero. Should not be to bad.

JD

Top
#182050 - 2007-10-29 04:09 PM Re: Reading a ini file [Re: noobness]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4628
Loc: The Netherlands
You could split the IP on the dot and do a Val() on the parts (remove the zero's) and then join the octets together again with a dot.

 Code:
Break on

$ip = Split($ip, ".")
For $i = 0 to UBound ($ip)
	$ip[$i] = Val($ip[$i])
Next
$ip = Join($ip, ".")


Edited by Mart (2007-10-29 04:10 PM)
Edit Reason: typo
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#182052 - 2007-10-29 04:20 PM Re: Reading a ini file [Re: Mart]
noobness Offline
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

 Code:
<?

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
 Code:
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
 Code:
$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
#182053 - 2007-10-29 04:21 PM Re: Reading a ini file [Re: Mart]
Jochen Administrator Online   happy
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6343
Loc: Stuttgart, Germany
shorter version:

 Code:

$ip = join(split($ip,'0'),'')

_________________________



Top
#182054 - 2007-10-29 04:26 PM Re: Reading a ini file [Re: Jochen]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4628
Loc: The Netherlands
Youíre still in the golfing mood I see
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 162 anonymous users online.
Newest Members
EricMyers, siddharthinfosy, raynebc, MasterNinjaSkar, hienttt
17516 Registered Users

Generated in 0.142 seconds in which 0.073 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org