LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   perl + how to get ip address of local machine (https://www.linuxquestions.org/questions/programming-9/perl-how-to-get-ip-address-of-local-machine-461770/)

alix123 07-07-2006 07:09 AM

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?

jmeads 07-07-2006 09:56 AM

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

carcassonne 07-09-2006 05:41 PM

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.

spx2 07-09-2006 08:02 PM

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

spooon 07-09-2006 09:09 PM

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

carcassonne 07-10-2006 06:03 AM

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.


All times are GMT -5. The time now is 11:22 PM.