Name

getipaddr — Get information about IPv4/IPv6 Addresses and Network Interface Devices configured on this NST system.

Synopsis

getipaddr [ -i DEVICE | --interface DEVICE ] [ -D [true]|false | --net-int-devices [true]|false ] [ --virtual [true]|false ] [ --physical [true]|false ] [ --ip-alias [true]|false ] [ --dump-xml [true]|false ] [ --ip-network-address-cidr [true]|false ] [ --ip-network-address-subnet [true]|false ] [ --ip-broadcast-address [true]|false ] [ -d [true]|false | --default-address [true]|false ] [ -r [true]|false | --default-route [true]|false ] [ -p [true]|false | --public-address [true]|false ] [ --public-name [true]|false ] [ --public-domain [true]|false ] [ -l HOSTNAME | --lookup HOSTNAME ] [ -x IPv4ADDR | --hostbyaddr IPv4ADDR ] [ -X IPv4ADDR | --hostsbyaddr IPv4ADDR ] [ -s HOSTNAME|IPv4ADDR | --source HOSTNAME|IPv4ADDR ] [ --IPv6 [true]|false ] [ --IPv6-CIDR [true]|false ] [ -w URL | --web-service URL ] [ -f [true]|false | --flush-cache [true]|false ] [ -c [true]|false | --cache-display [true]|false ] [ -h [true]|false | --help [true]|false ] [ -H [true]|false | --help-long [true]|false ] [ -v [true]|false | --verbose [true]|false ] [ --version [true]|false ]

Description

