Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to perl, trying to use gethostbyipaddr 1

Status
Not open for further replies.

5jgibbs

IS-IT--Management
Mar 8, 2005
151
US
Hi, my friend was helping me write my first perl script to edit a DHCP config file. I get a few errors when i try to run this script. Im running activeperl in windows for your information.

This is the script

<code>

#!/usr/bin/perl
use Socket;

open (FILE, "hosts.txt");
@ data = <FILE>;
close(FILE);

for($i=0,$i <= $#data; $i = $i+4){
($crap, $address) = split(/ /, $data[$i+2])
chop $address;

$host = gethostbyaddr($iaddr, AF_INET);
($host, $crap) = split(/./, $host);
print $data ($i);
}


</code>

these are the errors i get

syntax error at C:\perl_scripts\getip.pl line 8, near "4)"
syntax error at C:\perl_scripts\getip.pl line 13, near ");"
Execution of C:\perl_scripts\getip.pl aborted due to compilation errors.

Any info would be great. Thanks a ton.

 
There are couple of syntax errors in your code

Try this:

Code:
#!/usr/bin/perl
use Socket;

open (FILE, "hosts.txt");
@data = <FILE>;
close(FILE);

for($i=0[b];[/b]$i <= $#data; $i = $i+4){ # semicolon instead of comma
     ($crap, $address) = split(/ /, $data[$i+2]) ; # missing semicolon at the end of statment
     chop $address;

     $host = gethostbyaddr($iaddr, AF_INET);
     ($host, $crap) = split(/./, $host);
     print $data ($i);
     }


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
at a glance

Code:
for($i=0,$i <= $#data; $i = $i+4){
     ($crap, $address) = split(/ /, $data[$i+2])
should be
Code:
for($i=0;$i <= $#data; $i = $i+4){
     ($crap, $address) = split(/ /, $data[$i+2]);

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Also your final print statement looks funny

you sure you didn't want to print $address or $host?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top