Shell script calling ssh: how to interpret wildcard on remote server -


i work on customer environment on daily basis, comprised of 5 aix servers, , need issue same command on 5 of them.

so set ssh key-based authentication between servers, , whipped little ksh script broadcasts command of them:

#!/usr/bin/ksh  if [[ $# -eq 0 ]];         print "broadcast.ksh - broadcasts command 5 xxxxxxxx environments , returns output each"         print "usage:                   ./broadcast.ksh command_to_issue"         exit fi  set -a cust_hosts aaa bbb ccc ddd eee  host in ${cust_hosts[@]};          echo "============ $host ================"          if [[ `uname -n` = $host ]];                 $*                 continue         fi          ssh $host $*  done  echo "=========================================" echo "finished" 

now, works fine, until want use wildcard on remote end, like:

./broadcast.ksh ls -l java* 

since '*' expanded on local system opposed remote.

now, if using ssh remote commands, can around using single quotes:

ssh user@host ls -l java*    <-- _not_ work expected, since asterisk interpreted locally ssh user@host 'ls -l java*'   <-- _will_ work expected, since asterisk interpreted on remote end 

now, have tried incorporate script, , have tried create $command variable made of $* contents surrounded single quotes, have drowned in sea of escaping backslashes , concatenation attempts in ksh, no avail.

i'm sure there's simple solution this, i'm not finding thought come out , ask.

thanks,

james

as found, passing asterisk argument script doesn't work because shell expands before arguments processed. try double-quoting $* , either escaping asterisks/semi-colons etc backslashes in script call, or single quoting command.

for host in ${cust_hosts[@]};     echo "============ $host ================"     if [[ `uname -n` = $host ]];         "$*"         continue     fi     ssh $host "$*" done  $ ./broadcast.ksh ls -l java\*  $ ./broadcast.ksh 'ls -l java*; ls -l *log' 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -