SSH Loop brakes after 1st connection


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SSH Loop brakes after 1st connection
# 1  
Old 12-14-2011
SSH Loop brakes after 1st connection

I am trying to come up with a loop to connect to multiple servers and tun the same command on each one of them.

The number of servers will be typed in a single line separated with commas:

ex.
Code:
 ./test.ksh server1,server2,server3,.........

This is the code i've come up with till now:

Code:
list=$1
echo ${list} | tr ',' '\n' | while read server;
do
        echo "Server: ${server}"
        echo
        ssh ${server} "hostname"
done

Here i am trying just to run the "hostname" command on each server.

But after it runs in the first server, it exits the loop.

can someone help me?
# 2  
Old 12-14-2011
Use:

Code:
ssh -n ${server} "hostname"

or

Code:
ssh ${server} "hostname" </dev/null

This User Gave Thanks to dude2cool For This Post:
# 3  
Old 12-14-2011
Quote:
Originally Posted by lordseiya

Code:
 ./test.ksh server1,server2,server3,.........

This is the code i've come up with till now:

Code:
list=$1

Your problem is on list, you are only using the arg1
See:
Code:
soy@machine: temporal > cat test.sh
#!/bin/ksh
echo $1
echo $@

soy@machine: temporal > ./test.sh host1 host2 host3 host4
host1
host1 host2 host3 host4

# 4  
Old 12-14-2011
Quote:
Originally Posted by maya_style
Your problem is on list, you are only using the arg1
See:
Code:
soy@machine: temporal > cat test.sh
#!/bin/ksh
echo $1
echo $@

soy@machine: temporal > ./test.sh host1 host2 host3 host4
host1
host1 host2 host3 host4

No, the problem is as Dude2Cool pointed out. Have a closer look at the original post, the hostnames are separated with a comma which presents the entire list as $1. Your exampe does not use commas.

The ssh command will read from stdin unless the -n option is given or /dev/null is redirected into the command. The result is that the first name is read by the while, and ssh reads the remainder during the first execution.


A suggestion if you are using bash or ksh:
Code:
for host in ${1//,/ }
do
   echo "running on $host"
   ssh -n $host hostname
done

# 5  
Old 12-14-2011
Try this one,

Code:
for hostlist in `cat inputfile`
do
   ssh username@${hostlist} "command to execute"
done

# 6  
Old 12-15-2011
Thnxs This helped me out

Quote:
Originally Posted by dude2cool
Use:

Code:
ssh -n ${server} "hostname"

or

Code:
ssh ${server} "hostname" </dev/null

Bout codes worked really well.

Can you just tell me what is the difference between them?
# 7  
Old 12-15-2011
The -n option causes ssh to not read from stdin (we assume by closing it, but it doesn't really matter). Redirecting /dev/null into stdin causes an end of file on ssh's first read. In both cases, ssh does not read the input from the loop and all is well.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Ssh script to validate ssh connection to multiple serves with status

Hi, I want to validate ssh connection one after one for multiple servers..... password less keys already setup but now i want to validate if ssh is working fine or not... I have .sh script like below and i have servers.txt contains all the list of servers #/bin/bash for host in $(cat... (3 Replies)
Discussion started by: sreeram4
3 Replies

2. BSD

Connection SSH to remote by ssh

Hello guys! I am setting up a script to access a unix remote server. My problem is that when I put the ssh line "my host", the script does not wait for the server response asking for the password to execute the line in which I put the password, that is, I need to put a form in which script has a... (1 Reply)
Discussion started by: aroucasp
1 Replies

3. UNIX for Advanced & Expert Users

Connection-less using ssh

Hi! I know its a recurring problem, but I am failing to sort this out, I have two servers ( A and B), in which I am able to connect without having to put password from server B to server A, but the connect from server A to server B. takes 7 minutes to establish??? on Server A, I have the... (7 Replies)
Discussion started by: fretagi
7 Replies

4. UNIX for Advanced & Expert Users

How keep running a program n an another computer via a connection ssh when the connection is closed?

Hi everybody, I am running a program on a supercomputer via my personal computer through a ssh connection. My program take more than a day to run, so when I left work with my PC I stop the connection with the supercomputer and the program stop. I am wondering if someone know how I can manage... (2 Replies)
Discussion started by: TomTomGre
2 Replies

5. Red Hat

Ssh connection

hi, I have ssh connection between two servers for a functional Id for SFTP purpose. I aim is to setup this for is only work when below command is used by a .ksh script. ssh userid@servername:/directory Unfortunately users who have access to functional id are manually using above command... (2 Replies)
Discussion started by: maddy26615
2 Replies

6. Cybersecurity

ssh connection without password

The subject has been outlined in many articles, yet I can not establish a password-less ssh connection. Below I show what I did and then I include ssh debug info, maybe someone would be able to point out something I am not doing right. My setup: two SCO 5.0.7 boxes on a private lan, user... (6 Replies)
Discussion started by: migurus
6 Replies

7. UNIX for Dummies Questions & Answers

ssh connection

Hi @ all! I've a problem with a ssh-connection. I want to establish a ssh-connection between an AIX-System and an SunOS-System without a password. The Users are different one's. Command : user1@server1 /home/user1 > ssh user2@server2 Is it possible? Greetings olli-h (1 Reply)
Discussion started by: olli-h
1 Replies

8. UNIX for Advanced & Expert Users

ssh connection

pls how do i connect to my freebsd server via ssh from a windows client?? I have sshd running on d freebsd server. (9 Replies)
Discussion started by: lealyz
9 Replies
Login or Register to Ask a Question