netstat command to be executed for list of hosts

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting netstat command to be executed for list of hosts
# 1  
Old 10-24-2013
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 :
        echo "Running netstat status"
        cat $Serverlist | grep -v '#'
        if [ ! $? ]; then
                logIt "error no Serverlist"
                list=`cat $Serverlist | sort | grep -v '#' | awk '{print $2}' `
                export list
                for ip in $list; do
                        host=`grep -v "#" $Serverlist | grep -w $ip | awk '{print $1}'`
                        netstat -i | head -5 | tail -4 | awk '{ print $1 " " $6 " " $8 }'| while read output;
                                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)"
				 if [ $0errs -ge 0 ]; then
					echo "Netsat status for $driver, has output error $Oerrs on $host as on $(date)"

Serverlist file has 2 hosts ip address/name

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,
# 2  
Old 10-24-2013
Use ssh to remotely run the command:

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

# 3  
Old 10-24-2013
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 (' 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

# 4  
Old 10-24-2013
You can use below option to avoid that prompt:
-o StrictHostKeyChecking=no

These 2 Users Gave Thanks to Yoda For This Post:
# 5  
Old 10-25-2013
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

Last edited by Optimus81; 10-25-2013 at 12:10 PM..
# 6  
Old 10-25-2013
To skip header and blank line, put a check on variable: name
if [ "$name" = "Name" ] || [ -z "$name" ]

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

List commands executed on a server

Hi All, how to list all the commands executed by users on a linux server.... I dont have access to others .bash_history. and i am not root user.. Please help.. Thanks in advance.. (6 Replies)
Discussion started by: nanz143
6 Replies

2. UNIX for Dummies Questions & Answers

Set Command to output a log of every command executed in the script

Hi Guys, I like to output every command executed in the script to a file. I have tried set -x which does the same. But it is not giving the logs of the child script which is being called from my script. Is there any parameters in the Set command or someother way where i can see the log... (2 Replies)
Discussion started by: mac4rfree
2 Replies

3. UNIX for Dummies Questions & Answers

Need help with a netstat command

Do I have this command correct to show all current connections/sessions my Solaris box has? It does not seem to do anything. netstat -an | grep EST (6 Replies)
Discussion started by: SIFT3R
6 Replies

4. Shell Programming and Scripting

netstat command

Hi, In my project we use sftp with batch mode (password less) script in parallel for 14 sessions which connects to 2 different servers alternatively i.e. 7 connects to one server say server1 and the other 7 connects to say server 2. Now the problem is that these 14 sessions are run in... (5 Replies)
Discussion started by: dips_ag
5 Replies

5. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi "" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

6. Solaris

Check executed commands from remote hosts

Hello, Is there any way to check which user and from which IP executed a command to the server.I need something like the history but with information also from which IP the command executed. Thanks in advance (8 Replies)
Discussion started by: @dagio
8 Replies

7. UNIX for Dummies Questions & Answers

Netstat command query

hy guys, I did netstat and it is listening, what can i do from the client side that to see if the port is open? Regards Charneet (1 Reply)
Discussion started by: charneet
1 Replies

8. Cybersecurity

Help Required: Command to find IP address and command executed of a user

Hi, I am trying to write a script which would figure out who has run which command and their IP. As i dont have any clue as to which commands would do this job, i request some gurus to help me on this. Thanks Vishwas (2 Replies)
Discussion started by: loggedout
2 Replies

9. UNIX for Advanced & Expert Users

Netstat command

Hi.., Now, I am reading about the netstat command and its implementation. I have doubts in some options and its functionalities, natstat - M (Which is described as display masqueraded connections), what it means? What is Forwarding Information Base.?(--fib) Thanks in advance,... (3 Replies)
Discussion started by: nagalenoj
3 Replies

10. UNIX for Dummies Questions & Answers

netstat command between clustered hosts

I have 2 clustered hosts, is it possible for me to issue a netstat command against 1 host from the other ? (4 Replies)
Discussion started by: murphyboy
4 Replies
Login or Register to Ask a Question