getipaddr — Get information about the IP addresses associated with the sytem.
getipaddr [
-i
DEVICE
| --interface DEVICE
] [ --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
HOST|IP
| --lookup HOST|IP
] [
-x
HOST|IP
| --hostbyaddr HOST|IP
] [
-X
HOST|IP
| --hostsbyaddr HOST|IP
] [
-s
HOST|IP
| --source HOST|IP
] [ --IPv6 [true]|false
] [
-w
PATH
| --web-service PATH
] [
-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
]
This simple utility returns the IP address of the selected interface. If no interface is specified, all IP 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 IP 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 IP address via the --hostbyaddr
IP option.
This script can also be used to determine the source IP
address that will be used to reach a particular destination IP
address. This is done via the --source HOST|IP
option. This can be particularily useful if one needs to determine
the IP address of the machine with respect to the local
network. For example, running: getipaddr --source
www.google.com will typically report the IP address of
your machine from a LAN perspective.
Web service order for resolving the public IP 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 examples of using getipaddr:
[root@probe ~]# getipaddr
10.222.211.3
172.24.1.5
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
The --hostbyaddr IP (-x
IP) option allows one to lookup a host name associated
with a particular address (use --hostsbyaddr
IP or -X IP if you want additional
names associated with the IP address).
The --lookup HOST (-l
HOST) option is used to find the IP address
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 ~]#
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_URLSpace separate list of URL(s) web service host entries that can be used to fetch the public IP address from.
WGET_TIMEOUTSet the default wget network timeout for queries made to web service hosts to obtain the public IP address.
WGET_RETRYCNTSet 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 IP
# 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;
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 IP
address. If not specified, the IP 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 IP addresses associated with
interface(s). Specify all to force the
display of IP addresses for ALL network interface
devices.
--ip-network-address-cidr [true]|false]
This option displays the IP network address in
CIDR format 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 IP 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 IP 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 IP 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 IP address for this device will be displayed.
-r [true]|false] | [--default-route [true]|false]
If you specify this option, getipaddr will display the IP 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 IP address of the system as it appears to the rest of the Internet.
--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.
--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 HOST|IP] | [--lookup HOST|IP]
Use this option to look up the IP 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
IP 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 IP address is found and
displayed.
-x HOST|IP] | [--hostbyaddr HOST|IP]
Use this option to look up the host name associated
with a particular IP address. The normal name resolution
mechanisms (defined in /etc/nsswitch.conf
will be used when resolving the IP 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 HOST|IP] | [--hostsbyaddr HOST|IP]
Use this option to look up the host name and
alias(es) associated with a particular IP address. The normal
name resolution mechanisms (defined in
/etc/nsswitch.conf will be used when
resolving the IP address. Multiple names may be returned (space
separated).
-s HOST|IP] | [--source HOST|IP]
This option allows one to determine the source IP
address (a IP address on this machine), that will be used to
reach the destination HOST or IP 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 IP, only the first entry reported by ip
route get IP will be used. The script will only exit
with a return code of 0 if the IP address is determined and
displayed.
--IPv6 [true]|false]
By default, IP version 4 addresses are displayed. If you include this option, we will display the IP version 6 addresses instead.
-w PATH] | [--web-service PATH]
This optional URL is used to
specify a specific web service that returns the
IP address of your machine 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.
/etc/getipaddr.confGlobal configuration file for getipaddr.
${HOME}/.getipaddr.confOptional configuration file for
getipaddr. If this configuration file is
found it will take precedence over the global configuration
file:
"/etc/getipaddr.conf".
${HOME}/.getipaddr.cacheFile written by the getipaddr script to cache information once it has been determined (to speed up the response of future invocations).
${HOME}/.getipaddr.iftmpTemporary file created to hold ifconfig output. This file should be cleaned up once the getipaddr script terminates.
${HOME}/.getipaddr.wgetTemporary file created to hold wget output. This file should be cleaned up once the getipaddr script terminates.