How find IP Address using Bash Script from Linux Servers

This shell script can used to find ethernet interface details and ip address /subnet mask assigned from multiple servers. Script will also display interface name even if no ip address is set. Make sure you have authorized keys set so each server can be logined using ssh without a password.

Fetch IP address for each interface


#we save to variable name of file with list of server

#we save to variable list of servers from file
servers=`cat $serverlist`

#we save in variable name of file with results

#we get each server from servers variable
for server in $servers


#Printing servername to result file
echo -e "$server"> $result

#getting list of ethernet links via ifconfig command, and from all output we need onlu lines that have word link and starting with [a-z][a-z]*[0-9], and after we get first word from output
ips=`ssh [email protected]${server{ "ifconfig -a | grep -o -e '[a-z][a-z]*[0-9]*[ ]*Link' |grep -v lo| perl -pe 's|^([a-z]*[0-9]*)[ ]*Link|\1|'"`

After for every ethernet link from ips variable
for ip in $ips


#we get ips, via ifconfig command, and grepping for inet addr script.
INET=`ssh root${server} "ifconfig $ip |grep -o -e 'inet addr:[^ ]*' | grep -o -e '[^:]*$'"`
#same for mask
MASK=`ssh $server "ifconfig $ip |grep -o -e 'Mask:[^ ]*' | grep -o -e '[^:]*$'"`
#this check if any ip assigned
if [ -x $INET ];


#if no ip we will get no ip address set
INET="no ip address set"

#we check if we get mask
if [ -x $MASK ];
MASK="no mask address set"
#and we put result to result file.
echo "$ip : $INET : $MASK" >> $result

#this print empty line to result file
echo “” >> $result




Server1#cat result.txt
eth0 : :

eth1 : :

venet0 : no ip address set


Bobbin Zachariah 8:41 am

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.


Your email address will not be published. Required fields are marked *

All comments are subject to moderation.