Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

cpan module to do stuff remotely

by Steve_BZ (Hermit)
on Jun 05, 2012 at 20:40 UTC ( #974553=perlquestion: print w/ replies, xml ) Need Help??
Steve_BZ has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm looking for a cpan module which reads through some sort of file (eg a shell file) and executes system commands, and then checks for errors. It seems like this was what perl was built for:

Eg I might have a text file called update.sh, that looks like this:

apt-get -y install ffmpeg cpan -i FFmpeg::Command cp a.png b.png svn co ...... tar ............ etc etc etc

You get the idea. What I would like is a perl module that can read through such a file and execute the system maintenance commands on a remote PC.

These are my limitations:

  • It does not need to be synchronous (although it can be).
  • It is running behind a router or firewall *(this means I probably can't log on to it), so it will have to periodically download a file with a series of commands in it from a convenient server.
  • It should provide a log of activity that can be analysed or uploaded to a server later.

What do you think?

I am looking at Net::SSH::Perl and Sys::Manage::Desktops, but I'm not sure yet whether they meet my needs. How do you do this on your setups?

Thanks and regards

Steve

* = Update

Comment on cpan module to do stuff remotely
Select or Download Code
Re: cpan module to do stuff remotely
by thomas895 (Hermit) on Jun 05, 2012 at 21:23 UTC

    I am looking at Net::SSH::Perl and Sys::Manage::Desktops, but I'm not sure yet whether they meet my needs
    What does that mean, "do not meet my needs"? Net::SSH is tried and true, has great CPANRatings, appears to test correctly on almost every system(according to CPANTesters), and has worked for me in the past.
    What is your criteria?

    ~Thomas~
    confess( "I offer no guarantees on my code." );

      Hi Thomas,

      Well I think I said but I'm not sure yet whether they meet my needs. My needs are to run commands, trap errors and log output (as above) and I need to work through a firewall. I haven't tested Net:SSH yet, but it seems to be interactive and I'm not sure how you connect through a gateway if you don't have a dedicated IP address for the machine being maintained and the ports aren't forwarded. As I say I need to do some checking. Maybe it does everything I need.

      At the moment, I imagine the remote programme with regularly check a file on a website and execute the instructions in it.

      Regards

      Steve

        Net::SSH, or the OO Net::SSH::Perl is easy to use:

        #!/usr/bin/perl -wT use Net::SSH::Perl; use feature qw( say ); my $ssh = Net::SSH::Perl->new( "1.2.3.4" ); $ssh = Net::SSH::Perl->new( "my.host.name" ); #You can use a hostname $ssh->login( "zaphod", "twoheads2" ); my($stdout, $stderr, $exit) = $ssh->cmd( "cat /dev/universe" ); say "The command said: $stdout"; say "The command error'd: $stderr"; say "The command's exit status was $exit";

        You can always do it yourself if you really must, with IO::Socket, or even Socket if you dare.

        ~Thomas~
        confess( "I offer no guarantees on my code." );
Re: cpan module to do stuff remotely
by dasgar (Deacon) on Jun 05, 2012 at 21:36 UTC

    Don't know if such a module exists or not, but what you're wanting to do sounds a bit dangerous to me. For example, if the following happens to show up in your text file: rm /* -rf

    I'm sure you can find a combination of modules to help you achieve what you're wanting to do, but I personally would caution you to be sure to add one more requirement: sanitize the commands before running them to avoid running harmful commands such as the above example.

    Of course, I guess there's a possibility that I might not know what I'm talking about here and may have an unrealistic fear of harmful commands being run in your described scenario. Perhaps this isn't related to Little Bobby Tables.

      Nice link. Actually, no one else apart from the code maintainer has access to this. Bobby Tables won't make it onto the list.

      Regards

      Steve

        You hope that no one else apart from the code maintainer has access to this. If a miscreant gets into your script distribution point how many remote systems would it trash?

        I would not take the risk. Sign the data with a key not present on the distribution platform and have the remote systems verify that signature, at the very least. Plenty of other steps you could take in addition.

Re: cpan module to do stuff remotely
by tospo (Hermit) on Jun 06, 2012 at 06:33 UTC
    Why not just a cron job that uses wget or something to download the file and then simply executes it? Your files are shell scripts anyway and you want to execute them on the local machine, so why use Perl at all for this purpose? As for error handling you can redirect STDERROR to some sort of log file or catch errors and write something to a file or whatever you need to do. Isn't that all you need?
Reaped: Re: cpan module to do stuff remotely
by NodeReaper (Curate) on Jun 07, 2012 at 13:24 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://974553]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-12-23 00:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (133 votes), past polls