Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Net::SFTP mess

by Angel (Friar)
on Nov 09, 2004 at 00:55 UTC ( [id://406243]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks

I have to SSH into another box to transfer files. I really need to keep the other end of the system ( where the ftp server is running ) simple so no soap or other options will work. Basically I am automating a set of servers that all hold images, instead of updating each one. A perl script will update the servers.

Net::SSH I had ( since I am a end user not an admin ) installed for me but they could only get it to work on perl 5.8.0. But it has a different /lib and some of the modules like DBI are broken. I could push and have them update every module for the new perl edition, but I would rather not.

Is there a way around using this module? Is there a way of scripting SFTP in a different way?

Replies are listed 'Best First'.
Re: Net::SFTP mess
by tachyon (Chancellor) on Nov 09, 2004 at 01:59 UTC

    If you don't want to use perl modules you probably need to script SCP(1). Here is an edited bit of code from a backup script that should get you started

    my $SCP = '/usr/bin/scp'; my $KEYFILE = '/root/.ssh/id_rsa'; my $CVS = "$BACKUP/CVS-$REMOTE_HOST"; my $REMOTE_PATH = "$REMOTE_USER\@$REMOTE_HOST:$REMOTE_PATH"; my $res = `$SCP -o Protocol=2 -i $KEYFILE -c blowfish -B -p $REMOTE_PA +TH $CVS 2>&1`; $res =~ s/[^\n\040-\177]//g; warn $res ? "scp failed and returned:\n$res\n\n" : "scp succeeded!\n\n +";

    cheers

    tachyon

Re: Net::SFTP mess
by Aighearach (Initiate) on Nov 09, 2004 at 02:07 UTC
    You could just use scp. scp is way better than sftp for scripting, IMO. And also, for sysadmin type tasks, I would rather use Perl as glue, than try to reinvent tools with it just to do it in Pure Perl. Just too... religious, for me.

    I use public/private keys for authentication in this case, because not only is it (only slighter) safer than putting the password in the script, but it removes the need to interact with scp in order to transfer files.

    perl -e '@hosts = ( map { $_ . q[.example.com] } qw/ foo bar www / ); +system( q[scp], q[-r], @ARGV, sprintf(q[%s:remotedir], $_) ) foreach +@hosts;' /tmp/foo
    Here is another version using ssh and rsync, so as to only send the parts that changed, and ignores .CVS files:
    perl -e '@hosts = ( map { $_ . q[.example.com] } qw/ foo bar www / ); +system( q[rsync], q[-rcC], q[-e], q[ssh], @ARGV, sprintf(q[%s:remoted +ir], $_) ) foreach @hosts;' /tmp/foo
    Of course, if the usernames on the aren't the same, you have to add that also.

    --
    Snazzy tagline here
Re: Net::SFTP mess
by data64 (Chaplain) on Nov 09, 2004 at 03:14 UTC

    Depending on the particulars of what you are trying to do, you might want to look at rsync over ssh.


    Just a tongue-tied, twisted, earth-bound misfit. -- Pink Floyd

Re: Net::SFTP mess
by steves (Curate) on Nov 09, 2004 at 02:42 UTC

    And for yet another way: If you have Net::SSH::Perl installed, you can also use a remote command like cat (on a *nix system) to move files back and forth, since Net::SSH::Perl's cmd method lets you both capture standard output from the remote command and supply a buffer to use as standard input fed to the remote command.

    # $data contains data from a local file to put in the # remote file named by $to ($output, $error, $status) = $ssh->cmd("cat > $to", $data); # On return, $output contains data from the remote file # identified by $from (check status, etc. of course!) ($output, $error, $status) = $ssh->cmd("cat $from");

Re: Net::SFTP mess
by steves (Curate) on Nov 09, 2004 at 01:29 UTC

    I'm curious what issues you had getting Net::SFTP installled. I've installed it on both 5.6.1 and 5.8.0 (both Solaris). Do you have specifics about the problems there? It may be easier to fix the install than to do it another way.

      No I do not, and think that they will probably not give the details out. I figure these people can run circles around my meager skills and they tried getting it to run.
      How the heck did you get it to install on Solaris??!?!!??? I tried for two days and couldn't get the various dependencies to play nice ... I installed without a problem on various Redhats, but Solaris just wouldn't go ...

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://406243]
Approved by atcroft
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-03-28 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found