[solved] Process ssh command in while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [solved] Process ssh command in while loop
# 1  
Old 02-13-2013
[solved] Process ssh command in while loop

I have a script that reads a file containing a list of server names. It's suppose to loop through the list of names and execute a command on the remote server using ssh. It processes the ssh command for the first server in the list and then exits. Here's the code:

Code:
#!/bin/bash

FILENAME=server_list.txt

cat $FILENAME | while read LINE
do
        echo "$LINE"

        # Run the Users.sh script on the remote server
        ssh root@$LINE '/usr/local/bin/Users.sh > /root/UserAccounts.txt'

done

When I comment out the ssh command the script processes as expected. Does ssh do something when it exits the first time that is maybe stopping the rest of the script from processing? I'd appreciate any suggestions.

---------- Post updated at 05:20 PM ---------- Previous update was at 04:28 PM ----------

I found the answer. Gotta use the -n option in the ssh command

Code:
ssh -n root@$LINE '/usr/local/bin/Users.sh > /root/UserAccounts.txt'

This User Gave Thanks to westmoreland For This Post:
# 2  
Old 02-13-2013
Thanks for posting the solution.
# 3  
Old 02-14-2013
Code:
while read LINE <&3
do
 echo "$LINE"
 ssh -nx root@"$LINE" '...'
done 3< $FILENAME

This not only avoids the "useless use of cat": the additional <&3 and 3< allow to even have the bare ssh command. The -n and -x are still there for efficiency reason.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop is causing ssh command to exit from script after first iteration.

I am trying to check multiple server's "uptime" in a loop over "ssh". When I execute multiple ssh commands with hard coded servernames script is executing fine. But when I pass server names using while loop, script is exiting after checking first server's status, why? # serverList... (8 Replies)
Discussion started by: kchinnam
8 Replies

2. UNIX for Dummies Questions & Answers

SSH Loop command each minute

Hello all :) I want to know how can i do to loop a command each minute for example ? I'm in SSH under Putty ;) Thanks you for your help :) (1 Reply)
Discussion started by: stevefigueras
1 Replies

3. Shell Programming and Scripting

[Solved] While loop error when add sqlplus command

Hi gurus, I hit a block when write the script. I need do while loop, in the loop I have code below. sqlplus -s abc/abc@abc <<EOF spool testwhile select * from dual; spool off exit; EOF when I run script with only this code, it works fine. if I add code as below: #!/bin/ksh ... (5 Replies)
Discussion started by: ken6503
5 Replies

4. Shell Programming and Scripting

[SOLVED] put dd | ssh command in backgound

Greetings, I have an issue that has baffled me. I have done many searches, read the man documentation, and I have yet to find a solution. I am trying to run the following command within a script to copy a file across servers: $(dd if="$FDIR" bs=1024 2> /dev/null | ssh "$(whoami)@$SERVER"... (5 Replies)
Discussion started by: unaligned
5 Replies

5. Shell Programming and Scripting

run command with ssh[solved]

Hi all, Is it possible to make this possible ? $ echo $SKY_HOME /var/sink/SKY $ echo $SKY_HOME /home/smily/SKY $ ssh root@xyz "echo $SKY_HOME" root@xyz 's password: ****** /home/smily/SKY wrong output I was expecting the output as /var/sink/SKY (3 Replies)
Discussion started by: linuxadmin
3 Replies

6. Shell Programming and Scripting

while loop stops after first iteration - remote ssh exit command problem?

I have written the following script to update some Debian boxes. #!/bin/bash mxg_hosts_file="/etc/mxg/ssh-hosts" while read line ; do mxg_host="$(echo ${line} | awk -F":" '{print $1}')" mxg_port="$(echo ${line} | awk -F":" '{print $2}')" echo "Connecting and Upgrading... (3 Replies)
Discussion started by: jelloir
3 Replies

7. Shell Programming and Scripting

[solved] Killing 3rd command in ssh chain

Hi All, Noob question here... How do I kill the 3rd command in this ssh chain effectively? # ssh -t -t 10.80.0.5 'ssh 10.80.0.6 | /var/tmp/some_script' The "/var/tmp/some_script" contains: ssh 10.80.0.81 'echo "Hello World!!!!" >> /tmp/sample.txt'The problem is that once the sample.txt... (2 Replies)
Discussion started by: NYG71
2 Replies

8. Shell Programming and Scripting

[SOLVED] for loop to process files

I need to process a dirtree containing ms office files such that each file is stored as a variable and also, just the file file stem. Why? They will be using as input and output parameters for another script. For example /path/to/second_script -i filename.docx -o filename Here's what I... (1 Reply)
Discussion started by: graysky
1 Replies

9. Shell Programming and Scripting

How to starting process as daemon using ssh command?

Hello, I need to run a command on remote Linux using the ssh command from my local machine. I am able to execute the command on remote machine using ssh but it's behaving strangely. The command is supposed to start a daemon process on remote linux box and the control should return back to me... (5 Replies)
Discussion started by: nitinshukla
5 Replies

10. Shell Programming and Scripting

Pb in while loop with ssh command

Hi I use the following command to check if my_base is active or not : active_db=`${LOCAL_BIN}/ssh -l ${my_user} ${my_service} "ps -ef | grep ora_smon | grep ${my_base} | sed -e \"s/ */ /g\" | cut -d'?' -f2 | cut -d' ' -f3 | cut -d'_' -f3"` When I use a file listing databases to check them... (1 Reply)
Discussion started by: madmat
1 Replies
Login or Register to Ask a Question