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?


In reply to Net::OpenSSH changing directory does not work properly by Pascal2020

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.