FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free 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.
Related topics
Windows Tips&tricks!
How to protect LAN against Worms and spyware?
I was wondering something if anyone might know the awnser...
NNTP-News Server in 5 minutes
try new things...
Need help securing wireless network
Help to set up wireless network
Show ip adress from perl -script need help
network sharing
phpBB installation on a local intranet/network help
Home Network Setup
Halo: Combat Evolved Hosting Issue (PC)
Changing to a new Router Modem ADSL
MX records Propagation time
Reply to topic    Frihost Forum Index -> Scripting -> Others

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