By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,852 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,852 IT Pros & Developers. It's quick & easy.

running shell script from perl script

P: 9
i have a netwok,and i have a perl script running on a machine(say machine1). the purpose of the script is to login to each of the other machines using ssh. i am able to logon to other machne (say machine2) byusing the following code

system("ssh <host_ip>");

after loging on to the network i need to run another perl script that is saved on the machine2. i want it to be done from the perl script running on machine1. i tried to do it by giving

perl filename.pl > testfile.txt ***(i used the testfile to check wheether it is getting executed)

after loging on to machine2(ssh machine2) from the perl code running on machine1. but it is not working.

is there any way to solve the problem?
Feb 17 '08 #1
Share this Question
Share on Google+
6 Replies


KevinADC
Expert 2.5K+
P: 4,059
look into the Net::SSH module instead of shelling out to ssh. Search CPAN for Net::SSH

search.cpan.org
Feb 17 '08 #2

docdiesel
Expert 100+
P: 297
Hi,

try to give the command (or even script) to be executed by stdin:

Expand|Select|Wrap|Line Numbers
  1. system(" echo 'perl filename.pl >testfile.txt'  | ssh rmtserver ");
  2. # or
  3. system(" cat centr_mngd_code.pl   | ssh rmtserver 'perl >testfile.txt' ");
Additionally the second option gives you the possibility to store your perl script once on machine A while being able to execute it on the remote servers.

Another way I often used is the executing "outer scripts" or commands within back ticks:
Expand|Select|Wrap|Line Numbers
  1. $cmd_output = ` some_script.pl ` ;
  2. # or
  3. $cmd_output = ` perl some_script.pl ` ;
Regards,

Bernd
Feb 18 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Hi,

try to give the command (or even script) to be executed by stdin:

Expand|Select|Wrap|Line Numbers
  1. system(" echo 'perl filename.pl >testfile.txt'  | ssh rmtserver ");
  2. # or
  3. system(" cat centr_mngd_code.pl   | ssh rmtserver 'perl >testfile.txt' ");
Regards,

Bernd
You can run scripts on remote machines like that?
Feb 19 '08 #4

docdiesel
Expert 100+
P: 297
@KevinADC:

yes, you can. We're using the "cat script | ssh remotesrv" method to use, hm, a remote linux server as "external coprocessor" for our mainframe: The host is sending the shell commands via ssh, the linux system is executing them.

Regards,

Bernd

P.S.: Of course you've got to exchange ssh keys between the systems for remote login without password.
Feb 19 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
@KevinADC:

yes, you can. We're using the "cat script | ssh remotesrv" method to use, hm, a remote linux server as "external coprocessor" for our mainframe: The host is sending the shell commands via ssh, the linux system is executing them.

Regards,

Bernd

P.S.: Of course you've got to exchange ssh keys between the systems for remote login without password.
I see, very useful if the OP can shell in like that.

Thanks
Feb 19 '08 #6

P: 89
the main funda is to pass the shell command through ssh....
So, pipe the command through ssh.
then the ssh session will open and the command will pass through it.
it will take the enviroment variable of the remort machine.

for all this you need to creat the genkey. for public and private key in ssh.
so no password is required for the remort machine.

regards
rohit
Feb 21 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.