Reading an IP address in a shell script requires many time and various Linux distributions stores IP address in different text files. So most of us end up writing shell pipes/scripts to get the information using ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' syntax. There is an alternative to ifconfig+shell pipe hack - ifdata command. It is a little know tool that can be used to check for the existence of a network interface and find out info about your interface such as IP, netmask, MTU and much more. No need to write shell pipes and fetch information via ifconfig or ip command. This command is designed to be easily used by a shell script.
How do I install ifdata command?
Simply type the following command at shell prompt:
$ sudo apt-get install moreutils
OR
$ sudo yum install moreutils
Examples
Find out if the interface ppp0 exists or not, enter:
ifdata -e ppp0 && echo "Found" || echo "Not found"
OR
ifdata -e ppp0 && echo "Found" || { echo "Not found, starting pppd..."; pppd call barISP; }
Show eth0 configuration:
$ ifdata -p eth0
Sample outputs:
192.168.1.5 255.255.255.0 192.168.1.255 1500
Get the IPv4 address of the interface, enter:
#!/bin/bash # my firewall script # Get ip for eth0 _ip=$(ifdata -pa eth0) _ipt=/sbin/iptables ## do something on $_ip ## echo "Setting firewall for eth0 and ${_ip}..."
The complete summary of all options supported by ifdata command:
Usage: ifdata [options] iface
-e Reports interface existence via return code
-p Print out the whole config of iface
-pe Print out yes or no according to existence
-pa Print out the address
-pn Print netmask
-pN Print network address
-pb Print broadcast
-pm Print mtu
-ph Print out the hardware address
-pf Print flags
-si Print all statistics on input
-sip Print # of in packets
-sib Print # of in bytes
-sie Print # of in errors
-sid Print # of in drops
-sif Print # of in fifo overruns
-sic Print # of in compress
-sim Print # of in multicast
-so Print all statistics on output
-sop Print # of out packets
-sob Print # of out bytes
-soe Print # of out errors
-sod Print # of out drops
-sof Print # of out fifo overruns
-sox Print # of out collisions
-soc Print # of out carrier loss
-som Print # of out multicast
-bips Print # of incoming bytes per second
-bops Print # of outgoing bytes per second
I hope you will use this tiny too for the good.
Related Internet links
- Grab moreutils source code.
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop








![Top 8 Tools To Search Memory Under Linux / Unix [ Forensics Analysis ]](http://s13.cyberciti.org/images/shared/rp/3/17.jpg)

