1

I'm able to get the desired output by running this command on terminal:

top -bn 1 | grep "^ " | awk '{ printf("%-8s %-8s %-8s %-8s %-8s %-8s %-8s\n", $1, $2, $9, $10, $6, $11, $12); }' | head -n 6

And I'm able to SSH twice and get the hostname by running this on terminal:

ssh 192.168.5.209 "ssh 192.168.5.210 exec \"hostname\""

The 209 is serving as something like a portal so I can only SSH from 209 to 210. But the problem is how do I replace the "hostname" with the top command that I write above? There are too much special characters.

I am writing a script to execute from PHP btw.

3
  • It may be preferable to install your first snippet as a script in your x.x.x.210 server, and have your ssh commands call for that script directly.
    – SYN
    Dec 22, 2016 at 7:02
  • i mean, my manager need the script on the project root directory
    – Lim SY
    Dec 22, 2016 at 7:07
  • You only need to run the top -bn 1 on the target; you can do the filtering and formatting locally, with only normal (local) quoting. And FWIW you don't need grep and head, awk can subsume both of those -- but that wasn't your question. Similar: unix.stackexchange.com/questions/294676/… Dec 22, 2016 at 9:45

1 Answer 1

1

grep, awk, and head can be merged and run locally

 ssh 192.168.5.209 "ssh 192.168.5.210 top -bn 1 " |
 awk '/^ / { printf("%-8s %-8s %-8s %-8s %-8s %-8s %-8s\n", $1, $2, $9, $10, $6, $11, $12); c++ ; if (c==6) exit ;} 

where

  • /^ / search for line starting with a space
  • c++ ; if (c==6) exit ; will exit awk after 6 line printed.
2
  • hmmm... it seems to work. but now i'm getting another error : 'dumb': unknown terminal type. do you by any chance know what it is?
    – Lim SY
    Dec 22, 2016 at 9:18
  • try TERM=xterm before ssh
    – Archemar
    Dec 22, 2016 at 9:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .