Shell script to remote ssh machines and print the output of the top command

I want to write a shell script to do the following four things:

  • ssh remote machine (say hosti)
  • print the machine name in the file (top_out)
  • print the first few lines of the "top" command output to the same file as in step 2
  • repeat 1-3 for another machine

I tried this:

#! /bin/bash for i in 1 2 3 4 5 6 7 8 do echo "host$i" >> ~/mysh/top_out ssh host$i "top -n1 -b | head -n 15>> ~/mysh/top_out" echo "done" done 

The output file I received saved the top output for some machines (for example, host5-8), but it was empty for early machinessay, like host1-4. If I tried without the string "echo" host $ i "→ ~ / mysh / top_out", I can get the top output for the whole host1-8.

+6
source share
3 answers

When you do

 ssh host$i "top -n1 -b | head -n 15>> ~/mysh/top_out" 

you write the output to ~/mysh/top_out to the remote host, and not to the local computer. The remote host may not use the same physical home directory as your local computer. If you have NFS or something sharing your home directory on some machines, but not all, then you will see the symptoms you described.

Try to do

 ssh host$i "top -n1 -b | head -n 15" >> ~/mysh/top_out 

instead, or make things a little cleaner, maybe even

 #!/bin/bash for i in $(seq 1 8); do (echo "host$i" ssh host$i "top -n1 -b | head -n 15") >> ~/mysh/top_out echo "done host$i" done 
+6
source

you can try waiting for the script to save the output of each host after it is connected to it, you can also add additional commands to it, ps: it is assumed that you have the same username and password for all hosts:

 #/usr/bin/expect -f #write your hosts on a new line inside a file and save it in your workging directory as: #host 1 #host 2 #host 3 #user '' for password if it contains special chars #pass arguments to the script as ./script $username '$password' $hosttxt set user [lindex $argv 0] set pass [lindex $argv 1] #pass path to txt file with line separated hosts set fhost [lindex $argv 2] #set this to the path where you need to save the output eg /home/user/output.txt set wd "/home/$user/log.txt" #open hosts file for parsing set fhosts [open $fhost r] exec clear #set loguser 1 proc get_top {filename line user pass} { spawn ssh -l $user $line expect { "$ " {} "(yes/no)? " { send "yes\r" expect -re "assword:|assword: " send "$pass\r" } -re "assword:|assword: " { send "$pass\r" } default { send_user "Login failed\n" exit 1 } } expect "$ " {} send "top -n1 -b | head -n 15\r" expect -re "\r\n(.*)\r(.*)(\\\$ |# )" { set outcome "$expect_out(1,string)\r" send "\r" } puts $filename "$outcome\n\n-------\n" } while {[gets $fhosts line] !=-1} { set filename [open $wd "a+"] get_top $filename $line $user $pass close $filename } 
+2
source

Check if hosts for which you are not receiving output are displayed with the following error:

Invalid TERM environment variable.

If you get this error for some hosts, you can try the following command:

ssh user @host "screen -r; top" → file_where_you_want_to_save_output

0
source

All Articles