Linux Shell Script: Find Interface IP Address From Multiple Linux Servers

April 20, 2013 | By
| Reply More

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.

Linux Shell Script

#!/bin/bash

#we save to variable name of file with list of server
serverlist='server_list.txt'

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

#we save in variable name of file with results
result='result.txt'

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

do

#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

do

#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 ];

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

Output

Server1#./ifconfig.sh.sh

Server1#cat result.txt
red
eth0 : 91.197.140.34 : 255.255.255.0

eth1 : 192.168.33.34 : 255.255.255.0

venet0 : no ip address set

more shell scripts

Filed Under : SHELL SCRIPTS

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.