[LINUX] multiple ssh operations in scripts

z.B. upload a file to the server using scp and call a script on the server to handle the file(in this case: call lp to print it).

#!/usr/bin/sh

USER="username"
HOST="xx.xx.xx.xx"
FILE="$*"


scp $FILE $USER"@"$HOST":~/TEMP/TASK"
ssh $USER"@"$HOST "lp ~/TEMP/TASK"

the scripts will ask remote password twice since scp and ssh don’t share a SSH session.

Of course the best solution to this is using pub key.

Another solution to prevent multiple password input is to ask for password input with read -s from command line and use sshpass to provide password for scp and ssh. This wouldn’t leave the password in bash history.

So here is a modified script:

#!/usr/bin/sh

USER="xxx"
HOST="xx.xx.xx.xx"
FILE="$*"

echo -n "input password "
read -s password


sshpass -p $password scp $FILE $USER"@"$HOST":~/TEMP/TASK"
sshpass -p $password ssh $USER"@"$HOST "lp ~/TEMP/TASK"
edited 20.04.2024
created 24.05.2020
EOF
[+] click to leave a comment [+]
the comment system on this blog works via email. The button
below will generate a mailto: link based on this page's url 
and invoke your email client - please edit the comment there!

[optional] even better, encrypt the email with my public key

- don't modify the subject field
- specify a nickname, otherwise your comment will be shown as   
  anonymous
- your email address will not be disclosed
- you agree that the comment is to be made public.
- to take down a comment, send the request via email.

>> SEND COMMENT <<