http://www.perlmonks.org?node_id=942191

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

Hi

I want to use Net::OpenSSH to make life a bit easier in maintaining systems. For this I want to first copy a file to the destination machine and afterwards login to the machine, untar the file to a temp directory and changing the directory to this temporary directory so I can run a script from that directory.

However this fails to work. If I do for instance 'cd /usr/local/ ; ls -la' as system command it works fine. If I first do 'cd /usr/local/' and then a new command 'ls -la' it gives the listing of the login directory. Ergo it does change the directory but it loses the working directory when the next system command is run.

Example code

#!/usr/bin/perl use Getopt::Long; use Net::SFTP::Foreign; use XML::Simple; use Net::OpenSSH; my $config = XMLin('agama.xml'); my $debug = 0; my $username = ''; my $password = ''; GetOptions ('debug' => \$debug, 'file=s' => \$rpmfile, 'username=s' = +> \$username, 'password=s' => \$password); foreach $host (@{$config->{hostname}}) { if ($password eq '') { $ssh = Net::OpenSSH->new($host,user=>$usernam +e,timeout => 10,strict_mode => 0); } else { $ssh = Net::OpenSSH->new($host,user=>$username,password=>$pas +sword,timeout => 10,strict_mode => 0); } $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error; my $cmd1 = 'cd /usr/local/ ; ls -la'; my $cmd2 = 'ls -la'; print "Output cmd1:\n"; $ssh->system($cmd1) or die "remote command failed: " . $ssh->error; print "Output cmd2:\n"; $ssh->system($cmd2) or die "remote command failed: " . $ssh->error; }
Output:

Output cmd1:
total 96
drwxr-xr-x 12 root root 4096 Dec  6 21:00 .
drwxr-xr-x 15 root root 4096 Dec  6 21:01 ..
drwxr-xr-x  2 root root 4096 Oct  1  2009 bin
drwxr-xr-x  2 root root 4096 Oct  1  2009 etc
drwxr-xr-x  2 root root 4096 Oct  1  2009 games
drwxr-xr-x  2 root root 4096 Oct  1  2009 include
drwxr-xr-x  2 root root 4096 Oct  1  2009 lib
drwxr-xr-x  2 root root 4096 Oct  1  2009 lib64
drwxr-xr-x  2 root root 4096 Oct  1  2009 libexec
drwxr-xr-x  2 root root 4096 Oct  1  2009 sbin
drwxr-xr-x  4 root root 4096 Dec  6 21:00 share
drwxr-xr-x  3 root root 4096 Dec  6 20:10 src
Output cmd2:
total 160
drwxr-x---  7 root root  4096 Dec  7 10:02 .
drwxr-xr-x 25 root root  4096 Dec  7 09:23 ..
-rw-------  1 root root  1270 Dec  6 21:06 anaconda-ks.cfg
-rw-------  1 root root   198 Dec  6 20:11 .bash_history
-rw-r--r--  1 root root    24 Jul 13  2006 .bash_logout
-rw-r--r--  1 root root   191 Jul 13  2006 .bash_profile
-rw-r--r--  1 root root   176 Jul 13  2006 .bashrc
drwxr-xr-x  4 root root  4096 Dec  7 10:02 .cpan
-rw-r--r--  1 root root   100 Jul 13  2006 .cshrc
-rw-r--r--  1 root root 36832 Dec  6 21:06 install.log
-rw-r--r--  1 root root  3686 Dec  6 21:06 install.log.syslog
drwx------  2 root root  4096 Dec  7 10:02 .libnet-openssh-perl
drwx------  2 root root  4096 Dec  7 10:02 .ssh
-rw-r--r--  1 root root   129 Jul 13  2006 .tcshrc

Tested on CentOS 5.7 and RedHat 5.6. Also compiled a new version of OpenSSH on the main and destination machine but unfortunatly the results stay the same.

Can someone tell me what I am doing wrong? Do I need an extra setting or use a different command?

Replies are listed 'Best First'.
Re: Net::OpenSSH changing directory does not work properly
by salva (Canon) on Dec 07, 2011 at 09:58 UTC
Re: Net::OpenSSH changing directory does not work properly
by keszler (Priest) on Dec 07, 2011 at 10:53 UTC
      Net::OpenSSH does also support running a remote shell and it even allows to use Expect to interact with it.

      But in my opinion, talking to a shell is harder and less reliable than running simple commands even if they have to be prepended with cd $path && ....

        Aha - I overlooked $ssh->open2pty. Thank you.

        I agree that for a handful of serial-dependent commands the shell overhead is excessive, but at some point I believe it becomes worthwhile.

Re: Net::OpenSSH changing directory does not work properly
by TJPride (Pilgrim) on Dec 07, 2011 at 11:17 UTC
    Why not use absolute paths? Not necessarily as elegant, but it works.