Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Getting my IP adresses

by ruoso (Curate)
on May 14, 2007 at 16:59 UTC ( #615383=CUFP: print w/replies, xml ) Need Help??

I just wanted to point out the use of this module, because I need to find my IP address inside some script from time to time, and I just found Net::Interface.

With the following code, I find my IP addresses

map { join '.', unpack "CCCC", scalar $_->address() } Net::Interface->interfaces();

Or, to find out if a $ip is one of mine...

grep { $_ eq $ip } map { join '.', unpack "CCCC", scalar $_->address() } Net::Interface->interfaces();

Replies are listed 'Best First'.
Re: Getting my IP adresses
by cengineer (Pilgrim) on May 15, 2007 at 14:39 UTC
    Here's another way to get the IP addresses of your interfaces using IO::Interface and IO::Socket:
    #!/usr/bin/perl use strict; use warnings; use IO::Interface; use IO::Socket; # Create a simple socket to use with interfaces my $s = IO::Socket::INET->new(Proto => 'udp'); # Get list of interfaces my @interfaces = $s->if_list; my $addr; # Iterate over list of interfaces foreach (@interfaces) { $addr = $s->if_addr($_); print "interface = $_ : address = $addr\n"; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://615383]
Approved by kyle
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2020-11-26 06:11 GMT
Find Nodes?
    Voting Booth?

    No recent polls found