Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

In Perl, how to check for the remote folder existence using sftp command? Cant use Net:SFTP

by Yedu (Acolyte)
on May 25, 2016 at 14:31 UTC ( [id://1164077]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

As part of SFTP process, I need to validate the remote folder before sftp'ing the files. We cannot have the Net::SFTP module installed on our servers.

I have written the following code to accomplish the same. *

#!/usr/bin/perl -i my $sftp_cmd = "sftp $usr\@$address <<EOF \n cd $remote_dir \nbye \nEO +F"; my $sftp_log = `$sftp_cmd`; if ($? == 0) { print "SFTP Completed Successfully"; } else { print "SFTP Failed"; }

* When I pass some wrong directory to $remote_dir, it is showing the information "Couldn't canonicalise: No such file or directory" in the command line. But finally, throwing the message "SFTP Completed Successfully".

Can some one please help how to handle this?

Is there any other variable that I should use instead of '$?' to check for the status?.

Thanks in Advance.

Regards,
Edu

  • Comment on In Perl, how to check for the remote folder existence using sftp command? Cant use Net:SFTP
  • Download Code

Replies are listed 'Best First'.
Re: In Perl, how to check for the remote folder existence using sftp command? Cant use Net:SFTP
by runrig (Abbot) on May 25, 2016 at 14:44 UTC
    Use Net::SFTP::Foreign. It's pure perl, and uses your system's existing ssh (though it may require IO::Pty if using password auth).
      Thanks for your response. Yes. we are using password authenticated sftp set up in place and need to use the same. So, you mean, we can simply use Net::SFTP::Foreign? it has nothing to do with installation?
        What is preventing you from installing any other libraries? I think this is the first barrier you need to overcome.
Re: In Perl, how to check for the remote folder existence using sftp command? Cant use Net:SFTP
by choroba (Cardinal) on May 25, 2016 at 14:40 UTC
    Please, format your question with <code>...</code> tags.

    Also, the question was crossposted to StackOverflow. It's considered polite to inform about crossposting to prevent people not attending both sites from wasting their efforts hacking on a problem already solved at the other end of the internet.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: In Perl, how to check for the remote folder existence using sftp command? Cant use Net:SFTP
by haukex (Archbishop) on May 25, 2016 at 14:53 UTC

    Hi Yedu,

    At least on my system, the "Couldn't canonicalise: ..." message gets printed to sftp's stderr stream, which the backticks you're using to run the command don't capture, and the sftp command itself exits normally after the "bye", which is why you're not seeing an error in $?.

    My suggestion would be to switch to a module that is capable of capturing stderr when running the external command. My personal favorites are IPC::Run3 and Capture::Tiny. There's also the core module IPC::Cmd, which I don't have experience with.

    The "not so nice" solution is to have the shell merge the command's stdout and stderr streams by inserting " 2>&1" just before " <<EOF". Then you should be able to inspect $sftp_log for the error message with a regular expression.

    Hope this helps,
    -- Hauke D

      When I tried to use these modules IPC::Run3 and Capture::Tiny I am getting error like Can't locate IPC/Run3.pm and Can't locate Capture/Tiny.pm respectively. Does that mean, the modules are need to be installed? Please help.

        Yes, it means they need to be installed.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-23 19:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found