| |||||||||||
| |||||||||||
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.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |