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
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho