LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-10-2011, 09:19 AM   #1
angrymachine
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Rep: Reputation: Disabled
How to execute multiple commands via SSH in a script


I need to run several commands on a remote machine via ssh, but I want to do so in a script since there are many machines. Also, none of the commands require user feedback, so that simplifies it a bit.

Here is pseudo code of what I'm trying to do

ssh user@unix
command1
./script.sh
command2
command3
exit ssh

Any suggestions on how to do this?
 
Old 10-10-2011, 09:27 AM   #2
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
try write the script (e.g. "runthis.sh"):
Code:
#!/bin/sh
$(1st command to run)
$(second)
exit 0        <-- will make you logout/exit ssh
 
Old 10-10-2011, 09:27 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can use the -s option of bash to read the commands from standard input. Suppose you want to execute your local script test.sh on the remote machine:
Code:
ssh user@server 'bash -s' < test.sh
You can do this even inside a script using a here document to pass the commands as standard input:
Code:
ssh user@server 'bash -s' << EOF
  command_one
  command_two
  command_three
EOF
Hope this helps.
 
1 members found this post helpful.
Old 10-10-2011, 09:28 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello and Welcome to LinuxQuestions,

Did you have a look at the man page for ssh? Or do a search on the Internet? I'm sure either of those would have pointed you to something like this:
Code:
ssh user@host 'command1; command2; command3'
or you could put a rc file in ~/.ssh/rc on the remote server (look it up in the man page) to execute commands when logging in which would execute the commands before the shell is started. If either of the above doesn't work for you, then please be more specific on what you want to do.

Looking forward to your participation in the forums. Have fun with Linux.

Kind regards,

Eric
 
1 members found this post helpful.
Old 10-12-2011, 10:05 AM   #5
angrymachine
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks for all the replies. This was just what I was looking for.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running multiple commands remotely via SSH in a script gimpy530 Linux - General 4 12-19-2009 10:22 PM
How to execute multiple commands with find and -exec... tomstratton Linux - General 6 05-26-2009 03:26 AM
can ssh execute multiple commands at the same time? Singist Linux - Networking 2 04-04-2006 09:14 AM
LXer: Execute commands simultaneously on multiple servers LXer Syndicated Linux News 0 02-20-2006 11:31 AM
execute multiple ssh remote commands tom221 Linux - Newbie 2 01-28-2005 01:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration