1,105,578 Community Members

Automated ssh with top

Member Avatar
obiphil
Newbie Poster
2 posts since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, i wonder if someone can help me with this,

I am trying to get the results of top across multiple machines at the same time. I only care about the first process listed by 'top'. I can't figure out how to make this happen. I have got passwordless SSH, and have made the following script to extract the entry i care about from top.

#!/bin/sh
top | sed -n '
#Dont print all lines
/PID/ {
#if PID is found
n
#get the next line
p
#print it
}'

I dont know how to proceed to make it a) work across multiple machines simultaneously, if i ssh into another machine i get an error such as 'TERM environment variable not set', and if i run it in the background with & it just stops. b) make the output go onto the same line each time, instead of filling up my console with line after line of the same kind of output. The type of output i would like would be similar to top, just refreshing the output over the same lines.

Any kind of input towards helping me with either problem would be greatly appreciated!

Cheers,
Phil

Member Avatar
fpmurphy
Junior Poster
144 posts since Oct 2008
Reputation Points: 9 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 0 [?]
 
0
 

I suggest you start top in batch mode and with the number of iterations set to 1. This way you take a a quick snapshot of things and save it to a file.

Member Avatar
versatileint
Newbie Poster
6 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

There is the -f option with top which will help you re-direct the output to a file and then you can apply sed on the output file.

Member Avatar
obiphil
Newbie Poster
2 posts since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for the tips guys, they pointed me in the right direction. If anybody else wants to do a similar thing, here's the script i came up with in the end:

#!/bin/bash
FILE=$HOME/mpd.hosts
#IP addresses of Hosts to check must be in that file

clear
echo -e 'HOST\tUSER\tVIRT\tRES\tS\t%CPU\t%MEM\tTIME+\tCOMMAND'
while [ "$response" != q ]
do
  thisIP=30
  tput cup 1 0
  echo "                                                                                "
  tput cup 1 0
  top -b -n 1 | sed -n ';/PID/ {;n;s/[^a-z]*\([a-z]*\)[^0-9a-z]*[0-9]*[^0-9a-z]*[0-9]*/'$thisIP'\t\1/p;}' |sed -e 's/[[:space:]][[:space:]]*/\t/g' -e 's/\([^[:space:]]*\t[^[:space:]]*\t[^[:space:]]*\t[^[:space:]]*\t\)[^[:space:]]*\t/\1/' -e 's/\([0-9]*:[0-9]*\)\.[0-9]*/\1/g'
  cnt=1
  cat $FILE | while read line
  do
    cnt=$(($cnt + 1))
    thisIP=$(echo $line | sed 's/[0-9]*\.[0-9]*\.[0-9]*\.//')
    tput cup $cnt 0
    echo "                                                                                "
    #Assumes an 80 character line
    tput cup $cnt 0
	  ssh -n -XC -p 22111 $line " top -b -n 1 "| sed -n ';/PID/ {;n;s/[^a-z]*\([a-z]*\)[^0-9a-z]*[0-9]*[^0-9a-z]*[0-9]*/'$thisIP'\t\1/p;}' |sed -e 's/[[:space:]][[:space:]]*/\t/g' -e 's/\([^[:space:]]*\t[^[:space:]]*\t[^[:space:]]*\t[^[:space:]]*\t\)[^[:space:]]*\t/\1/' -e 's/\([0-9]*:[0-9]*\)\.[0-9]*/\1/g'
  done
  maxCnt=$cnt
  echo "--------------------------------------------------------------------------------"
  echo "Type q to quit or anything else to refresh sooner"
  read -s -n1 -t 5 response
  #Will accept keypress, but wont do anything about it till it reaches here
done

The script also formats the output so it has the columns i wanted, as well as displaying the output from the current machine. It assumes that the file $HOME/mpd.hosts contains a list of IP addresses, 1 per line, that you can ssh to without a password.

Question Answered as of 4 Years Ago by fpmurphy and versatileint
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article