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
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

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:

/home/michal/id/script.sh

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;
$host='192.168.1.5';
$user='root';
$pwd='root';

$prg_to_run='/home/michal/id/script.sh

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

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

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

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

------------------------------------------------------------------

I have tried to add one more line:

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

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!
Michal


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
Michal



This page is a thread posted to the opentalk forum at www.opentalk.org.uk 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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho