LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-15-2006, 05:24 AM   #1
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Rep: Reputation: 40
perl: Get ip address of NIC (eth0)


Hi.


Anyone knows how to retreive the IP address of a network interface using Perl? I need to retreive the IP address of eth0.
 
Old 03-15-2006, 09:56 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

This is one way of doing just that:

Code:
#!/usr/bin/perl

$IP_eth0 = `ifconfig eth0`;
$IP_eth0 =~ s/.*inet addr:(.*)  Bcast:/1/;
print "IP eth0 => " . $1 . "\n";
It could be that the output of your ifconfig differs from mine. The relevant line looks like this: inet addr:192.168.101.11 Bcast:192.168.101.255 Mask:255.255.255.0 for my version. If your output differs you need to change the regular expression accordingly. If you don't know how, post the output of ifconfig eth0 and I/We will help.

Hope this helps.
 
Old 03-15-2006, 10:48 AM   #3
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by druuna
Hi,

This is one way of doing just that:

Code:
#!/usr/bin/perl

$IP_eth0 = `ifconfig eth0`;
$IP_eth0 =~ s/.*inet addr:(.*)  Bcast:/1/;
print "IP eth0 => " . $1 . "\n";
It could be that the output of your ifconfig differs from mine. The relevant line looks like this: inet addr:192.168.101.11 Bcast:192.168.101.255 Mask:255.255.255.0 for my version. If your output differs you need to change the regular expression accordingly. If you don't know how, post the output of ifconfig eth0 and I/We will help.

Hope this helps.


Thanks! I'll try it out some time later, but I'm sure it'll work. I'm using IPv6, but modifying the regular expression _should_ be manageable.
 
Old 03-18-2006, 05:13 AM   #4
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 657

Original Poster
Rep: Reputation: 40
It worked!

Again, thanks! Don't quite understand exactly _how_ the script works, though.
 
Old 03-18-2006, 05:56 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

A little breakdown of the commands:

1) $IP_eth0 = `ifconfig eth0`;
2) $IP_eth0 =~ s/.*inet addr: (.*) Bcast:/1/;
3) print "IP eth0 => " . $1 . "\n";

The first line is probably clear: fill $IP_eth0 with the output of the ifconfig eth0 command (ifconfig eth0 is not a pearl command, but a unix command).

The second line uses one of perl's regular expression ( s/regex/replacement/modifiers ) to 'cut out' the part you want.
In this case all the output stored in $IP_eth0 is checked and if the following .*inet addr: (.*) Bcast: is found, the part between the ( and ) can be represented as $1, so ( and ) are not part of the regular expression, they are special and can be used in the replacement part of the regular expression.

Take a look here perlrequick and here perlretut for a more detailed explanation.

The third line prints $1, which is what is set after the substitute command, the part between the ( and ), in the previous line.

Hope this clears things up.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
eth0 NIC Card Not Working! 3com 3c501 NIC. Mustard010 Linux - Networking 16 08-15-2007 02:22 AM
Eth0 up, has ip address, but cant connect to anything! Munix Linux - Distributions 1 04-10-2004 08:34 PM
My eth0 won't get an IP address on my LAN poeta_boy Linux - Networking 4 01-29-2004 03:00 PM
eth0 and host address tommytomato Linux - Newbie 2 12-03-2003 04:29 PM
Should i use the eth0 ip address of my internet ip address when applying iptables ForumKid Linux - Security 2 01-03-2002 08:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration