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

I'm using Net::OpenSSH with the following openssh version: OpenSSH_5.3p1 Debian-3ubuntu4, OpenSSL 0.9.8k 25 Mar 2009

My problem is, when I connect with my script to a remote machine and then issue the command:

cd /home/test/Scripts/TestCaseScripts

I get the following error:
bash: line 0: cd: /home/test/Scripts/TestCaseScripts/: No such file or + directory
I verified that the command string works as expected when I login via ssh and run the command by hand..........Does this issue have to do with my pty or terminal settings? My connection options are very simple:
$session_obj = Net::OpenSSH->new("$host", user => $username, password +=> $password, timeout=> 30, strict_mode => 0,master_opts => [ -o => " +StrictHostKeyChecking=no"]);
I read through the cpan docs for Net::OpenSSH but wasn't sure which option to set

Replies are listed 'Best First'.
Re: Net::OpenSSH can't change working directory
by ahmad (Hermit) on Jul 16, 2010 at 19:52 UTC

    I don't think it's about setting options... show us your code so we could know what's you are talking about.

    Normally your command should be something like $session_obj->system('cd /home/...') or die $session_obj->error;

    If it says directory doesn't exists then you might have some spelling error.

      Here is the code I'm using for capturing the command:
      my ($out,$err) = $self->{connected}->capture2($params->{cmd});
      I double checked the spelling to make sure it wasn't that, copy/paste style. Same command works by hand. Could it be because I need to quote the command? I have a command list that I loop through gathering the result output
      cd /home/sitest/Scripts..... tclsh test_script.tcl ./check_network.pl
Re: Net::OpenSSH can't change working directory
by jethro (Monsignor) on Jul 16, 2010 at 19:58 UTC

    You could try some easier path like '/' first and work your way up if that succeeds. One possibility would be some invisible characters in your path string, that can happen if you read that path from a file, for example

    PS: What ahmad meant with 'Show us your code' was more along a minimal runnable script that exhibits the problem. Then someone here could run your script with a different ssh-server and report if the script basically works or not

      Sorry, it's part of a large application I'm building so I dummed it down to a simple test:
      #!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; my $username = 'user'; my $password = 'password'; my $params; $params->{cmd} = "cd /home/user/scripts"; my $session_obj; $session_obj = Net::OpenSSH->new("x.x.x.x", user => $username, passwor +d => $password, strict_mode => 0,master_opts => [ -o => "StrictHostKe +yChecking=no"]); $session_obj->error and die "ssh failed: " . $session_obj->error; my ($out,$err) = $session_obj->capture2({timeout => 30}, $params->{cmd +}); print "THE OUT IS $out\nTHE ERROR IS $err\n";

        Tried your code and it worked. I used 'cd /usr/local/bin; ls' and got the expected listing

        You haven't said if you tried 'cd /' or something else like 'ls' as commands. Did that work or not?

        The FAQ of Net::OpenSSH informs us that the module uses ssh in command mode, i.e. it logs in for each new command string. Using Expect together with Net::OpenSSH or other ssh modules is offered as a solution, it should use interactive mode.

        You could also just join commands together with ';', for example "cd /usr/local/bin; ls; mv $somefile $otherfile". Low-tech but simple.

        It seems that I have two different problems. The first problem is how do I get successive commands to work as if the user is inside the shell or pseudo terminal. If I used the above script and add a second capture line after the first with the command "pwd" I still see the directory as /home/user which means each command is unique to the home directory. So I guess I need to know how to combine command sets........the 2nd error is the bash cd error, but I don't see that same error with my small local script.
Re: Net::OpenSSH can't change working directory
by Khen1950fx (Canon) on Jul 18, 2010 at 00:11 UTC
    I tidied-up your script. This worked for me.
    #!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; my $host = 'host'; my $username = 'user'; my $password = 'pass'; my $cmd1 = 'cd /home/tests/Scripts/TestCaseScripts'; my $cmd2 = 'ls'; my $ssh = Net::OpenSSH->new($host, user => $username, password => $password, timeout => 30, strict_mode => 0, master_opts => [ -o => "StrictHostKeyChecking=no"]); $ssh->error and die "ssh failed: " . $ssh->error; $ssh->system($cmd1) or die "remote command failed: " . $ssh->error; my ($out,$err) = $ssh->capture($cmd1); $ssh->error and die "remote command failed: " . $ssh->error; $ssh->system($cmd2) or die "remote command failed: " . $ssh->error; ($out, $err) = $ssh->capture2($cmd2); $ssh->error and die "remote command failed: " . $ssh->error;