http://www.perlmonks.org?node_id=974553

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:

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

Replies are listed 'Best First'.
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?
Re: cpan module to do stuff remotely
by dasgar (Priest) 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 thomas895 (Deacon) 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." );