mantra2006 has asked for the wisdom of the Perl Monks concerning the following question:

hello

I was using the following script to connect to remote host and copy a file. The script actually copies the file. But when I run the script it asks me password for remote host. Is there a way to automate so that the script won't ask for password and everything copy when the script is run

use::warnings; use::strict; my $rmthost = 'backupserver'; my $rmtuser = 'anonymous'; my $rmtpath = 'tempdir'; my $lclpath = '/home/user/testdir'; my $lclglob = '*.txt'; my $flagfile = '.last_backup'; my $r_opts = "'ssh -l $rmthost $rmtuser'"; my $lclfiles = "$lclpath/$lclglob"; sub modtime { my $file = shift; my @stats = stat $file or return 0; return $stats[9]; } while (1) { my $timestamp = modtime("$lclpath/$flagfile"); my $run = grep {modtime($_) > $timestamp} glob $lclfiles; if ($run) { system "scp $r_opts $lclfiles $rmthost:$rmtpath"; open FLAG, ">$lclpath/$flagfile" or die; close FLAG or die; } sleep 60; }

20060724 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Considered by Hue-Bond: Mark as OT, since this isn't strictly a Perl question
Unconsidered by Arunbear: Keep: 24, Edit: 19, Reap: 2

Replies are listed 'Best First'.
Re: SCP connection
by Joost (Canon) on Jul 24, 2006 at 18:42 UTC
Re: SCP connection
by ikegami (Patriarch) on Jul 24, 2006 at 18:51 UTC

    What is

    use::warnings; use::strict;
    They don't do anything.
    use::warnings; # Returns the var "*warnings" in the pkg "use". use::strict; # Returns the var "*strict" in the pkg "use". print $abc; # "use strict" would give an error (not declared), # "use warnings" would give a warning (undefined), # but we get nothing.

    You want

    use warnings; use strict;
Re: SCP connection
by madbombX (Hermit) on Jul 24, 2006 at 18:53 UTC
    I would suggest that you check out the module from CPAN called Net::SCP. It should be able to do what you are looking to do.

    With regard to getting the password into the script. This isn't secure practice. A slightly more secure practice would be to generate a pwless ssh key and use that key to connect to the server.

    use Net::SCP qw(scp); my ($host, $username) = ("example.com", "eric"); $scp = Net::SCP->new( { "host"=>$hostname, "user"=>$username } ); for my $file (@files) { $scp->put($file) or warn $scp->{errstr}; }

    However, if you are really intent on hard-coding the password into the server, then consider using Net::SSH2. It allows all the different types of authentication.

    use Net::SSH2; my $ssh2 = Net::SSH2->new(); $ssh2->connect('example.com') or die ("SSH2 Connect Error: $!"); if ($ssh2->auth_keyboard('password')) { for my $file (@files) { scp_put($file); } }

    Note: All the code is untested.

      Net::SSH::Perl has a perl-only implementation of SSH-1 and SSH-2, with a variety of authentications schemes. The docs identify some performance advantages to being all perl, but more relevant here they say
      It also simplifies the process of using password-based authentications; when writing a wrapper around ssh you probably need to use Expect to control the ssh client and give it your password. Net::SSH::Perl has built-in support for the authentication protocols, so there's no longer any hassle of communicating with any external processes.
      hey thanks for the reply..where can I get Net::SCP qw(scp); or Net::SSH2; and where do I copy these files in my directory?
Re: SCP connection
by superfrink (Curate) on Jul 24, 2006 at 19:17 UTC
    You can also look into ssh-agent. This lets you type your password once (to decrypte ssh keys) and then your later processes can use the decrypted key.

    The tutorial I used to set it up is at http://mah.everybody.org/docs/ssh. Be sure to read the security notes http://www.unixwiz.net/techtips/ssh-agent-forwarding.html#sec In particular any process that runs as the same user or as root will be able to use the decrypted credentials to connect to the destination machine.