Q. How do I find out my IP address assigned to eth0 or ra0 interface using perl?
A. If you need to know the IP address of the UNIX / Linux machine you are running on, use the following perl one liner. Perl don't have any inbuilt facility but combination of ifconfig command ans shell pipes you can craft something as follows to display your system IP address:
Find my IP address using Perl One liner and shell pipes
Type the following command at a shell prompt:
ifconfig -a | perl -ne 'if ( m/^\s*inet (?:addr:)?([\d.]+).*?cast/ ) { print qq($1\n); exit 0; }' You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop







![Linux Copy File Command [ cp Command Examples ]](http://s13.cyberciti.org/images/shared/rp/3/23.jpg)





{ 9 comments… read them below or add one }
thanks really helpful!
For non-root users, put /sbin/ in front of the ifconfig line, so it becomes
/sbin/ifconfig -a | perl -ne 'if ( m/^\s*inet (?:addr:)?([\d.]+).*?cast/ ) { print qq($1\n); exit 0; }'
sir,
i’m trainne in system admin i working on thin client
i’ve tried the code above then i get ip addr but when i simple use ifconfig
i got the error COMMAND NOT FOUND
can u tell y
You can use gethostbyname in the Socket package to do the work for you:
use Socket;
use strict;
my ($host) = `hostname`;
chomp($host);
my $iaddr = gethostbyname($host);
if (defined($iaddr)) {
print (inet_ntoa($iaddr), “\n”);
} else {
die (“ipaddr failed to get IP address for $host\n”);
}
very useful.. saved lot of my time :) thanks
I’ve made a bash function based in these post and comments.
http://poisonbit.wordpress.com/2009/11/28/getipsfor-interfacename/
very useful really
Thanks for figuring my ifconfig regex parsing for me. Saved me a few minutes, and it works for Mac and Linux.
#!/usr/bin/perl
use LWP::UserAgent;
$browser = LWP::UserAgent -> new() or die “problem dog?\n”;
$browser -> agent(“niggaBrowzaaa”);
sub encontrarIP{
$result = $browser->request(HTTP::Request->new(GET=>$_[0]));
$content = $result->content;
}
@url=(
“http://www.showmyip.com/simple/”,
“http://showip.net/”,
“http://www.ip-adress.com/”,
“http://www.showmyip.com/”,
“http://www.showmemyip.com/”,
“http://www.showmyip.gr/”,
“http://show-my-ip.com/”);
$urlLength = @url;
for ($i=0;$i didn’t worked.\n”;
if($i==$urlLength-1){
print “\nNONE of them worked WTF !!\n”;
print “worst luck than me ?? \n”;
print “nah ! you probably aren’t connected\n”;
}
}
}