This network utility returns the IPv4 Address of the selected interface. If no interface is specified, all IPv4 Addresses configured on this system including the public Internet address will be displayed. All public information requires a CGI script running at a web server (see the "Check IP" tool under the "IP Tools" section at http://www.networksecuritytoolkit.org/ for additional details).

This script can also be used to lookup the IPv4 Address(es) associated with a particular host name via the --lookup HOST option.

This script can also be used to lookup the host name associated with a particular IPv4 Address via the --hostbyaddr IP option.

This script can also be used to determine the source IPv4 Address that will be used to reach a particular destination IPv4 Address. This is done via the --source HOSTNAME|IPv4 option. This can be particularily useful if one needs to determine the IPv4 Address of the machine with respect to the local network. For example, running: getipaddr --source www.google.com will typically report the IPv4 Address of your machine from a LAN perspective.

Note

Web service order for resolving the public IPv4 Address information:

  • Optional URL specified web service.

  • Last good URL web service response cached value found in: ${HOME}/.getipaddr.cache.

  • Cycle through URL web service list found in variable: GET_IP_ADDR_URL - which is found in either configuration file: "${HOME}/.getipaddr.conf" or "/etc/getipaddr.conf".

Here are some examples of using getipaddr on system "probe":

[root@probe ~]# getipaddr
10.222.211.3
172.24.1.5
172.24.1.101
172.24.1.102
127.0.0.1
65.29.66.15
[root@probe ~]# getipaddr --default-address
10.222.211.3
[root@probe ~]# getipaddr --default-route
10.222.211.1
[root@probe ~]# getipaddr --public-name
cpe-65-29-66-15.indy.res.rr.com
[root@probe ~]# getipaddr --public-domain
indy.res.rr.com
[root@probe ~]# getipaddr --lookup www.cnn.com
64.236.24.28
64.236.29.120
64.236.16.20
64.236.16.52
64.236.16.84
64.236.16.116
64.236.24.12
64.236.24.20
[root@probe ~]# getipaddr --source www.cnn.com
10.222.211.3
[root@probe ~]# getipaddr --interface eth0 --ip-network-address-cidr
10.222.211.0/24
[root@probe ~]# getipaddr --interface eth1 --ip-network-address-subnet
172.24.0.0/255.255.0.0
[root@probe ~]# getipaddr --interface eth0 --ip-broadcast-address
10.222.211.255
[root@probe ~]# getipaddr --interface eth1 --ip-broadcast-address
172.24.255.255
[root@probe ~]# getipaddr --interface eth1 --ip-alias
172.24.1.5
172.24.1.101
172.24.1.102
[root@probe ~]# getipaddr -D --ip-alias
lo
eth0
eth1
eth1:0
eth1:1
[root@probe ~]# getipaddr -i all -D --ip-network-address-cidr
lo: 127.0.0.1/8
eth0: 10.222.211.3/24
eth1: 172.24.1.5/24
eth1:0: 172.24.1.101/24
eth1:1: 172.24.1.102/24
[root@probe ~]# getipaddr --IPv6
::1
2001:db8:0:f102::1
2001:db8:0:f101::1
fe80::3285:a9ff:fe44:7e37
fe80::a236:9fff:fe00:696a
[root@probe ~]# getipaddr --IPv6-CIDR -i eth1
2001:db8:0:f102::1/64
2001:db8:0:f101::1/64
fe80::3285:a9ff:fe44:7e37/64

Network Interface Device listing example using getipaddr on system "pktcap2":

[root@pktcap2 ~]# getipaddr -D
dummy0
vmnet1
vmnet8
lo
p2p1
p3p1
p3p2
p4p1
p5p1
p5p2
p7p1
[root@pktcap2 ~]# getipaddr -D --virtual
dummy0
vmnet1
vmnet8
lo
[root@pktcap2 ~]# getipaddr -D --physical
p2p1
p3p1
p3p2
p4p1
p5p1
p5p2
p7p1
[root@pktcap2 ~]# getipaddr -D --ip-alias
p2p1
p3p1
p3p2
p4p1
p5p1
p5p1:a1
p5p1:a2
p5p2
p7p1

HOSTNAME/IPv4Lookup

The --hostbyaddr IPv4 (-x IPv4) option allows one to lookup a host name associated with a particular address (use --hostsbyaddr IPv4 or -X IPv4 if you want additional names associated with the IPv4 Address).

The --lookup HOSTNAME (-l HOSTNAME) option is used to find the IPv4 Aaddress associated with a particular host name.

As an example of using these options, let's consider a system configured to use /etc/hosts as its first source for name resolution and DNS as its second source. For this example, we'll assume that the /etc/hosts consists of the following two entries:

127.0.0.1		localhost.localdomain localhost
192.168.200.2		server.linux.bogus disks printers mail

The following demonstrates the output one might see given these settings:


[root@probe ~]# getipaddr --hostbyaddr 192.168.200.5
[root@probe ~]# echo $?
1
[root@probe ~]# getipaddr --hostbyaddr 192.168.200.2
server.linux.bogus
[root@probe ~]# echo $?
0
[root@probe ~]# getipaddr --hostsbyaddr 192.168.200.5
[root@probe ~]# echo $?
1
[root@probe ~]# getipaddr --hostsbyaddr 192.168.200.2
server.linux.bogus
disks
printers
mail
[root@probe ~]# echo $?
0
[root@probe ~]# getipaddr --lookup bad.linux.bogus
[root@probe ~]# echo $?
1
[root@probe ~]# getipaddr --lookup server.linux.bogus
192.168.200.2
[root@probe ~]# echo $?
0
[root@probe ~]# 
[root@probe ~]# getipaddr --lookup mail
192.168.200.2
[root@probe ~]# echo $?
0
[root@probe ~]# getipaddr --lookup www.networksecuritytoolkit.org
70.103.140.142
[root@probe ~]# echo $?
0
[root@probe ~]# getipaddr --hostbyaddr 70.103.140.142
box142.bluehost.com
[root@probe ~]# echo $?
0
[root@probe ~]# getipaddr --hostsbyaddr 70.103.140.142
box142.bluehost.com
[root@probe ~]# echo $?
0
[root@probe ~]# 

Global Configuration File

This section describes the content of the getipaddr global configuration file: "/etc/getipaddr.conf". This file currently holds the URL web service list, a default wget timeout value and a default wget retry count value. This global configuration file is sourced by the getipaddr script resulting with the following variables that will be known by this script:

GET_IP_ADDR_URL

Space separate list of URL(s) web service host entries that can be used to fetch the public IPv4 address from.

WGET_TIMEOUT

Set the default wget network timeout for queries made to web service hosts to obtain the public IPv4 address.

WGET_RETRYCNT

Set the default wget retry count to a number of times.

The following is an example global configuration file found on a NST probe:

[root@probe ~]# /bin/cat /etc/getipaddr.conf
#
# Default values for the getipaddr script set at the build time of the NST.

#
# The space separate list of URL(s) we can use to fetch our public IPv4
# Address from
GET_IP_ADDR_URL="http://nst.sourceforge.net/nst/cgi-bin/ip.cgi http://www.networksecuritytoolkit.org/nst/cgi-bin/ip.cgi http://www.univstech.com/nst/cgi-bin/ip.cgi";

#
# set the default wget timeout to: 10 seconds...
WGET_TIMEOUT=10;

#
# set the default wget retry count to: 8...
WGET_RETRYCNT=8;
     
   

Options

The following command line options are available:

[-i DEVICE] | [--interface DEVICE]

This option allows to specify a specific network interface device to obtain it's IPv4 Address. If not specified, the IPv4 Address associated with ALL network interfaces (or no interfaces depending on the other options you specified) will be displayed. Specify 'disable' to prevent the display of IPv4 Addresses associated with interface(s). Specify 'all' to force the display of IPv4 Addresses for All network interface devices. Add the [--ip-alias] option to include all IPv4 Alias Addresses.

[-D [true]|false] | [--net-int-devices [true]|false]

This option is used to Display All available Network Interface devices. These devices can be found in the Linux Virtual File System: "Sysfs" listed under directory: "/sys/class/net". Use the [--virtual] option as a filter to display only the "Virtual" Network Interface devices. Use the [--physical] option as a filter to display only the "Physical" Network Interface devices. Add the [--ip-alias] option to include all IPv4 Alias Network Interfaces.

[--virtual [true]|false]

This option is used as a filter with the [-D|--net-int-devices] option to Only Display available "Virtual" Network Interface devices.

[--physical [true]|false]

This option is used as a filter with the [-D|--net-int-devices] option to Only Display available "Physical" Network Interface devices.

[--ip-alias [true]|false]

Use this option to include all IPv4 Alias Addresses when the [-i|--interface] option is selected for displaying the IPv4 Address bound to the Network Interface. One can also use this option to include all IPv4 Alias Network Interfaces when the [-D|--net-int-devices] option is used for displaying all available Network Interface devices.

[--dump-xml [true]|false]

This option displays all available network interfaces in XML format including configuration settings and state information. ***Note: Most options are ignored when this parameter is used except for the "--public-name", "--public-domain" and the "--flush-cache" option. Information for any detected IPv4 Alias Addresses will be included in the output.

[--ip-network-address-cidr [true]|false]

This option displays the IPv4 network address in CIDR (Classless Inter-Domain Routing) notation for a specific network interface device. The [-i|--interface DEVICE] network interface device parameter is required for this option.

[--ip-network-address-subnet [true]|false]

This option displays the IPv4 Network Address in subnet format for a specific network interface device. The [-i|--interface DEVICE] network interface device parameter is required for this option.

[--ip-broadcast-address [true]|false]

This option displays the IPv4 Broadcast Address for a specific network interface device. The [-i|--interface DEVICE] network interface device parameter is required for this option.

[-d [true]|false] | [--default-address [true]|false]

If you specify this option, getipaddr will display the IPv4 Address associated with the default Network Interface Device. The following sequence will be used to determine the default Network Interface Device. Initially, the Network Interface Device associated with the first detected default gateway (route) will be used. If no default gateway is defined, then the first detected Network Interface Device in the routing table will be used. If no Network Interface Device is configured, then finally the Loopback Device: (lo) will be used. Once a default Network Interface Device is determined, the associated IPv4 Address for this device will be displayed.

[-r [true]|false] | [--default-route [true]|false]

If you specify this option, getipaddr will display the IPv4 Address associated with the first detected default route (gateway) configured on this system.

[-p [true]|false] | [--public-address [true]|false]

If you specify this option, getipaddr will use the wget utility in combination with the web service URL to report the IPv4 Address of the system as it appears to the rest of the Internet. This option can also be used with the "--dump-xml" option so that the public IPv4 Address will be included with the XML document.

[--public-name [true]|false]

If you specify this option, the getipaddr will use the wget and dig utilities in combination with the web service URL to report the host name of the system as it appears to the rest of the Internet. This option can also be used with the "--dump-xml" option so that the public host name will be included with the XML document.

[--public-domain [true]|false]

If you specify this option, the getipaddr will use the wget and dig utilities in combination with the web service URL to report the domain of the system as it appears to the rest of the Internet.

[-l HOSTNAME] | [--lookup HOSTNAME]

Use this option to look up the IPv4 Address(es) associated with a particular host. For example: getipaddr -l www.networksecuritytoolkit.org. If you use this option, all other options are ignored (we will only lookup the host specified in this mode). NOTE: Its legal to enter a numeric IPv4 Address (like: 192.168.0.2), we will simply echo back the value specified. The script will only exit with a return code of 0 if at least one IPv4 Address is found and displayed.

[-x IPv4ADDR] | [--hostbyaddr IPv4ADDR]

Use this option to look up the host name associated with a particular IPv4 Address. The normal name resolution mechanisms (defined in /etc/nsswitch.conf will be used when resolving the IPv4 Address to a host name. Only one host name will be returned by this method (even if multiple host names are associated with the Address).

[-X IPv4ADDR] | [--hostsbyaddr IPv4ADDR]

Use this option to look up the host name and alias(es) associated with a particular IPv4 Address. The normal name resolution mechanisms (defined in /etc/nsswitch.conf will be used when resolving the IPv4 Address. Multiple names may be returned (space separated).

[-s HOSTNAME|IPv4ADDR] | [--source HOSTNAME|IPv4ADDR]

This option allows one to determine the source IPv4 Address (an IPv4 Address on this system), that will be used to reach the destination Host Name or IPv4 Address specified on the command line. For example: getipaddr -s www.networksecuritytoolkit.org. If you use this option, all other options are ignored. NOTE: If your routing table is configured such that there are multiple ways to reach the destination IPv4, only the first entry reported by ip route get IPv4 will be used. The script will only exit with a return code of 0 if the IPv4 Address is determined and displayed.

[--IPv6 [true]|false]

By default, IPv4 (Version: 4) Addresses are displayed. If you include this option, we will display the IPv6 (Version: 6) Addresses instead.

[--IPv6-CIDR [true]|false]

By default, IPv4 (Version: 4) Addresses are displayed. If you include this option, we will display the IPv6 (Version: 6) Addresses instead in CIDR (Classless Inter-Domain Routing) notation.

[-w URL] | [--web-service URL]

This optional URL is used to specify a specific web service that returns the IPv4 Address of your system as seen by the rest of the Internet. The default URL Web service list is found in: $HOME/.getipaddr.conf or /etc/getipaddr.conf and is associated with GET_IP_ADDR_URL configuration value.

[-f [true]|false] | [--flush-cache [true]|false]

This option will clear any cached information. This will cause a fresh copy of any necessary public information to be retrieved from the Internet.

[-c [true]|false] | [--cache-display [true]|false]

The getipaddr script caches information retrieved from the Internet to speed up its response. Use this option to display the current contents of the cache.

[-h [true]|false] | [--help [true]|false]

When this option is specified, getipaddr will display a short one line description of getipaddr, followed by a short description of each of the supported command line options. After displaying this information getipaddr will terminate.

[-H [true]|false] | [--help-long [true]|false]

This option will attempt to pull up additional getipaddr documentation within a text based web browser. You can force which browser we use setting the environment variable TEXTBROWSER, otherwise, we will search for some common ones.

[-v [true]|false] | [--verbose [true]|false]

When you set this option to true, getipaddr will produce additional output. This is typically used for diagnostic purposes to help track down when things go wrong.

[--version [true]|false]

If this option is specified, the version number of the script is displayed.

Files

/etc/getipaddr.conf

Global configuration file for getipaddr.

${HOME}/.getipaddr.conf

Optional configuration file for getipaddr. If this configuration file is found it will take precedence over the global configuration file: "/etc/getipaddr.conf".

${HOME}/.getipaddr.cache

File written by the getipaddr script to cache information once it has been determined (to speed up the response of future invocations).

${HOME}/.getipaddr.iftmp

Temporary file created to hold ifconfig output. This file should be cleaned up once the getipaddr script terminates.

${HOME}/.getipaddr.wget

Temporary file created to hold wget output. This file should be cleaned up once the getipaddr script terminates.

Environment

TEXTBROWSER

This controls what text based browser is used to display help information about the script. If not set, we will search your system for available text-based browsers (Ex: elinks, lynx ...).

See Also

dig(1), wget(1), Network Security Toolkit