FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Try to find local network adress

 


svecia
I wish get my script to lookup my local network adress but all I get is 127.0.0.1
I know it's something like 192.168......

I tried this in perl
use Socket;
$packed_ip = gethostbyname( "localhost" );
if (defined $packed_ip) {
print inet_ntoa($packed_ip);
}

I could use another language like python or java
MrBlueSky
That's not as easy as it sounds. You want the ip-address which is bound (propably dynamic, using DHCP) to one of the computers network interfaces (probably eth0 when you are on Linux). The easiest way is propably to read the output of an external program like ifconfig (ipconfig on windows) or arp.

For example, on Linux:

Code:

@output = split(/ /, `arp -i eth0 -a`);
$output[1] =~ /([\d.]+)/;
$addr = $1;
print "$addr\n";


But this will only work if you run the script as root. If the script is run as root anyway, thats no problem, but if it is not you have to figure out another way. The same goes for ifconfig. If you are on windows this isn't a problem, and you can use the output of ipconfig or arp.
ashok
Instead, use your own script:

Code:
$packed_ip = gethostbyname( "localhost" );
if (defined $packed_ip) {
print inet_ntoa($packed_ip);
}


But the mistake here is "localhost" always refers to 127.0.0.1
Use your computer name to get the IP address your system. You can get your computer name by right clicking on My Computer>Properties>Computer Name.

Code:
$packed_ip = gethostbyname( "mycompname" );
if (defined $packed_ip) {
print inet_ntoa($packed_ip);
}


Replace mycompname with your computer name, your script will generate the IP Address assigned to your primary ethernet adapter, i.e. your system's IP.
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.