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:
- use newline delimited entries in hosts.txt and: <hosts.txt xargs -i % <ssh-command>
- if hosts.txt cannot changed: < <(tr ' ' '\n' < hosts.txt) xargs -i % <ssh-command>
Comments
Post a Comment