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 07-07-2006, 07:09 AM   #1
alix123
Member
 
Registered: Nov 2004
Posts: 63

Rep: Reputation: 15
perl + how to get ip address of local machine


I have machine which has three newtork card all assigned with 2 Ip address in the following manner :-

Local Area Connection 1
DHCP Enabled. . . . . . . . . . . : No
IP Address. . . . . . . . . . . . : 192.168.251.145
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IP Address. . . . . . . . . . . . : 192.168.251.134
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.251.99
DNS Servers . . . . . . . . . . . : 192.168.251.101

Local Area Connection 2
IP Address. . . . . . . . . . . . : 192.168.251.146
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IP Address. . . . . . . . . . . . : 192.168.251.137
Subnet Mask . . . . . . . . . . . : 255.255.255.0

Local Area Connection 3
IP Address. . . . . . . . . . . . : 192.168.251.148
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IP Address. . . . . . . . . . . . : 192.168.251.139
Subnet Mask . . . . . . . . . . . : 255.255.255.0

Now the problem is how to get the ip address(both the primary and secdonry address) based on the connection name.

for example if want to get all ip address assigned on local area connection 1 (both primary and secondary Seperately).

Are there any inbuilt functions in perl to get all ip address based on the network connection name.

Or is there any way to get it from the registry , Iam not sure whether it is stored in registry?

Last edited by alix123; 07-07-2006 at 07:11 AM.
 
Old 07-07-2006, 09:56 AM   #2
jmeads
Member
 
Registered: Dec 2004
Location: London
Distribution: Slackware
Posts: 44

Rep: Reputation: 15
guess your in windows, its a bit messy but very easy

my @data = `ipconfig`;

then read though the array for your connection name and ip addresses
 
Old 07-09-2006, 05:41 PM   #3
carcassonne
Member
 
Registered: Jul 2005
Distribution: Fedora6 x86_64
Posts: 118

Rep: Reputation: 15
Quote:
Originally Posted by alix123
Are there any inbuilt functions in perl to get all ip address based on the network connection name.
The solution given by jmeads is quite good and simple. You just have to parse the buffer since all the information is there.

I'd like to add a comment on 'inbuilt functions' in Perl. Perl is as much CPAN as you can throw at it. For instance if you use the module Net::Address::Ethernet you'll get inbuilt functionality but then there are some dependencies on other modules such as:

Env::Path
Regexp::Common
Test::Pod

In the end, just parse the buffer and that's quite good. But I thought I'd take this opportunity to mention that there are 'hundreds' of modules available for Perl from the CPAN (cpan.org) and those adds a whole lot of 'inbuilt' functionalities.

Cheers.
 
Old 07-09-2006, 08:02 PM   #4
spx2
Member
 
Registered: Dec 2005
Distribution: debian
Posts: 160

Rep: Reputation: 30
it would probably be pretty easy if you'd do something like this
use system function or exec function equivalent from
c/c++ to perl
exec("ifconfig > stuff.txt");
then you parse stuff.txt to get what you need with the
filestream functions in perl.
that would be all i think
 
Old 07-09-2006, 09:09 PM   #5
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Quote:
Originally Posted by spx2
it would probably be pretty easy if you'd do something like this
use system function or exec function equivalent from
c/c++ to perl
exec("ifconfig > stuff.txt");
then you parse stuff.txt to get what you need with the
filestream functions in perl.
that would be all i think
well if you're going to do that then might as well just open it directly
Code:
open FILEHANDLE, 'ipconfig |' or die 'blah';
by the way you don't want to use exec because it abandons this program
 
Old 07-10-2006, 06:03 AM   #6
carcassonne
Member
 
Registered: Jul 2005
Distribution: Fedora6 x86_64
Posts: 118

Rep: Reputation: 15
Quote:
Originally Posted by spx2
it would probably be pretty easy if you'd do something like this
use system function or exec function equivalent from
c/c++ to perl
exec("ifconfig > stuff.txt");
then you parse stuff.txt to get what you need with the
filestream functions in perl.
that would be all i think
This is what jmeads has suggested. What"s wrong with:

my @buffer = `ipconfig`;

Then you parse the buffer. You could even derive a hash from this if you need the info often.

Keep the complicated stuff only when it's needed. When it can be simple, keep it simple. And right.
 
  


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
mac address of local machine sadafwaqas Linux - Networking 3 05-19-2006 04:39 AM
Setup local machine to allow lan machines to retrieve its local user mail. Brian1 Linux - Networking 3 03-30-2006 05:04 AM
how to get ip address of local machine using linux commands shahidawan Linux - General 1 03-31-2005 05:26 AM
how to get ip address, broadcast address, mac address of a machine sumeshstar Programming 2 03-12-2005 04:33 AM
Problem with perl module for w3c validator to work on my local Apache+PHP+perl instal tbamt Linux - Software 0 12-16-2004 05:37 PM

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

All times are GMT -5. The time now is 02:05 AM.

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