Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Perl SFTP do_mkdir not able to create directory

by skylinedreamer (Novice)
on Apr 05, 2013 at 04:53 UTC ( #1027064=perlquestion: print w/ replies, xml ) Need Help??
skylinedreamer has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks,

I am trying to batch process sftp file transfers using perl threads. I want to create a directory in the home directory before transferring the file into the server. I am passing the login information to the method while i start the thread. I am able to connect with no probs. I am using a Solaris SPARC Machine

I am able to transfer the file, but am unable to create a folder. My code is as follows with the error msg below.

sub sub1 { my @InboundParameters = @_; my $num = $InboundParameters[0]; print "started thread $num : Establishing connection with $Inbound +Parameters[1]\n"; my $host = $InboundParameters[2]; my $sftp = Net::SFTP->new($host, user => $InboundParameters[3], pa +ssword => $InboundParameters[4], debug => 1) or return ( { 'errors' => "Unable to connect to $h +ost: " . $@, 'success' => 0 } ); #$sftp->put("bar", "2345SD/baz"); $sftp->do_mkdir("ftp2SAT5") or "Cannot make directory: $@" . $sftp +->error; print "done with thread $num\n"; return $num; }
Error msg :
Login completed, opening dummy shell channel. channel 0: new [client-session] Requesting channel_open for channel 0. channel 0: open confirm rwindow 0 rmax 32768 channel 1: new [client-session] Requesting channel_open for channel 1. Sending subsystem: sftp Requesting service subsystem on channel 1. channel 1: open confirm rwindow 0 rmax 32768 sftp: Sending SSH2_FXP_INIT sftp: Remote version: 3 sftp: Sent message T:14 I:0 channel 1: rcvd eof channel 1: output open -> drain channel 1: obuf empty channel 1: output drain -> closed channel 1: close_write channel 1: rcvd close channel 1: input open -> closed channel 1: close_read channel 1: send close channel 1: full closed thread failed to start: Connection closed at ./ftp2sats_v0.1.pl line 8 +1 Bus error (core dumped)
Seeking your advice. :-)

Comment on Perl SFTP do_mkdir not able to create directory
Select or Download Code
Re: Perl SFTP do_mkdir not able to create directory
by kcott (Abbot) on Apr 05, 2013 at 06:28 UTC

    G'day skylinedreamer,

    Welcome to the monastery.

    Try changing

    $sftp->do_mkdir("ftp2SAT5") or "Cannot make directory: $@" . $sftp->er +ror;

    to (i.e. add die)

    $sftp->do_mkdir("ftp2SAT5") or die "Cannot make directory: $@" . $sftp +->error;

    Update: See ++Loops' reply below.

    to (i.e. change or to and die)

    $sftp->do_mkdir("ftp2SAT5") and die "Cannot make directory: $@" . $sft +p->error;

    If do_mkdir() is failing, you'll now get some indication of why.

    I also note that there's no output from print "started thread ..., but error output shows thread failed to start: ... - you may want to investigate that also.

    -- Ken

      HI Ken, Thanks for your response. I didnt copy paste the whole output from the script. But its infact printing the first line "started thread.."
Re: Perl SFTP do_mkdir not able to create directory
by Loops (Curate) on Apr 05, 2013 at 06:37 UTC

    Seems you have to supply the attrs parameter to make do_mkdir happy. Also it returns 0 as a successful return, so "or die ..." will do the wrong thing unfortunately. Your code with a few modifications works okay:

    use strict; use warnings; use Net::SFTP; use Net::SFTP::Attributes; sub sub1 { my @InboundParameters = @_; my $num = $InboundParameters[0]; print "started thread $num : Establishing connection with $Inbound +Parameters[1]\n"; my $host = $InboundParameters[2]; my $sftp = Net::SFTP->new($host, user => $InboundParameters[3], pa +ssword => $InboundParameters[4], debug => 0) or return ( { 'errors' => "Unable to connect to $h +ost: " . $@, 'success' => 0 } ); my $attrs = Net::SFTP::Attributes->new(()); $sftp->do_mkdir("ftp2SAT5", $attrs) and die; print "done with thread $num\n"; return $num; } sub1 qw(1 me localhost my_username my_password);
      Hi Loops, thank you for your solution. It worked right out of the box. Seems like do_mkdir needs the Attributes to be included as the parameter
Re: Perl SFTP do_mkdir not able to create directory
by salva (Abbot) on Apr 05, 2013 at 07:30 UTC
    Use Net::SFTP::Foreign instead.

    If you use it combined with Net::OpenSSH you will be even able to reuse the same SSH connections from all the threads:

    use Net::OpenSSH; my $ssh = Net::OpenSSH->new($host, user => $InboundParameters[3], password => $InboundParameters[4]); # then from the threads call... sub sftp_task { my $sftp = $ssh->sftp; $sftp->mkdir("ftp2SAT5"); ... }
      Hi salva,

      Thanks for your response. But I am trying to send the file to different hosts. So I wont be able to use the same SSH to do the task I presume.

        In that case, you can use Net::OpenSSH::Parallel:
        use Net::OpenSSH::Parallel; my $pssh = Net::OpenSSH::Parallel->new; $pssh->add_host($_) for @hosts; sub sftp_task { my ($host, $ssh) = @_; my $sftp = $ssh->sftp; $sftp->mkdir("ftp2SAT5") or die "unable to create directory: " . $sftp->error; } $pssh->all(parsub => &sftp_task); $pssh->run;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2015-07-07 01:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (86 votes), past polls