1

Is there any way I can combine the following commands into one command? I do not want to login in each time for each command.

sshpass -p 'somepwd' ssh user@server "mkdir -p /home/user/test"
sshpass -p 'somepwd' scp file.sh user@server:/home/user/test
sshpass -p 'somepwd' scp /test/somefile.txt user@server:/home/user/test
sshpass -p 'somepwd' ssh user@server -C "cd /home/user/test;./file.sh"

I did check the answer for combing multiple commands when using ssh and scp; Based on that I would still need 3 logins, one for first ssh and mkdir, one for scp and one for ssh and running the shell script.

Is there a better solution?

4
  • 3
    Have you tried key based authentication method for ssh ? If not, try searching ssh-keygen or do a man ssh-keygen. Commented Dec 26, 2017 at 22:35
  • 2
    You can use rsync to copy multiple files, and it will automatically create directories as needed. So that allows you to combine the first 3 commands. Commented Dec 26, 2017 at 22:38
  • 2
    ControlMaster is your friend -- you can have a single connection used for multiple commands. Commented Dec 27, 2017 at 0:11
  • 1
    (see en.wikibooks.org/wiki/OpenSSH/Cookbook/Multiplexing for an intro) Commented Dec 27, 2017 at 0:17

2 Answers 2

2
  1. Use public/private keys instead of password authentication. Not only will this simplify the use of ssh, it is much more secure, especially after you disallow password authentication on the server you are connecting to. Using password authentication means you will get hacked, or your server has already been compromised and you don't know it yet. The rest of this answer assumes you have set up public/private keys.

  2. I see you have files in /test. Don't put your work in the root directory, this invites security issues. Instead, work in your home directory unless you are experienced with setting up permissions properly.

  3. Because file.sh is in your current directory (whatever that is) and you want a file from /test/ you cannot use rsync. rsync would be a good choice if all your files lived in the same directory.

Here is what we are left with; I have not messed with the location of /test/ because I don't know enough about the task:

ssh user@server "mkdir -p /home/user/test"
scp file.sh user@server:/home/user/test
scp /test/somefile.txt user@server:/home/user/test
ssh user@server -C "cd /home/user/test;./file.sh"
Sign up to request clarification or add additional context in comments.

1 Comment

The only thing I miss on this answer is the ControlMaster.
1

With GNU tar and ssh:

tar -c file.sh test/somefile.txt | sshpass -p 'somepwd' ssh user@server -C "tar -C / --transform 's|test/||;s|^|/home/user/test/|' --show-transformed-names -xv; cd /home/user/test; ./file.sh"

For more secure methods to pass the password with sshpass, see man sshpass.

1 Comment

Nice use of tar! How is authentication accomplished?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.