AhmFM
AhmFM

Reputation: 1782

ssh to list of nodes in bash

I need to ssh several nodes in a list.
My code, when I run each command manually, works, but in a bash script, it does not.

sjc_nodes=$(kubectl get nodes -l nodeGroup=gpu --no-headers -o wide | awk '{print $1}' | sed -n -e 1,3p)

for sjc_node in "${sjc_nodes}"
do
echo $sjc_nodes;
ssh -F $HOME/.ssh/ssh_config userme@"$sjc_node.ssh-server.net" "nvidia-smi"
done;

Expected:

node1 node2 node3

nvidia-smi tests

Actual:

node1 node2 node3
usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl]
  [-m minttl] [-O length] [-P proxy_username] [-p source_port]
  [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout]
  [-X proxy_protocol] [-x proxy_address[:port]]       [destination] [port]
ssh_exchange_identification: Connection closed by remote host

My config file is below

cat ~/.ssh/ssh_config

HashKnownHosts no
 ServerAliveInterval 60
CanonicalizeHostname yes
CanonicalDomains server-ssh.net
ConnectionAttempts 100
ForwardAgent yes
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
user userme
Host *.server-ssh.net 10.*
  Compression yes
  ProxyCommand ssh -o "StrictHostKeyChecking=No" [email protected] nc %h %p

Upvotes: 1

Views: 987

Answers (1)

Barmar
Barmar

Reputation: 780879

Putting the variable in quotes makes it just one word, so you don't loop over all the nodes. Get rid of the quotes so that the shell will split the variable into words.

for sjc_node in $sjc_nodes

This is the exception to the general rule that you should always quote variables.

Upvotes: 2

Related Questions