How to transfer file using scp without password with Perl script?

I am writing a Perl script that should 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 configure the keys before running the script.

Is there a way to pass the password to scp from a Perl script or configure the keys from a perl script?

This script will be run as part of the script assembly, which also recreates the hard drive needed to run the script. Therefore, I cannot be there to configure the keys every time the project is built.

+4
source share
6 answers

You can use the Perl Expect module, see the example on the Well House consultants forum .

There is a telnet example in his documentation that is easy to change for SSH.

Net :: SSH :: Expect is another Perl module that will do exactly what you want. However, I have not used this one before.

+1
source

You can use Net :: SSH :: Perl . Below is an example of code that 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 the remote computer and give you the result on your local system. So, instead of scp, you can use this script with the "cat" command to capture the output of "cat filename" on the local system and redirect the output to a file on the local system.

Hope this helps.

+3
source

Use ssh-agent. And if you use Gnome, the Gnome Keyring SSH Agent is great.

+2
source

Just create keys that don't have passwords.

0
source

You can use SSH-Key (without password).

0
source
 #!/usr/bin/perl -w ###################################################### # # # # # Script to send files to server # # Author: Jurison Murati # # # ###################################################### use strict; use Net::SCP::Expect; use File::Copy; use IO::Compress::Gzip qw(gzip $GzipError); my $host = "192.168.0.1"; my $user = "user"; my $pwd = "password"; my $RemoteDir = </nodes>; my $file; my $displaydate= `date +'%Y%m%d%H%M%S'`; print "Filloi dergimi date $displaydate\n"; my $scp = Net::SCP::Expect->new(host=>$host,user=>$user,password=>$pwd,recursive=>1); my $dir = '/arch'; opendir(DIR, $dir) or die $!; while (my $file = readdir(DIR)) { next if ($file =~ m/^\./); $scp->scp("$dir/$file","$RemoteDir") or die $scp->{errstr}; print "file $dir/$file moved on date $displaydate\n"; } exit 0; 
0
source

All Articles