Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Enterprise Linux Forums > Linux - Enterprise
User Name
Linux - Enterprise This forum is for all items relating to using Linux in the Enterprise.


  Search this Thread
Old 10-14-2013, 06:58 AM   #1
LQ Newbie
Registered: Nov 2012
Posts: 21

Rep: Reputation: Disabled
Single command to run multiple commands on ssh

Hello Team,

I have an requirement to run multiple commands over ssh.
1.I need to connect one server using ssh
2.Then i need to run sftp username@ip
3.Then I want to download single *.zip file to local dir and then i want to scp fetch that file from remote server to my local machine.

i know the individual commands but i need to run with single or couple commands
I am new to scripting kindly help me
Old 10-14-2013, 07:07 AM   #2
Senior Member
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Okay first and foremost we have to thinkabout how we get the login to succed if run from script. If you want to use a password take a look into expect.
Else setup passwordless ssh login with private public key pair. There are more than enough tuts on the net for this.

For the actual scripting you can pass a command to be executed at the remote machine to ssh. If you want to run multiple commands you can use either a script on the remote machine or use ; to tell the shell that this commands end. After the ; comes another command to be run.

So the script could look like this
ssh sshusername@sshserver "sftp-binary sftpuser@sftpserver remote_filename localfilename
scp sshusername@sshserver localfilename targetfilename
First line is a magic shebang that tells the shell which program it is to use to interpret the rest of the content.
second line connects to sshserver with the user sshusername and runs the comman sftp-binary to download the file from the sftpserver to the sshserver.
Next line uses scp to copy file from sshserver to your local machine.

Check the bash scripting guide on to get a grip of shell scripting.
1 members found this post helpful.
Old 10-14-2013, 07:16 AM   #3
LQ Newbie
Registered: Nov 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thank you very much sir
Old 10-14-2013, 07:35 AM   #4
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
You can run more than one command over an SSH connection by separating them with semi-colons:

ssh -i/path/to/key/file user@host 'command1; command2; commamd3'
scp is generally easier to use non-interactively than sftp, but you can use a "here document" in a script:

sftp -i/path/to/key/file user@host <<-END
You will probably also want to set up a public/private key pair and use these to authenticate with the server (rather than a password). Run ssh-keygen -b2048 to generate these (don't enter a password), then transfer the public key file (ending ".pub") to the server. Append the contents of this to /home/user/.ssh/authorized_keys (or create this file, if it doesn't exist) and you should now be able to connect without entering the password by passing the location of the other (private key) file to ssh using the -i parameter. Make sure the private key file is stored securely (it should only ever be readable by your user).
1 members found this post helpful.
Old 10-14-2013, 07:37 AM   #5
LQ Newbie
Registered: Nov 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
zhjim Sir while executing it showing sftp-binary command not found can u please help me
Old 10-14-2013, 07:40 AM   #6
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,275

Rep: Reputation: 5463Reputation: 5463Reputation: 5463Reputation: 5463Reputation: 5463Reputation: 5463Reputation: 5463Reputation: 5463Reputation: 5463Reputation: 5463Reputation: 5463
sftp-binary should be replaced by your actual sftp executable (with full path)


sftp, ssh

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
ftp command line client - run multiple ftp commands in one line dlugasx Linux - Server 1 09-13-2012 06:24 AM
How to run multiple X server in single terminal in linux greensuman Linux - Software 3 12-12-2010 07:34 AM
How to run multiple process in single command prompt babu198649 Linux - Newbie 7 10-12-2007 08:27 AM
using multiple -exec commands in a single find command? Bluemilk Linux - Newbie 4 03-19-2006 10:46 AM
executing multiple commands by ssh jpan Linux - General 1 10-22-2004 02:12 PM > Forums > Enterprise Linux Forums > Linux - Enterprise

All times are GMT -5. The time now is 04:16 AM.

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