Thursday, January 21, 2010

Linux shell script to print interface's IP Address

Prints out the IP Address of the given network interface followed by a trailing newline.

To install, copy and paste to a new file named
/usr/local/sbin/ipaddr
and execute the following command:
chmod 755 /usr/local/sbin/ipaddr

Usage: ipaddr <interface>
e.g. ipaddr eth0


#!/bin/sh
# Copyright (c) 2010 Sean A.O. Harney
# This script is licensed under GNU GPL version 2.0 or above

if [ $# -ne 1 ] ; then
echo -e "Usage:\t$0 <interface>" ;
exit 1 ;
fi

/sbin/ifconfig $1 | \
grep 'inet addr:' | \
awk '{ split($2, ar, ":") ; print ar[2] }' ;

exit 0 ;

No comments:

Post a Comment