scp file1 file2 remote01:/path/to/somewhere/ scp file1 file2 remote01:/path/to/another/The problem is I need to type the password for remote01 twice. I had never managed to find some clever scp syntax to allow specifying two destinations, and a post on the TLUG mailing list confirmed that. But what I did learn from TLUG is that there is something called ssh-agent that can store passphrases for key pairs; this plugged a gap in my knowledge. There are three ways to login in via ssh/scp:
- Give your password
- Make a keypair with no passphrase
- Make a keypair with a passphrase
But it turns out there is this program called ssh-agent that remembers passphrases for you. And I found it is already running in the background on Ubuntu.
Enough chat, let's look at the solution. First, I created a one-off keypair for this script, on my machine, using something like this:
cd ~/.ssh ssh-keygen -t dsa -C me@example.com -f key_for_remote01 chmod 600 key_for_remote01* scp -p key_for_remote01.pub remote01:~When generating the keypair give a reasonably secure passphrase (you will have to type it in each time, and it is only of use to people in possession of key_for_remote1, so no need for a 20-random-character monster; I believe it is perfectly fine for it to be the same as your normal ssh login password for remote01).
Then log in to remote01 and append key_for_remote01.pub to ~/.ssh/authorized_keys. If that file does not exist then you can just rename key_for_remote01.pub to authorized_keys and move it into ~/.ssh/
(By the way, there is no need to put your private half of the keypair in ~/.ssh/ but that seems as good as place as anywhere else.)
Now, I modified my script as follows:
ssh-add -t 120 ~/.ssh/key_for_remote01 scp -i ~/.ssh/key_for_remote01 file1 file2 remote01:/path/to/somewhere/ scp -i ~/.ssh/key_for_remote01 file1 file2 remote01:/path/to/another/ ssh-add -d ~/.ssh/key_for_remote01
What happens is the ssh-add line will ask you for your passphrase. The two scp lines then work automatically. Finally the
ssh-add -d
stops it caching your passphrase (forcing you to type your passphrase each time you run this script).The
-t 120
parameter says the passphrase will expire after two minutes. This is just in case the batch file doesn't complete and so does not get chance to run ssh-add -d
.Note: you can use that same key pair for other machines. Basically anywhere you put the *.pub half of the key pair will let you login. And you can login from anywhere you have the private half of the key pair.
Note: the timeout/deletion code above is deliberate for this application, but you don't have to do it this way. By allowing it to cache it permanently you would only be prompted for your passphrase once, and then all future ssh and scp logins would be automatic. They will be cleared when you log-out of gnome (on ubuntu, at least) or shutdown your machine.
Note: If you don't want to specify -i each time you use ssh/scp then you can add an entry to your ~/.ssh/config file, like this:
Host remote01 Hostname 10.1.2.3 Port 22 IdentityFile ~/.ssh/key_for_remote01
Note: I used -C with my real email address. This is put in the public key, and I wanted the administrator of remote01 to know who put the key there. Without -C it defaulted to "myusername@myhost". The administrator of remote01 knows nothing about my machine names so this seemed unreasonable and I decided to use -C. But the advantage of that default is it seems ssh-agent knows about that name and will prompt automatically the first time you try to use ssh/scp, which means there is no need to run ssh-add first. I have not worked out yet if ssh-agent can be told to know about my email address too.
No comments:
Post a Comment