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

The working directory after logging in is '/users/UserName', but why can't you change to a relative directory below that (while using the absolute directory name works, and using the relative directory works with the command line interactive sftp). E.g. this fails at the setcwd with "Couldn't get realpath for remote 'Foo': Folder not found: Foo":
use Net::SFTP::Foreign; my $sftp = Net::SFTP::Foreign->new( 'sftp.foo.com', autodie => 1, ); # cwd is '/users/UserName' print $sftp->cwd(),"\n"; # Would like to cd to directory below 'UserName' $sftp->setcwd('Foo');

Replies are listed 'Best First'.
Re: Net::SFTP::Foreign setcwd to relative directory
by syphilis (Archbishop) on Aug 27, 2011 at 01:22 UTC
    How about (untested):
    $sftp->setcwd('./Foo');

    I don't know if that will work.

    Cheers,
    Rob
Re: Net::SFTP::Foreign setcwd to relative directory
by Khen1950fx (Canon) on Aug 27, 2011 at 11:50 UTC
    I encountered a few problems when I ran the script. autodie => 1 kept showing up as an invalid option, so I just used autodie. Also, I had to use chdir before I could get setcwd with an absolute path to work. Another odd thing was that it didn't work at all without a timeout. I'm sure that salva would know more about that.
    #!/usr/bin/perl use strict; use warnings; use Cwd; use autodie qw( :all ); use Net::SFTP::Foreign; my $sftp = Net::SFTP::Foreign->new( 'sftp.foo.com', password => 'foo_foo', more => '-v', timeout => 0, autoflush => 1, autodisconnect => 2, ); chdir('/root/Desktop'); $sftp->setcwd('/root/Desktop'); print "===================\n", getcwd(), "\n", "Done!\n", "===================\n"; undef $sftp;
      I ran the script. autodie => 1 kept showing up as an invalid option,

      You are probably using an old version of the module not supporting that feature.

      timeout   => 0

      Setting the timeout to 0 guarantees a broken SSH connection to the host and then any method call on the object will fail.

Re: Net::SFTP::Foreign setcwd to relative directory
by salva (Canon) on Aug 28, 2011 at 14:36 UTC
    I am unable to reproduce the problem.

    Could you run the script with debugging enabled:

    $Net::SFTP::Foreign::debug = -1;
    ... and post here the output?