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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get IP address of machine

Status
Not open for further replies.

pdawlall

Programmer
Mar 6, 2003
5
ZA
Hi..
I need some help. Is there a command that can generate the ip address of the machine that im working on in perl.
I dont want to write down the ip address of my machine but i want it to get it by some command in case i have to use my program on a different machine.

Thanks
 
Courtesy of missbarbell, try:

use Socket 'inet_ntoa';
use Sys::Hostname 'hostname';
$addr = inet_ntoa(scalar gethostbyname(hostname() || 'localhost'));
print "$addr\n";
 
Hi
Thanks, I've tried it but i get an error message like :
Bad arg length for Socket::inet_ntoa, length is 29, should be 4

Is there any other way to get the ip address
 
I don't know of any other way to get perl to read the IP address. The relevant perldoc does say that the method above does require the system concerned to have working DNS - is this functioning on your system?

The only other possibility that I can think of is to use an operating system command to write the address to a file then use perl to pull it from the file (not an elegant solution).
 
Thanks again..
I do have a working DNS on my system. I've tried various possibilities from the internet but the only one that works is when i check the network config. and write it down myself in my perl program. It works fine, but when i run my program on another machine then i'll have to go check that ip address and include that in my perl script.

What was the other possibility you were speaking about and how do i do that??

Thanks again
 
You could use whatever command you are currently running to get the IP address and pipe it to a file. So, for example, on an NT workstation you do:

ipconfig > ipaddress.txt

You then open ipaddress.txt and use a regex to pull the IP address out of the relevant line. Not pretty, but I can't see why it should not work unless you are intending to run your script on machines which have different operating systems.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top