[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"
[+] click to leave a comment [+]
>> SEND COMMENT <<