public
Last active

Workaround for SNMP::Session->new(DestHost=>"bogus") printing to STDERR

  • Download Gist
woraround.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
my $sess;
 
my ($tempSTDERR, $STDERRfilename) = File::Temp::tempfile();
 
open SAVE_STDERR, '>&STDERR';
open STDERR, '>&' . fileno($tempSTDERR);
 
# If SNMP::Session->new() is going to die for whatever reason, make sure we
# restore STDERR first!
eval {
$sess = SNMP::Session->new(
'Community' => 'public',
'DestHost' => 'bogus',
'Version' => '2c',
);
};
 
my $err = $@;
 
# Don't juse use
# open STDERR, '>&SAVE_STDERR';
# because it gives this warning:
# Name "main::SAVE_STDERR" used only once: possible typo
# refering to the line opening SAVE_STDERR above
open STDERR, '>&' . fileno(SAVE_STDERR);
 
close $tempSTDERR;
 
die $err if $err;
 
my $stderrOutputDuringSessionNew = `cat $STDERRfilename`;
unlink $STDERRfilename;
chomp $stderrOutputDuringSessionNew;
if ($stderrOutputDuringSessionNew ne '') {
warn sprintf "STDERR output from SNMP::Session->new(): '%s'",
$stderrOutputDuringSessionNew;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.