Linux: Find my IP address using Perl at a shell prompt

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; }'

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 11 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
11 comments… add one
  • raj Oct 23, 2007 @ 13:26

    thanks really helpful!

  • Leigh Nov 7, 2007 @ 22:42

    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; }'

  • ashish Jun 16, 2008 @ 13:24


    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

  • ian Nov 18, 2008 @ 15:38

    You can use gethostbyname in the Socket package to do the work for you:

    use Socket;
    use strict;

    my ($host) = `hostname`;

    my $iaddr = gethostbyname($host);

    if (defined($iaddr)) {
    print (inet_ntoa($iaddr), “\n”);
    } else {
    die (“ipaddr failed to get IP address for $host\n”);

  • Akshatha Jun 23, 2009 @ 4:12

    very useful.. saved lot of my time :) thanks

  • poisonbit Nov 28, 2009 @ 3:46

    I’ve made a bash function based in these post and comments.

  • Deepti Mar 5, 2010 @ 20:18

    very useful really

  • Peter Burkholder Oct 3, 2011 @ 21:44

    Thanks for figuring my ifconfig regex parsing for me. Saved me a few minutes, and it works for Mac and Linux.

  • Snap ( Oct 9, 2011 @ 7:41

    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;
    $urlLength = @url;

    for ($i=0;$i didn’t worked.\n”;
    print “\nNONE of them worked WTF !!\n”;
    print “worst luck than me ?? \n”;
    print “nah ! you probably aren’t connected\n”;


  • alex toderita Oct 11, 2013 @ 11:37
    /sbin/ip -o -4 addr show eth0 | perl -lane 'print $1 if /h(d. E)//'

    More verbose… for the kill…

    perl -MSocket -e 'require "sys/"; socket($sock, PF_INET, SOCK_STREAM, (getprotobyname("tcp"))[2]); $buf = pack("a256", "eth0"); ioctl($sock, SIOCGIFADDR(), $buf); @addr= unpack("x20 C4", $buf); print join(".", @addr),"n";'

    • Paul Rice Apr 27, 2014 @ 20:21

      God bless you, Sir! lol

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum