Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Workaround for SNMP::Session->new(DestHost=>"bogus") printing to STDERR
my $sess;
my ($tempSTDERR, $STDERRfilename) = File::Temp::tempfile();
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
# 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'",

This comment has been minimized.

Show comment Hide comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment