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

So I'm trying to automate the transfer of some files between two linux boxen. My very simple script:
#!/usr/bin/perl use strict; use Net::SCP::Expect; my $user = "xferusr"; my $password = "xxxxxxxx"; my $server = "server123.internal"; my $remotedir = "/var/www/html/reports/2009"; my $filelocation = "/opt/work/reports/2009/"; print "Login...Starting scp..."; my $scpe = Net::SCP::Expect->new(host=>$server, user=>$user, password= +>$password, recursive=>'1'); print "\nFILELOCATION:" . $filelocation . "*\n"; print "REMOTEDIR: " . $remotedir . "\n"; $scpe->scp($filelocation . '*', $remotedir); print "SCP complete\n";
It dies with the error:
Error: last line returned was: opt/work/reports/2009/*: No such file or director at /usr/lib/perl5/site_perl/5.8.0/Expect.pm line 760
If I set verbose I get:
Executing: program /usr/bin/ssh host server123.internal, user ftpusr, command scp -v -r -t /var/www/html/stats_intra/2008 OpenSSH_3.6.1p2, SSH protocols 1.5/2.0, OpenSSL 0x0090701f xferusr@server123.internal's scp exited with non-success state: 256 at ./test_scp.pl line 19
If from the command line I do:
scp /opt/work/reports/2009/* xferusr@server123.internal:/var/www/html/ +reports/2009
It works just wonderfully. So I'm guessing I'm doing something wrong with Perl.

It appears from that first error message that the "/" at the beginning of the path was stripped off. I'm not sure why or how to fix that, or if this is just some weirdness with the error messages.

Can anyone help me figure this out?

Thanks in advance.

Replies are listed 'Best First'.
Re: Problem with Net::SCP::Expect
by salva (Canon) on Feb 23, 2009 at 15:22 UTC
    I have not tested it, but reading the documentation it seems that Net::SCP::Expect, by default, quotes the arguments passed to scp and so, wildcard characters will not be expanded by the shell. Try adding auto_quote => 0 to the constructor call.

    You can also try using other Perl packages as Net::SSH2 or Net::OpenSSH.

      The auto_quote thing didn't work :(

      I'm on RHEL 3 and there isn't a libssh2 package for Net::SSH2

      I'm trying Net::OpenSSH now. However it is complaining:

      "ssh: illegal option -- M"

      Any suggestions?

        The auto_quote thing didn't work :(

        Try running the script with strace to see how it calls the scp program.

        I'm trying Net::OpenSSH now. However it is complaining: "ssh: illegal option -- M"

        That probably means that the version of OpenSSH installed on the machine is too old for Net::OpenSSH. It requires OpenSSH 4.1 or later (5.1 is recommended).

        update: BTW, could you use SFTP instead of SCP?

Re: Problem with Net::SCP::Expect
by zentara (Cardinal) on Feb 23, 2009 at 16:04 UTC
    No such file or director at ....... /

    I hate to keep mentioning this, but see help with scp error codes and try to use an exact fullpath/filename on the remote target. I may be remembering old software, and maybe they have improved things, but the scp and sftp programs seemed to require a fullpath so as avoid the ambiguities of what the CWD is on the remote.....because you are trying to dump that filename in whatever the CWD is on the remote.....you may not have permissions. Also I'm not sure you can use wildcard * patterns like that in scp, but I may be wrong. You may have to get a dirlist, and loop thru the files 1 at a time, and scp the fullpath/filename. ( or use the -r option for recursive)


    I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness
Re: Problem with Net::SCP::Expect
by Illuminatus (Curate) on Feb 23, 2009 at 16:58 UTC
    If you are not limited in your ability to change the environment on either system to some degree, may I suggest using xinetd on the destination? You can define a port number in /etc/services and create a config entry for the daemon. Write a perl program to be invoked by the daemon for this service. This program will be started by xinetd with the incoming connection as stdin. It is pretty easy to pass across path/file/size, followed by the data. The source system perl script simply needs to connect to that system/port, provide the path/file/size, and then send the data. It is not secure, but if everything is within your firewalls, this might not be a problem. I use this approach all the time
Re: Problem with Net::SCP::Expect
by Anonymous Monk on Jun 03, 2015 at 11:02 UTC
    Also check the home directory of the user at destination. Check if the bashrc is echo-ing extra lines at login. If so disable the welcome message/extra lines for this particular user. It should work.