[Ilugc] Find IP address of a machine

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Mon Oct 23 20:48:20 2006

On Mon, Oct 23, 2006 at 06:12:01PM +0530, Arun Velusamy wrote:

Hi all,
   I have a scenario where my perl program has to find the IP address 
of the machine.  I didn't know how to achieve this in perl (yet to look 
cpan though), so i tried doing it through shell commands.  The usual way 
we do it is by 'ifconfig'  and here is how i achieved that,

      `/sbin/ifconfig | grep inet | awk '{print $2}' | sed 's#addr:##g'`


If anyone of you could achieve this, through a simple means, plz let me 
know.
girish@linux:~/perl$ perl ip.pl 
192.168.1.2
girish@linux:~/perl$ cat ip.pl 
#!/usr/bin/env perl

use strict;

my $ip = ();

$ip = `/sbin/ifconfig eth0 | grep "inet addr" | tr -s ' ' | cut -d' ' -f3 | cut 
-d: -f2 `;

printf "%s", $ip;

Other related posts: