Skip to content

Instantly share code, notes, and snippets.

@pmorch
Last active September 27, 2015 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmorch/1210984 to your computer and use it in GitHub Desktop.
Save pmorch/1210984 to your computer and use it in GitHub Desktop.
Workaround for SNMP::Session->new(DestHost=>"bogus") printing to STDERR
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;
}
@pmorch
Copy link
Author

pmorch commented Sep 12, 2011

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