running shell script from perl script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aravindtn
    New Member
    • Jan 2008
    • 9

    running shell script from perl script

    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?
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    look into the Net::SSH module instead of shelling out to ssh. Search CPAN for Net::SSH

    search.cpan.org

    Comment

    • docdiesel
      Recognized Expert Contributor
      • Aug 2007
      • 297

      #3
      Hi,

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

      Code:
      system(" echo 'perl filename.pl >testfile.txt'  | ssh rmtserver ");
      # or
      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:
      Code:
      $cmd_output = ` some_script.pl ` ;
      # or
      $cmd_output = ` perl some_script.pl ` ;
      Regards,

      Bernd

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Originally posted by docdiesel
        Hi,

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

        Code:
        system(" echo 'perl filename.pl >testfile.txt'  | ssh rmtserver ");
        # or
        system(" cat centr_mngd_code.pl   | ssh rmtserver 'perl >testfile.txt' ");
        Regards,

        Bernd
        You can run scripts on remote machines like that?

        Comment

        • docdiesel
          Recognized Expert Contributor
          • Aug 2007
          • 297

          #5
          @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.
          Last edited by docdiesel; Feb 19 '08, 05:45 PM. Reason: Added PS

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Originally posted by docdiesel
            @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

            Comment

            • rohitbasu77
              New Member
              • Feb 2008
              • 89

              #7
              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

              Comment

              Working...