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

Getting a Unix server IP address

Status
Not open for further replies.

landhippy

Programmer
Feb 7, 2003
9
GB
I need to run a Perl script to return the IP address of the server the script is running on.

 
use Socket 'inet_ntoa';
use Sys::Hostname 'hostname';
$addr = inet_ntoa(scalar gethostbyname(hostname() || 'localhost'));
print "$addr\n";

Should do the trick

Barbie
Leader of Birmingham Perl Mongers
 
Thanks, I had come up with the following:

use Socket; #Include the socket library.

$hostName = $ARGV[0]; #Grab command line parameter
$addr1 = inet_aton($hostName); #Get IP address in internal format.
$addr2 = inet_ntoa($addr1); #Get the IP address in dotted notation.

print $addr2; #Print the IP address to standard output.

Both seem to work fine.

Thanks for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top