-1

For practising reasons Im trying the following script :

#!/bin/bash

NUM_OF_SERVERS=(server1 server2)
SSH_USERNAME=root
SSH_PASSWORD=password
version=$(cat /etc/redhat-release)


for HOST in ${NUM_OF_SERVERS[@]};
  do /usr/bin/sshpass -p $SSH_PASSWORD ssh $SSH_USERNAME@$HOST echo "$version"

echo "The software version for ${NUM_OF_SERVERS[0]} is $version"
echo "The software version for ${NUM_OF_SERVERS[1]} is $version"

 done

and the output :

bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `echo CentOS Linux release 7.4.1708 (Core) '
The software version for server1 is CentOS Linux release 7.4.1708 (Core)
The software version for server2 is CentOS Linux release 7.4.1708 (Core)
The software version for server1 is CentOS Linux release 7.4.1708 (Core)
The software version for server2 is CentOS Linux release 7.4.1708 (Core)

Why this syntax error arises and why the loop occurs 4 times instead of 2 ?

Any help would be appreciated.

VorX
  • 435
  • 2
  • 7
  • 9
  • 4 times, because u exec 2 line every loop (2 line * 2 loop = 4) – beliy Nov 27 '17 at 12:56
  • try `for HOST in ${NUM_OF_SERVERS[@]}; do /usr/bin/sshpass -p $SSH_PASSWORD ssh $SSH_USERNAME@$HOST echo "$version" echo "The software version for ${HOST} is $version" done` – beliy Nov 27 '17 at 12:57
  • also try `/usr/bin/sshpass -p $SSH_PASSWORD ssh $SSH_USERNAME@$HOST "echo \"$version\""` – beliy Nov 27 '17 at 13:06
  • `for host in server1 server2; do sshpass -p "$SSH_PASSWORD" ssh "$SSH_USERNAME"@"$host" cat /etc/redhat-release; done` – tripleee Nov 27 '17 at 14:10

1 Answers1

0

This loop only runs number of members of array times. It may look like it runs more times, because of the 2 echoes.

I believe you should take care more about the identation.

for HOST in ${NUM_OF_SERVERS[@]};
  do /usr/bin/sshpass -p $SSH_PASSWORD ssh $SSH_USERNAME@$HOST echo "$version"

    echo "The software version for ${NUM_OF_SERVERS[0]} is $version"
    echo "The software version for ${NUM_OF_SERVERS[1]} is $version"

 done

If you really want to print information about version, there are other issues as well.

for HOST in ${NUM_OF_SERVERS[@]}; do
    v=`/usr/bin/sshpass -p $SSH_PASSWORD ssh $SSH_USERNAME@$HOST "uname -a"`
    echo "The software version for $HOST is $v"
 done

since the two echoes are totally independent from the current values iterated by the loop.

ntj
  • 171
  • 12
  • This is utterly pointless. The string `$version` is defined locally. Why would you `ssh` to a remote host to echo back this now static string to you? – tripleee Nov 27 '17 at 14:08
  • Also the quoting around the array is wrong, though it's probably harmless here. See also https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable – tripleee Nov 27 '17 at 14:09
  • You're right about both things, however I believe it's safe to omit the quotes if you are confident. `$version` is changed to `uname -a` for the reason changing the local `$version`. Could be `cat /etc/redhat-release` as well, but it's just childplay to put the correct command after ssh. – ntj Nov 27 '17 at 14:19