0

team, I have below cord that does ssh login and stays connected. I want to run some commands in for loop but getting some syntax errors. The same exact commands work when i manually login to node and sudo bash and just copy paste.

code

read -p "specify just the list of nodes " nodes


for node in $nodes
do
        ssh -q -F $HOME/.ssh/ssh_config -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -t $node.team.net \
        "for line in `docker ps | grep test | awk '{print $1}'`;
          do
              POD_ID=$(docker inspect $line --format='{{ index .Config.Labels "io.kubernetes.pod.uid" }}')
              POD_NAME=$(docker inspect $line --format='{{ index .Config.Labels "io.kubernetes.pod.name"}}')
              POD_VOL="/var/lib/kubelet/pods/$POD_ID/volumes"
              POD_DU=$(du -sh  $POD_VOL < /dev/null)
              HOSTNAME=$(hostname)
              AGENT_SHA=$(docker inspect $line --format='{{ index .Config.Image }}' | cut -d ':' -f2)
              STARTED_AT=$(docker inspect $line --format='{{ .State.StartedAt }}')
              echo $HOSTNAME, $POD_NAME, $POD_DU, $AGENT_SHA, $STARTED_AT
          done"
        printf "\n"
done;

output


Your new SSH certificate is ready for use!
specify just the list of nodes node1
"docker inspect" requires at least 1 argument.
See 'docker inspect --help'.

Usage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Return low-level information on Docker objects
"docker inspect" requires at least 1 argument.
See 'docker inspect --help'.

expected

container1 45GB
..
..

shellter
  • 36,525
  • 7
  • 83
  • 90
AhmFM
  • 1,552
  • 3
  • 23
  • 53
  • 1
    All of the `$(command)` and `$variable` substitutions are being expanded by the local shell before the commands are sent to the remote computer. I'd send the commands as a here-document with a quoted delimiter instead of a double-quoted string. See [this similar question](https://stackoverflow.com/questions/53493901/why-does-this-shell-script-work-for-one-instance-and-not-the-other) and its answers. – Gordon Davisson Jun 13 '20 at 00:16

1 Answers1

1

Is better send a file with script content and run. Something like:

Copy script

for a in {server1,server2,serverN}; 
do 
    scp your_script.sh root@$a:/path/to/your_script.sh 
done

Exec script

for a in {server1,server2,serverN}; 
do 
    ssh root@$a "sh /path/to/your_script.sh par1 par2 parn"; 
done