Executing multiple ssh commands inside a shell simultaneously


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Executing multiple ssh commands inside a shell simultaneously
# 1  
Old 12-18-2012
Executing multiple ssh commands inside a shell simultaneously

I would like to execute a commands in four different servers through ssh at a single instance(simultaneously).

Below are the details with examples,

Code:
ssh user1@server1 "grep xxxx logs"

ssh user1@server2 "grep xxxx logs"

ssh user1@server3 "grep xxxx logs"

Each statement will take some time to complete, and if i give like the above inside an shell, only when ssh at server1 is complete it will move to the next ssh.

For and while loop also takes one by one, only when one ssh is done, it picks the next ssh.

But, i would like to execute these three ssh commands at the same instance inside the shell and wait till all the three ssh gets completed and come out of the shell.

Could you please help me out?
# 2  
Old 12-18-2012
How about:-
Code:
ssh user1@server1 "grep xxxx logs" > server1.log &
ssh user1@server2 "grep xxxx logs" > server2.log &
ssh user1@server3 "grep xxxx logs" > server3.log &
wait

This should fire them off and wait for all you background processes to finish before returning control. You can then read the log files to see what you got.

Be careful, as it will wait for all background processes, so if you were to set up a quick check from the same session and did a
Code:
touch server1.log
tail -f server1.log &

... and then ran your script, it would wait until your tail had completed.


I hope that this is useful.

Robin
Liverpool/Blackburn
UK
# 3  
Old 01-11-2013
Thanks Robin. I missed out your reply.

The above logic of moving the ssh commands to back end works fine in case of executing single command , but i'm facing issue when doing multiple commands like below.

Code:
ssh user1@server1 <<EOF
"grep xxxx logs" > server1.log 
cmd1;
cmd2;
EOF&

cmd;

ssh user1@server2 <<SOF
"grep xxxx logs" > server2.log
cmd1;
cmd2;
SOF&

cmd;

ssh user1@server3 <<DOF
"grep xxxx logs" > server3.log
cmd1;
cmd2;
DOF&

cmd;
wait


If i try executing the above code, i'm getting the below errors.

Code:
-bash: line 5: EOF: command not found

-bash: line 5: SOF: command not found

-bash: line 5: DOF: command not found

TERM environment variable not set.

Could you please help me with this.
# 4  
Old 01-11-2013
here-document syntax is a little confusing. All the commands go directly after the first, including redirection and backgrounding, not on the EOF.

Code:
ssh user1@server1 <<EOF > server1.log &
grep xxxx logs
cmd1
cmd2
EOF

# 5  
Old 01-17-2013
Thanks Corona688..

This seems to work, but still facing some issues..

1. Not able to access the variables from the main script within the EOF tags
2. When using redirection operation within the EOF tags, i'm facing error with the file or folder doesn't exists.

When i tried the redirection operation with simple ssh, still its not working. Can you advise how we can use the file redirection operation within EOF tags.

Thanks in Advance.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Executing multiple commands in a file at same time

Hi Am having file.ksh as below wc -l file1.txt wc -l file2.txt wc -l file3.txt wc -l file4.txt i want all the commands in this file to execute in same time please help Thanks in advance (1 Reply)
Discussion started by: ragu.selvaraj
1 Replies

2. Shell Programming and Scripting

Executing remote commands via ssh

Hi, I'm tryin to write a script that will collect information about a remote servers, put them into variables and print them to screen. # /usr/bin/bash ls $1 > /dev/null 2>/dev/null if then echo "$1 is file" for server in $(cat $1) do # echo $server ... (5 Replies)
Discussion started by: moshesa
5 Replies

3. Shell Programming and Scripting

executing commands in remote server using ssh

I have some commands which need to be executed in remote machine. I have Linux Server from where I need to connect to Solaris server using ssh and then declare some variable over there and run some commands. I don't want to call a script which is present in Solaris server from Linux server... (7 Replies)
Discussion started by: maitree
7 Replies

4. Shell Programming and Scripting

Need help with executing multiple commands in remote machine

Hi, I work on a jumpserver and I wrote a script to transfer a file from source server to destination server. #!/bin/ksh echo "\nEnter the file name:\n" read name echo "\nSelect the Source server\n" echo "1. ODS PROD " echo "2. ODS DROPBOX" echo "3. ODS STE" echo "4. ODS STE DROPBOX"... (6 Replies)
Discussion started by: ajayakunuri
6 Replies

5. Solaris

Help with executing multiple remote commands after multiple hops

Hi SSHers, I have embedded this below code in my shell script.. /usr/bin/ssh -t $USER@$SERVER1 /usr/bin/ssh $USER2@S$SERVER2 echo uptime:`/opt/OV/bin/snmpget -r 0 -t 60 $nodeName system.3.0 | cut -d: -f3-5` SSH to both these servers are public-key authenticated, so things run... (13 Replies)
Discussion started by: LinuxUser2008
13 Replies

6. UNIX for Dummies Questions & Answers

how to stay in remote shell after executing commands in ssh?

the ssh calling convention: ssh <server> If I put commands in the section, ssh will execute them immediately after logging in and return to local shell. I want to stay in the remote shell after executing these commands. How can I achieve this? Thanks for all. (1 Reply)
Discussion started by: hplonlien
1 Replies

7. Shell Programming and Scripting

executing commands over ssh

Hi I am trying to send a command over ssh with a parameter but the shell fails to expand the command properly any ideas what am i doing wrong with this. This is ssh on AIX for i in 71 72 73 74 75 do for server in server1 server2 do somestr="Some String" echo "$server... (3 Replies)
Discussion started by: xiamin
3 Replies

8. Shell Programming and Scripting

Automating executing commands on multiple tapes and changing (loading) them

The thing that we need to be able to do is: - Scanning tapes from failed backup jobs with bscan (a bacula command) - loading the next tape with the mtx command Bscan does unload tapes but does not load them. Bscan comes with this message when it's done with a tape: Mount Volume... (0 Replies)
Discussion started by: Tr00tY
0 Replies

9. Programming

How to know there are multiple instances inside application itself,not through ssh

hi, As you know, in Windows Programming, in WinMain method you will give n argument which you can know if this is another instance of the application or just the fresh execution. How this functionality can be achieved in Linux application? Most of suggestions i have seen here in threads are... (5 Replies)
Discussion started by: Sedighzadeh
5 Replies

10. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies
Login or Register to Ask a Question