Visit Our UNIX and Linux User Community


Ssh to an array of servers in a for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ssh to an array of servers in a for loop
# 1  
Old 09-11-2013
Ssh to an array of servers in a for loop

There are 4 remote hosts that I have stored in an array. A ssh trust has been created from the local host to each of the remote hosts.
I am trying to ssh to each of the servers in a for loop as shown below.

Code:
declare -a host
 host[${#host[@]}]="server1"
 host[${#host[@]}]="server2"
 host[${#host[@]}]="server3"
 host[${#host[@]}]="server4"

for host in "${host[@]}"
do
  ssh -qtt user@${host} "sh -s "${version}""  <  test.sh
done

test.sh is a file on the local server that I am running on the remote host after I ssh to the remote host. But it is terminating after the first iteration. If I use -n switch it prompts me for password in the second iteration.

Please can you guide me on this issue.

Thanks!

Last edited by Sree10; 09-12-2013 at 06:12 AM..
# 2  
Old 09-11-2013
You cannot double quote double quotes. You could escape them with \, or just use a single quote.

Code:
ssh -qtt user@${host} "sh -s '${version}'"  <  test.sh

# 3  
Old 09-11-2013
Code:
for host in "${host[@]}"
do
ssh -qtt ohsadm@${host} "sh -s '${version}'"  <  test_steps.sh
done

exits after the first iteration. host array has 4 servers. I want the ssh in the for loop to run through all the 4 servers.
# 4  
Old 09-11-2013
Hi,
Try with -n -o BatchMode=yes options:
-n for manage stdin
-o BatchMode=yes for no ask password

Regards.
# 5  
Old 09-11-2013
You do not want -n when running a script read from stdin! This is not the usual stdin problem.

Can you show your complete, unmodified script? I think something funny is going on here.
# 6  
Old 09-12-2013
I am able to iterate successfully through the array if I specify the commands as mentioned below.

Code:
for host in "${host[@]}"
do
  echo $host
  ssh -qtt user@${host} "cd /build && cp -p web.zip web.zip_${version} && rm -rf test && unzip /build/web.zip && cd /bin ";  done;

However, if I try to add the commands in a separate file called test.sh and stream it to ssh as shown below, it exits after the first iteration. Pressing Ctrl-C manually triggers the next iteration.

Code:
for host in "${host[@]}"
do
  ssh -qtt user@${host} "sh -s "${version}""  <  test.sh
done

# 7  
Old 09-12-2013
I repeat: You cannot put double quotes inside double quotes that way.

This is wrong:

Code:
"sh -s "${version}""

This would be more correct:

Code:
"sh -s '${version}'"

I just realized, however -- your test.sh probably uses the ${version} variable, which is why you are feeding it into sh, yes? You want it to carry over. But arguments to sh -s don't work that way, you don't get the names, just $1 $2 $3. Not to mention, you need -- if you want it to assume the following are arguments. Try this:

Code:
$ sh -s -- a b c
$ echo $1

a

$ echo $2

b

$ echo $3

c

$ exit
$


So I think you actually want:
Code:
ssh -qtt user@${host} "sh -s -- '${version}'"  <  test.sh

...and your script should be using $1 and not ${version} .

I suspect you can omit the -tt completely, too. I see no commands in there that actually need a terminal, and throwing one into the mix when not needed seldom helps.

Last edited by Corona688; 09-12-2013 at 01:25 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #325
Difficulty: Medium
Early RAM was not directly seated onto the motherboard so it could easily changed and upgraded.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find active SSH servers w/ ssh keys on LAN

Hi, I am trying to complete my bash script in order to find which SSH servers on LAN are still active with the ssh keys, but i am frozen at this step: #!/bin/bash # LAN SSH KEYS DISCOVERY SCRIPT </etc/passwd \ grep /bin/bash | cut -d: -f6 | sudo xargs -i -- sh -c ' && cat... (11 Replies)
Discussion started by: syrius
11 Replies

2. UNIX for Beginners Questions & Answers

ssh multiple servers

Hi folks. I'm pretty new to unix, while I'm learning a lot I'm finding bash scripting quite confusing. Im sure it's not really, my head just hasn't clicked with it. Anyway, I need a script to loop the ip addresses stored in a file and run a "pgrep <process>" and return the pid or some... (2 Replies)
Discussion started by: MuntyScrunt
2 Replies

3. UNIX for Dummies Questions & Answers

Ssh not working to one server from any of the servers

Hello, I tried ssh in debug mode and below is the debug snippet.ssh to a host is not working from any of the hosts No credentials cache found debug1: Miscellaneous failure No credentials cache found debug1: Next authentication method: publickey debug1: Offering RSA public key:... (7 Replies)
Discussion started by: Vishal_dba
7 Replies

4. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

5. Shell Programming and Scripting

Iterate array using loop over ssh

A simple script: #!/bin/bash test=test test1=(test1 test2 test3) echo ${test1 } ssh server 'echo '$test'; echo '${test1 }' ; echo '${test1}' ; for m in $(seq 1 $(echo '${test1 }' | tr " " "\n" | wc -l)); do echo $m ; echo '${test1}'; done'Here is the result: test1 test2 test3 testing... (5 Replies)
Discussion started by: mharald
5 Replies

6. IP Networking

Cant SSH Solaris servers

Hi all. Im working in a telco Environment and recently setup a new server. The other servers are a combination of Solaris + Linux machines. Using my new server , I can ping all other servers ( solaris + redhat linux ) but the issue lies where I try to ssh. I can only successfully ssh linux... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

7. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

8. UNIX for Dummies Questions & Answers

SSH into multiple linux servers

Hi All, Okay, I need help. I need to ssh in to multiple linux servers execute certain commands and get them to email and print on the screen when the script is being executed. So below is my script. Its not working :-(. #!/bin/bash #linux/UNIX box with ssh key based login... (7 Replies)
Discussion started by: xytiz
7 Replies

9. UNIX for Advanced & Expert Users

ssh: require passphrase from some servers but not others

We have a number of linux clusters running Oracle. The clusters require that all member nodes have promptless/passwordless login through ssh (ie. the keys were created with null passphrases). We also have a management server that is not a member of any cluster but routinely connects to nodes of all... (3 Replies)
Discussion started by: Squeakygoose
3 Replies

10. Shell Programming and Scripting

connecting servers using SSH - help needed

Hi all, I have a script written successfully and tuned by our members, now I am trying to execute this script from a taxi server. My aim is I want to connect to four servers via ssh where I want to ran a script or run some commands and write the output to a file in the Taxi server. I... (12 Replies)
Discussion started by: senthilkumar_ak
12 Replies

Featured Tech Videos