7

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

  1. ssh a remote machine (say hosti)
  2. print the machine name to a file (top_out)
  3. print the first few lines of the output of the 'top' command to the same file as in step2
  4. repeat 1-3 for an other 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 that I got had saved the top output for some machines (say like host5-8), but it was blank for the early machinessay like host1-4. If I tried without the line "echo "host$i" >> ~/mysh/top_out", I can get the top output for all the host1-8.

5
  • best to include at the top of your script to 'zero' out the file for each run (at least while testing), i.e. echo > ~/mysh/top_out. Good luck. Commented Jan 6, 2014 at 5:36
  • do you need to change this: ssh host$i "top -n1 -b | head -n 15>> ~/mysh/top_out" to this: ssh host$i "top -n1 -b | head -n 15 " >> ~/mysh/top_out, ie, the >> ~/mysh/top_out needs to be outside the quotes. Commented Jan 6, 2014 at 5:40
  • does the file ~/mysh/top_out need to reside in host1-htos8 or should it reside in the local host where ssh is executed from? Commented Jan 6, 2014 at 5:47
  • Please precise: on step 2. where have file to be located: print the machine name to a file (top_out) on remote host or on my desktop Commented Jan 6, 2014 at 5:55
  • Sorry for haven't stated the goal clearly. I want to save the file (top_out) in the local host. Commented Jan 7, 2014 at 9:28

3 Answers 3

9

When you do

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

you’re writing the output to ~/mysh/top_out on the remote host, not the local machine. The remote host might not be using the same physical home directory as your local machine. If you have NFS or something sharing your home directory on some machines but not all, then you’d see the symptoms you described.

Try doing

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

instead, or to make things slightly 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
Sign up to request clarification or add additional context in comments.

Comments

2

you can try an expect script to save the output of each hosts after it connects to it, you can also add more commands to it, p.s. : this assumes 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 e.g /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
 }

Comments

0

Check if the hosts for which you are not getting the output is showing following error:

TERM environment variable not set.

If you are getting this error for some of the hosts you can try following command:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.