bash - Using xargs to ssh to multiple hosts, receiving :Name or service not known -


i writing shell script ssh multiple hosts, , perform operations on them.

my script, test.sh, looks follows:

cat < $1 | xargs -e -d ' ' -i % ssh % grep "example" /opt/example/test.log 

i run via following command

./test.sh prod_hosts.txt 

and contents of prod_hosts.txt:

hostname1 hostname2 hostname3 hostname4 

i have verified there no carraige return @ end of file, yet getting following error:

[ryan@hostname1 ~]$ ./test.sh prod_hosts.txt ssh: hostname4 : name or service not known xargs: ssh: exited status 255; aborting 

it looks ssh's 4 hosts has blank entry attempting ssh with, hence error.

any idea i'm missing here? seems i'm missing obvious!

echo '1 2' | xargs -d ' ' -i % echo % produces:

1 2 <blank line> 

whereas echo -n '1 2' | xargs -d ' ' -i % echo % returns:

1 2 

i.e. xargs decides generate 1 more output entry if input string ended newline.

workarounds:

  1. use newline delimited entries in hosts.txt and: <hosts.txt xargs -i % <ssh-command>
  2. if hosts.txt cannot changed: < <(tr ' ' '\n' < hosts.txt) xargs -i % <ssh-command>

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -