views:

114

answers:

5

I am writing a Perl script that needs to transfer files between computers using scp. I know about public key authentication, but I need the script to be fully automated, so I can't visit the machines to set up the keys before the script is run.

Is there any way to either pass the password to scp from the Perl script or set up the keys from within the perl script?

This script will be run as part of a build script that also reimages the hard drive I need to run the script on. So I can't be there to set up keys every time the project is built.

A: 

Just create keys that don't have passwords.

Alex Howansky
how is that possible?
djondal
@djondal: run `ssh-keygen` and read the output :P
Daenyth
+1  A: 

You could use the Perl Expect module, see an example at Well House consultants' forum.

Its documentation has a telnet example which is easily changed for SSH.

Net::SSH::Expect is another Perl module to do exactly what you want. I haven't used this one previously, though.

amadain
+1  A: 

Make use of ssh-agent. And if you use Gnome, Gnome Keyring SSH Agent is wonderful.

djondal
+3  A: 

You can use Net::SSH::Perl. Below is the sample code you can use.

#!/usr/bin/perl -w
use strict;
use Net::SSH::Perl
my $cmd = 'command';
my $ssh = Net::SSH::Perl->new("hostname", debug=>0);
$ssh->login("username","password");
my ($stdout,$stderr,$exit) = $ssh->cmd("$cmd");
print $stdout;

This code will simply run the given 'command' on remote machine and give you the output on your local system. So, to instead of scp, you can use this script with command 'cat' to capture the output of 'cat filename' on local system and redirect the output in file on local system.

Hope this helps.

Space
A: 

You can use a SSH-Key (without a password).

Sebastian Thiele