[Chicago-talk] Any handy operators for finding the local IP?

Steven Lembark lembark at wrkhors.com
Tue Mar 22 20:36:17 PST 2005

-- Richard Reina <richard at rushlogistics.com>

> I know there's a handy $< to find the UID of the user
> executing the prorgram.  Is there any handy operator
> to find the IP address of the local machine that is
> running the program.

If you know the interface name it's not at all difficult
to parse ifconfig's output:

	x qx(/sbin/ifconfig eth0) =~ /inet addr:(\S+)/
	0  ''

If you're writing local code then the interface name
is a reasonable guess. For the general case you can

	  DB<5> x ( $a = qx( /sbin/ifconfig ) ) =~ /inet addr:(\S+)/g
	0  ''
	1  ''
	2  ''
And pick the one that seems most useful (e.g. the
lowest subnet).

If all of your hosts are singly-homed (which the example
above is NOT) then filtering out works. In most
cases you know the subnet you're ineterested in and can
search for that:

		  DB<6> x ( $a = qx( /sbin/ifconfig ) ) =~ /inet addr:(192.168.\S+)/g
	0  ''
	1  ''

Which would give you a single value on most systems (this
was run in perl -d on a basion host with DMZ and LAN interfaces).

Steven Lembark                                       85-09 90th Street
Workhorse Computing                                Woodhaven, NY 11421
lembark at wrkhors.com                                     1 888 359 3508

More information about the Chicago-talk mailing list