Shell Script to Find IP Address from Multiple Servers

In this tutorial, we learn how to write a shell script to find IP address from multiple Linux servers. This is very helpful when doing some auditing on the servers.

Shell script can be used to find ethernet interface details and IP address/netmask assigned from multiple servers.

The 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 login using ssh without a password.

Shell Script to get IP address

In the following script, 'server_list.txt' file keeps the server lists and 'result.txt' file contains all output with servername and ip details.


#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 root@${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 '[^:]*

Sample Output


Server1#cat result.txt
eth0 : :

eth1 : :

venet0 : no ip address set
"` #same for mask MASK=`ssh $server "ifconfig $ip |grep -o -e 'Mask:[^ ]*' | grep -o -e '[^:]*

Sample Output

"` #this check if any ip assigned if [ -x $INET ]; then #if no ip we will get no ip address set INET="no ip address set" fi #we check if we get mask if [ -x $MASK ]; then MASK="no mask address set" fi #and we put result to result file. echo "$ip : $INET : $MASK" >> $result done #this print empty line to result file echo “” >> $result done

Sample Output


Leave a Comment