How can I solve this problem? I have a bash script (under Ubuntu Server), doing several SSH connections, executes some remote transfers, like rsync multiple times.
My problem is, that I would like to avoid entering password multiple times. But I don't want to use SSH public key exchange, because if anybody has the key, will be able to connect the remote computer.
I would like the following:
- SSH should ask password when I start the bash script
- reuse password, so don't ask password again until timeout
- when timeout is over, ask password again.
Something similar, how sudo is working, just with ssh.
Any idea how to solve this?
EDIT1:
I found a new SSH feature named ssh multiplexing. Maybe using this I can reach the goal I want.
https://unix.stackexchange.com/questions/50508/reusing-ssh-session-for-repeated-rsync-commands
Would this work?
(Reposting my comment as an answer per request from klor).
It's not what you want to hear, but this is what key-based authentication is for. So long as you put a passphrase on your private key, it's no less secure than password authentication.
You can use
ssh-agent
to avoid needing to enter the passphrase every time, and the-t
option tossh-agent
will give you the timeout behavior you're after.Your script will need some logic to determine when the timeout occurs. One way would be to run
ssh
andrsync
with-o BatchMode=yes
, which will prevent interactive authentication methods, so if the key is no longer usable,ssh
will exit instead of prompting for a password. You can use the exit code to determine if you need to runssh-add
again;$?
should be set to255
in this case.You'll still need to work out how to feed the passphrase to
ssh-add
, because it doesn't provide a way to accept it programmatically. Unless your script will prompt you to enter it by hand, you'll probably need to useexpect
for that part, and that will mean hard-coding the passphrase somewhere.It seems I found the perfect solution for my needs:
Reusing ssh session for repeated rsync commands
This solution asks password only once, then doing rsync 3 times without asking password again. Each rsync reuses the SSH connection using SSH multiplexing.
Could be possible to change the SSH config, and store the SSH multiplexing settings, but using this solution there is no need to change the server config, the script works as is.
If you're using a keyfile, you can use the
AddKeyToAgent
option (OpenSSH>=7.2).So your script might look like this.
Implementation of James Sneeringer's comment as a profile script.
Then you can run the command
autossh myserver
and it will auto-initialize an agent if one isn't available. The alias is to prevent accidentally calling an interactive function from a script.Problematically, on systems that don't automatically start an agent with the WM session, this may litter your machine with ssh-agent processes, you may need to
ssh-agent -k
to end it, orkillall ssh-agent
. Some additional scripting may be required if you want to share a single ssh-agent across an entire login session or across multiple process sessions.I'd normally tell you to try
expect
, but in this case I think it's beter to use sshpass, it is very easy to install and to use, here I post some use casessshpass -p<pass> ssh <args> sshpass -pfoobar ssh -o StrictHostKeyChecking=no user@host command_to_run
have fun :)
EDIT 1
on a script