0

In Short, I want to split single line output string into multiple rows and label each raw with the hostname.

Hostname ProcessName CPUCore

output=$(ssh -q -o "StrictHostKeyChecking yes" $ssh_host 'ps -eo comm,lastcpu | grep rrcp')

How to insert loop through a return out put that looks like.

rrcpd 17 rrcpd 0

and re-write as

Hostname1 rrcpd 17
Hostname1 rrcpd 0

Note

  • The numbers can very from 0-32

  • Hostname is a variable string $myhost

  • The text does change, meaning it is not strictly rrcpd, it could be e.g.

    rrcpd 17 rrcpd 0

    Cheese 3 cakes 8

    Cats 9 dogs 3

Command that run on each host

output=$(ssh -q -o "StrictHostKeyChecking yes" $ssh_host 'ps -eo comm,lastcpu | grep rrcp')

The output can come back with 3-5 process when I remove the grep, hence I want to be able to split each output and add the hostname to the raw to label each output. I am checking what processes are running on each host.

rrcpd 0 rrcp_mon.sh 24

rrcpd 0

rrcpd 0

rrcpd 0 rrcpd 1

rrcpd 0 rrcpd 0 rrcp_mon 24 rrcp_mon 24

rrcpd 0

rrcpd 0

rrcpd 17 rrcpd 0

ard 9 ssh 32 httpd 21 bax 22

I have modified the provided below and managed to get it to work

#!/bin/bash
for remote in $(cat ssh_hosts2.txt)
do
    ssh -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
    sed "s/^/$remote /"
done
  • I don't get it. Please add a proper example and result. For the given examples, the answer is valid. Please properly format you text so that we don't have to guess what you mean. – pLumo Aug 13 '18 at 11:45
  • Why is the last line outputted if you grep for rrcp? – Kusalananda Aug 13 '18 at 11:46
2

please try this,

I have saved the given sample output in file /tmp/test.txt.

 xargs -n 2 | sed 's/^/Hostname1 /' 
  • -n means maximum arguments per line.

For example;

cat /tmp/test.txt  | xargs -n 2 | sed 's/^/Hostname1 /g' 
Hostname1 rrcpd 17
Hostname1 rrcpd 0
Hostname1 Cheese 3
Hostname1 cakes 8
Hostname1 Cats 9
Hostname1 dogs 3
  • There is only one match of ^ per line, so using /g is not needed. – Kusalananda Aug 13 '18 at 12:02
0

The only reason you would see the type of data that you show (more than two fields per line in the output of the ssh command) is when you collect the output of your ssh+ps call into a variable and then output it without quoting it properly, as in output=$(ssh ...) followed by echo $output instead of echo "$output". Luckily, you don't need to collect the data in a variable at all.

while read remote; do
    ssh -n -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
    awk -v host="$remote" '/rrcp/ { print host, $1, $2 }'
done <hosts.txt

Here, awk is reading directly from the ssh command and also does the work originally done by grep in your code. It assumes that it will get lines with two fields and will prefix each pair of fields with the given hostname (read from hosts.txt).

Alternatively,

while read remote; do
    ssh -n -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu |
    sed "s/^/$remote /"
done <hosts.txt

Here, we simply prefix each line with the hostname read from the text file.

  • I want to read the output in of a single line of string and numbers which is normally process name followed by a number which is the core. Sometimes its a single text and number and sometimes it return 2, 3 or 4 processes with text followed by number. My aim is to get this command output ps -eo comm,lastcpu and split each text number, text number ........ to multiple rows and label each row with a host name followed by text number. – SaSa2929 Aug 13 '18 at 12:21
  • @SaSa2929 Did you actually test the code I posted? The ps utility will only ever output two columns. The only reason you find yourself working with lines of more columns is that you echo an unquoted variable. – Kusalananda Aug 13 '18 at 12:25
  • Thank you, it works, almost, Tried it, it only reads the first host, how do I get it to loop to the next host? – SaSa2929 Aug 13 '18 at 12:34
  • @SaSa2929 Ah, I forgot, ssh would eat the data from the input file. Use ssh -n. I will update the answer... – Kusalananda Aug 13 '18 at 12:38
  • I have modified it to use a for loop and it works now and returns all the processes, how is it possible to go one step further and provide a list of processes that I am interested in and ignore all others. I can defind the processes in an array of some sort, or include list, all others are exluded. #!/bin/bash for remote in $(cat ssh_hosts2.txt) do ssh -q -o StrictHostKeyChecking=yes "$remote" ps -eo comm,lastcpu | sed "s/^/$remote /" done – SaSa2929 Aug 13 '18 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.