Tuesday, September 11, 2012

Linux/Unix Bash shell script to find out machine/system IP address

This is a script which will show you IP address assigned to your machine. This script even take cares even if the host have multiple interfaces and shows the assigned IP address.


#!/bin/bash
#Purpose: To show the IP address of a machine
#Author:Surendra Anne
#Date: 2012-09-11
#Licence: GPLv3

for i in `ifconfig | cut -d" " -f1 | sort | grep -v '^$'`
do
if ifconfig $i | grep addr: &> /dev/null
then
echo "The interface $i have IP Address:$(ifconfig $i | grep addr: | awk '{print $2}' | cut -d":" -f2)"
fi
done

Your are free to add/modify this script and use it for your purpose. Some of the tools used here are

SORT command
cut command
ifconfig command
grep command
redirection operation
echo command
awk command
for loop
if statement

0 comments:

Post a Comment