A few remarks and another solution you might find useful:

I have written a similar script, maybe you can adept it to your needs, if our fellow monks don't think its to insecure

#!/usr/local/bin/perl -w # # perl_by_ssh # should be named ssh_via_perl :-) use strict; use vars qw($PROGNAME $VERSION); use lib "/path/to/secure/user/libexec" ; use utils qw(%ERRORS &print_revision &support &usage); $PROGNAME = 'perl_by_ssh'; $VERSION = '$Revision: 1.9 $'; sub print_help (); sub print_usage (); $ENV{'PATH'}=''; $ENV{'BASH_ENV'}=''; $ENV{'ENV'}=''; my $ssh2 = '/path/to/ssh/bin/ssh'; my $ssh2_opts = '-F /path/to/secure/user/.ssh2/ssh2_config -x -l user' +; my $remote_path_prefix = '/path/to/remote/exec/dir'; my $log_prefix = '/path/to/secure/user/var/checklog/'; my $host = $ARGV[0] if $ARGV[0] !~ m/^[\.a-z0-9\-]+$/i; my $remote_cmd = $ARGV[1] if $ARGV[1] !~ m/^[:%'"\\\/\.a-z0-9 \-_#]+$/ +i; unless ($host) { print "hostname '$host' invalid"; exit($ERRORS{'UNKNOWN'}); } unless ($remote_cmd ) { print "command '$remote_cmd' invalid"; exit($ERRORS{'UNKNOWN'}); } if ($remote_cmd =~ /#TIME#/) { my $time = `/bin/date +"%D %T"`; chomp($time); $remote_cmd =~ s/#TIME#/$time/g; } #open (LOG, ">>","$log_prefix/remotecmd.log"); #print LOG "executing: $remote_cmd \n"; #close(LOG); my $opt_log = 1 if $ARGV[2] && $ARGV[2] =~ m/^-l$/; $opt_log |= 0; my $log_file_basename = $1 if $remote_cmd =~ m/^([a-z0-9_\-]+)\s*/i; my $logfile = $log_file_basename ? $log_prefix . $log_file_basename . + ".log" : ""; my $command = "" . $ssh2 . " " . $ssh2_opts . " " . $host . " " . $rem +ote_path_prefix . $remote_cmd . " 2>&1"; my $erg = `$command`; $erg =~ s/[^a-z0-9:\[\]/\/]//i; my $exit_code = $? >> 8; print $erg; if ($logfile && $opt_log) { if ( -d $log_prefix && -r $log_prefix) { open(LOG, ">>", $logfile); print LOG '[' . `time` . "] " . $host . " " . $erg; close(LOG); } } exit($ERRORS{'UNKNOWN'}) if $erg =~ m/warning: Connecting to [a-z0-9\. +-]+ failed/i; exit($ERRORS{'UNKNOWN'}) if $exit_code > 4; exit($exit_code); # # nagios plugin-standards # sub print_usage () { print "Usage: $PROGNAME <host> 'cmd' [-l]\n"; } sub print_help () { print_revision($PROGNAME,$VERSION); print 'Copyright (c) 2002,2003 This plugin executes checks remote via ssh2, returning their stdou +t and exit-status. If -l is supplied as the third parameter, the result string is log +ged in [time] $host $result_string format into <user_home>/var/checklog/<remote_cmd_basename>.log if you include the string "#TIME#" in the provided command, it wil +l be replaced with the current localtime in the format "MM/DD/YY hh:mm:ss" (as \'date +"%D %T"\' +provides) '; print_usage(); print " "; support(); }

Note that -T is absent, because it runs with it enabled, and I can spare a few CPU-cycles this way, and that this script won't runout of the box for you, due to the absence of util.pm, but it should illustrate a IMVHO sane solution.

Edit: inserted untainting version :)

regards,
tomte


Hlade's Law:

If you have a difficult task, give it to a lazy person --
they will find an easier way to do it.


In reply to Re: simple iterate and run cmd script by Tomte
in thread simple iterate and run cmd script by patris

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.