How can I automate the entering of password in Perl?
code:
my $makecmd .= system "ssh remotehost;";
system( "$makecmd" );
output:
Enter passphrase for key '~/.ssh/id_rsa':
How can I automate the entering of password in Perl?
code:
my $makecmd .= system "ssh remotehost;";
system( "$makecmd" );
output:
Enter passphrase for key '~/.ssh/id_rsa':
Net::OpenSSH supports keys with pass-phrases:
In any case, including the pass-phrase in the scripts defeats its purpose, mostly.
You could use Expect to do this. From this page: http://metacpan.org/pod/Net::SSH::Expect
Using passpharse:
Using password:
You can use an SSH agent to store the passphrase in memory. Although this approach is more cumbersome than using an unencrypted key, it is slightly more secure. There is an excellent comparison of the two methods in the O'Reilly book SSH, The Secure Shell: The Definitive Guide under chapter 11.1, Unattended SSH: Batch or cron Jobs.
Using an unencrypted key
The big advantage of using an unencrypted (passphrase-less) key is ease of configuration. To generate a key with an empty passphrase or to set the passphrase to nothing on an existing key, run
And that's it, no more configuration required. The big drawback to this is that your private key is now sitting on your filesystem in plain text.
Using an agent
The configuration process for an SSH agent is more involved and depends on which agent you use. Chapter 6.3 of the O'Reilly book, SSH Agents, and the IBM developerWorks article Getting started with SSH security and configuration describe how to configure
ssh-agent
, the default agent included with OpenSSH. The archlinux wiki page on SSH Keys also describes other agents like GNOME Keyring and pam_ssh.Let's look at the set-up process for
ssh-agent
. When you run the commandit not only starts the agent, but also spits out shell commands for setting some environment variables. In a Bourne-style shell, the output looks like this:
These environment variables tell your shell how to access the agent. Any scripts that use the agent will need these environment variables to be set. You can save the shell commands to a file for later use when you first invoke the agent:
Next you need to add your private key to the agent:
Finally, you need to make sure that the appropriate environment variables are set when your Perl script is invoked. One way to do this would be to write a wrapper script:
As long as the agent is running, your script can use the private key without having to enter the passphrase. Note that if only one uid will be using the agent, it would be easiest to start the agent under that uid:
One drawback to using an agent is that you will have to manually restart the agent and re-enter your passphrase on server reboot. This is the price you pay for the (arguably marginal) additional security you get from using an encrypted key.