From 1b7e75938c52dd285ca95c56b56747c2ca001e3f Mon Sep 17 00:00:00 2001 From: levinse Date: Sun, 24 Jul 2011 16:11:50 +0000 Subject: [PATCH] shellcommands.pm: move to Net::OpenSSH and add ignored_errors option, RT831 --- FS/FS/part_export/shellcommands.pm | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/FS/FS/part_export/shellcommands.pm b/FS/FS/part_export/shellcommands.pm index 50af45d7d..73433852a 100644 --- a/FS/FS/part_export/shellcommands.pm +++ b/FS/FS/part_export/shellcommands.pm @@ -80,6 +80,9 @@ tie my %options, 'Tie::IxHash', 'Radius group mapping to reason (via template user)', type => 'textarea', }, + 'ignored_errors' => { label => 'Regexes of errors to ignore, separated by newlines', + type => 'textarea' + }, # 'no_queue' => { label => 'Run command immediately', # type => 'checkbox', # }, @@ -350,6 +353,7 @@ sub _export_command { host => $self->machine, command => $command_string, stdin_string => $stdin_string, + ignored_errors => $self->option('ignored_errors') || '', ); if($self->option($action . '_no_queue')) { @@ -440,6 +444,7 @@ sub _export_replace { host => $self->machine, command => $command_string, stdin_string => $stdin_string, + ignored_errors => $self->option('ignored_errors') || '', ); if($self->option('usermod_no_queue')) { @@ -465,8 +470,22 @@ sub shellcommands_queue { } sub ssh_cmd { #subroutine, not method - use Net::SSH '0.08'; - &Net::SSH::ssh_cmd( { @_ } ); + use Net::OpenSSH; + my $opt = { @_ }; + my $ssh = Net::OpenSSH->new($opt->{'user'}.'@'.$opt->{'host'}); + die "Couldn't establish SSH connection: ". $ssh->error if $ssh->error; + my ($output, $errput) = $ssh->capture2($opt->{'command'}); + die "Error running SSH command: ". $ssh->error if $ssh->error; + if ($errput && $opt->{'ignored_errors'} && length($opt->{'ignored_errors'})) { + my @ignored_errors = split('\n',$opt->{'ignored_errors'}); + foreach my $ignored_error ( @ignored_errors ) { + $errput =~ s/$ignored_error//g; + } + chomp($errput); + } + die $errput if $errput; + die $output if $output; + ''; } #sub shellcommands_insert { #subroutine, not method -- 2.11.0