Reputation: 4342
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?
Upvotes: 1
Views: 2532
Reputation: 88583
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
.
Upvotes: 1
Reputation: 8403
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.
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.
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"
Upvotes: 2