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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |