Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
Perl and SSH2 problem

Posted by micrak (micrak), 15 January 2008
Hello All.

I need to run a shell script that is located in one directory:


This script references 2 other scripts that reside in the same directory (home/michal/id). However, within a Perl script I need to make a connection as root via ssh, which places me in the root home. I am trying to change the working directory within the Perl script so that my main shell script can reference these other 2 scripts without a need of specyfying a direct path (can't change them).

Could you please help me? Have a look at the code:

use Net::SSH2;


my $ssh2 = Net::SSH2->new();
if (!$ssh2->connect($host, 22)){
print "Error..";

if (!$ssh2->auth_password($username,$pwd)){
print "Error..";

elsif (auth_ok) {
print "Connection OK\n";
my $channel = $ssh2->channel();
print $output;}

else {print "Error..\n"; exit;}


I have tried to add one more line:

$channel->exec('cd /home/michal/id');
and then

but it didn't help. Any ideas on how I can change the working directory and still be able to execute that script from that directory

Thank's a lot!

Posted by admin (Graham Ellis), 15 January 2008
You should be able to do it by execing a cd followed by your other commands all within the same command rather that with separate execs.

Try running

"cd /home/michal/id; $prog_to_run"

all in one go (you may need to do it in a shell).

Posted by micrak (micrak), 15 January 2008
Thanks Graham, this is what I needed here!

Best regards

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho