Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How to execute Perl code on remote machine from a local Perl script and process the output/variables in your local Perl script

by dushyant (Acolyte)
on Sep 15, 2013 at 05:49 UTC ( #1054158=perlquestion: print w/replies, xml ) Need Help??
dushyant has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I want to write one perl script a.pl which will run on Local Machine A. This script a.pl will execute some unix command and check timestamp of log files on Remote Machine B. I also want to run for loop and text filtering/formatting on log files of Remote Server. I want to import all the output of remote unix commands and outcome of text processing of remote log files into my local perl script a.pl, So that I can generate a report and email it.

I can use Net::OpenSSH to execute unix command on remote server. But I dont know how I am going to use for loop on remote machine to process all the log files in different directories. The problem is If I use Bash shell for loop then my local Perl script a.pl will give syntax error and if I use perl for loop then I got bash c syntax error. Another big problem is if everything goes fine than how to import variables and output to my local perl script

Thanks.
  • Comment on How to execute Perl code on remote machine from a local Perl script and process the output/variables in your local Perl script

Replies are listed 'Best First'.
Re: How to execute Perl code on remote machine from a local Perl script and process the output/variables in your local Perl script
by Corion (Pope) on Sep 15, 2013 at 07:00 UTC

    You don't show any code and error messages, so it is hard to suggest any concrete fixes to your script.

    I recommend to write a Perl script that runs on the machine B, and copy that to the machine B and start that through SSH.

    You could make the script B output JSON (or JSON::Tiny) to transfer the resulting data back in a convenient way.

Re: How to execute Perl code on remote machine from a local Perl script and process the output/variables in your local Perl script
by marinersk (Priest) on Sep 15, 2013 at 08:24 UTC

    Let me start by saying I agree with Corion's notes entirely. You can't run a script on Machine A and have it magically perform its Perl code on Machine B. At best it could cause Machine B to launch a Perl script which is already present there.

    It sounds like you want to solve a common sysadmin issue, where you need to collect remote log information into a central location.

    Two approaches, amongst many, to do this:

    1. You could write complementary Perl scripts; one on Machine A which directs the activities in some way, and one on Machine B which performs the activities thusly directed. This will require that you establish some mechanism which permits them to communicate with one another.
    2. The script on Machine A could simply read the log files from Machine B directly. This solution is easier to implement but can cause potentially disastrous amounts of network traffic. WARNING: This approach does not scale well.

    Good luck with the script(s) -- I think this is one of the more common uses of Perl, so you'll get loads of help if you get stuck, once you've written some code you can show.

    As a final note, there's nothing wrong with asking "theory" questions, so long as it's clear you're still in the Perl world. I do it all the time here, and the answers have, without fail, always led me to a solution.

    Update: Fixed typo "complimentary" to "complementary" as originally intended. The scripts will not be expected to say positive things about each other; rather, they will be expected to work together well and perform roughly reciprocal functions. :: facepalm ::

Re: How to execute Perl code on remote machine from a local Perl script and process the output/variables in your local Perl script
by shantanu_bhadoria (Novice) on Sep 16, 2013 at 09:50 UTC

    The solution here depends on how much work you want to do and what permissions you have on Machine B. If you can SSH to it then Net::OpenSSH to execute commands is the simplest and quickest way to get this done. Other options will require you to put some code on Machine B.

    One easy to implement but heavier(in terms of system resources) option is to run a webservice on a unoccupied port number using Dancer or Catalyst etc. on Machine B and let the webservice run the commands and return the result to you.

    Third option albeit insecure in its bare form is to write a program using one of the event handling systems like AnyEvent, EV or POE to create a listening network socket on Machine B. This requires familiarity with event loops.

    Make a socket connection from Machine A to Machine B when required and send the command to run. The Target Machine can then process the command and stream the result back to the Machine A on the same socket.

    You can use POE::Component::Server::TCP on Machine B and POE::Component::Client::TCP on Machine A.
    Note that on Machine B the server must run with the same permissions that you wish to grant for running the commands(You dont want a random person running "sudo rm -rf /"). Of course a better assurance against abuse would be to write some code in the server to decide weather to accept a command or not.

    HTH
    -Shantanu Bhadoria

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1054158]
Approved by vinoth.ree
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2017-12-12 14:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (333 votes). Check out past polls.

    Notices?