linux - Creating a single line from output of multiple shell commands -


i try ssh multiple hosts (thousands of them), capture command output , write in file. output of command should in same line, comma separated, space separated, in same line each host.

so commands like":

ssh $host "hostname; uname -r; echo mypassword| sudo -s ipmitool mc info | grep 'firmware revision' " > ssh.out 

but if use this, write command output separate lines. (3 lines per host). want 1 line per host, let's say:

myserver,3.2.45-0.6.wd.514,4.3 (comma or field separator fine) 

how can this?

use bash array variables. append output of each ssh command array. @ end, echo elements of array. result array elements on single line. set ifs (the internal field separator) desired per-host delimiter. (i used ,.) handling multiple lines of output multiple commands within same ssh session, use tr replace newlines delimiter. (i used space.)

code

sshoutput=()  host in host01 host02 host03;     sshoutput+=($(ssh $host 'echo $(hostname; uname -a; echo mypassword) | tr "\n" " "')) done  ifs=,; echo "${sshoutput[*]}"; 

output

host01.domain.com linux host01.domain.com 2.6.32-696.3.1.el6.x86_64 #1 smp thu apr 20 11:30:02 edt 2017 x86_64 x86_64 x86_64 gnu/linux mypassword ,host02.domain.com linux host02.domain.com 2.6.32-696.3.1.el6.x86_64 #1 smp thu apr 20 11:30:02 edt 2017 x86_64 x86_64 x86_64 gnu/linux mypassword ,host03.domain.com linux host03.domain.com 2.6.32-696.3.1.el6.x86_64 #1 smp thu apr 20 11:30:02 edt 2017 x86_64 x86_64 x86_64 gnu/linux mypassword  

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -