netstat command to be executed for list of hosts

Hi All,

Need to run the netstat -i command on the list of hosts and check if "Ierrs" and "Oerrs" has value greaterthan 0.
for Ex: below output, driver bge1 and bge3 has Oerrs and Ierrs value > 0, So, script should report saying

"Netstat status for $host, driver bge1 has Oerrs = 20, Failed"
"Netstat status for $host, driver bge3 has Ierrs = 10, Failed"

Netstat -i ouptut :

root@1Q-01 # netstat -i
Name  Mtu  Net/Dest      Address     Ipkts     Ierrs  Opkts    Oerrs Collis Queue
lo0   8232 loopback      localhost   12167130  0      12167130  0     0      0
bge0  1500 1Q-01 	1Q-01 		 6118243   0       1204595  0     0      0
bge1  1500 1Q-01-DFT 	 1Q-01-DFT 	 6572109   0       2907975 20     0      0
bge3  1500 1Q-01-BD1 	 1Q-01-BD1 	 3416729  10         89438  0     0      0

main script :

        Serverlist=../Serverlist
        echo "Running netstat status"
        cat $Serverlist | grep -v '#'
        if [ ! $? ]; then
                logIt "error no Serverlist"
                exit
        else
                list=`cat $Serverlist | sort | grep -v '#' | awk '{print $2}' `
                export list
                for ip in $list; do
                        check=$STATUS_OK;
                        host=`grep -v "#" $Serverlist | grep -w $ip | awk '{print $1}'`
                        netstat -i | head -5 | tail -4 | awk '{ print $1 " " $6 " " $8 }'| while read output;
                        do
                                driver=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
                                Ierrs=$(echo $output | awk '{ print $2 }' )
				Oerrs=$(echo $output | awk '{ print $3 }' )
                                  if [ $Ierrs -ge 0 ]; then
					echo "Netsat status for $driver, has input error $Ierrs on $host as on $(date)"
				 fi
				 if [ $0errs -ge 0 ]; then
					echo "Netsat status for $driver, has output error $Oerrs on $host as on $(date)"
									
                                 fi
                        done
                done
        fi

Serverlist file has 2 hosts ip address/name

1Q-01 10.11.122.10
2Q-02 10.11.122.13

Problem is am facing is, i don't know how to run this script or netstat command on each hosts listed in serverlist file.

Am running this main script from management server which has access to all the hosts which are listed in serverlist file.

Anyhelp much appriciated.

Many Thanks,
O

Use ssh to remotely run the command:

#!/bin/ksh

while read name ipaddr
do
        ssh -n "$ipaddr" "netstat -i" 2> /dev/null | while read name mtu netdest addr ipkts ierrs opkts oerrs collis queue
        do
                [ "$name" = "Name" ] && continue
                [ $ierrs -gt 0 ] && print "Error message here"
                [ $oerrs -gt 0 ] && print "Error message here"
        done
done < "$Serverlist"

Thanks Yoda. Really SSH trick works but only concern is that, is there way through script itself we can enter "yes" on times when its prompt from some server..

am talking about handling this when we do SSH to some host :

root@1Q-V01 # ssh -n "1Q-01" "netstat -i" 2
The authenticity of host '1q-01 (10.11.122.11)' can't be established.
RSA key fingerprint is 92:9e:53:75:02:86:06:84:19:aa:b4:72:da:18:42:db.
Are you sure you want to continue connecting (yes/no)? yes

You can use below option to avoid that prompt:

-o StrictHostKeyChecking=no 
2 Likes

Thanks Yoda. it worked.

Can you tell me how can i skip firs line(which is headerline) and the last line(blank line) while reading netstat -i command, otherwise i get "unary operator expected error" when it's evaluating if conditions for ierrs/oerrs for headerline.

Name  Mtu  Net/Dest      Address     Ipkts     Ierrs  Opkts    Oerrs Collis Queue

To skip header and blank line, put a check on variable: name

if [ "$name" = "Name" ] || [ -z "$name" ]
then
        continue
fi