I'm looking to use an imported constant via a variable that holds it's name.
So Net::SNMP exports a constant named INTEGER - I want to be able to use/access that constant via a variable that holds the name of it.
With the below code I get
Inside snmpset OID:1.3.6.1.4.1.9.9.68.1.2.2.1.2.22, HOST:1.1.1.1, STRI
+NG:mystring, TYPE:INTEGER, VALUE:1
ERROR (1.3.6.1.4.1.9.9.68.1.2.2.1.2.22 1.1.1.1 mystring): Unknown ASN
+.1 type [INTEGER].
but it works fine when I call snmpset this way:
snmpset("1.3.6.1.4.1.9.9.68.1.2.2.1.2.22","1.1.1.1","mystring",INTEGER
+,1);
Outputs:
Inside snmpset OID:1.3.6.1.4.1.9.9.68.1.2.2.1.2.22, HOST:1.1.1.1, STRI
+NG:mystring, TYPE:2, VALUE:1
But statically putting INTEGER in there stops it from being modular - I'd have to make a function for each variable type I would need to set.
How would I pass $bah in such a way that it was actually passing the constant?
#!/usr/bin/perl
use Net::SNMP;
my $bah = "INTEGER";
snmpset("1.3.6.1.4.1.9.9.68.1.2.2.1.2.22","1.1.1.1","mystring",$bah,1)
+;
sub snmpset {
my $oid = shift || die "No oid passed to snmpset\n";
my $host = shift || die "No host passed to snmpset\n";
my $string = shift || die "No community string passed to snmpset\n";
my $type = shift || die "No value type passed to snmpset\n";
my $value = shift || die "No value passed to snmpset\n";
print "Inside snmpset OID:$oid, HOST:$host, STRING:$string, TYPE:$ty
+pe, VALUE:$value\n";
my ($session, $error) = Net::SNMP->session(
-timeout => 5,
-hostname => $host,
-community => $string,
-port => shift || 161
);
if (!defined($session)) {
printf("ERROR ($oid $host $string): %s.\n", $error);
#exit 1;
return -1;
}
my $result = $session->set_request(
-varbindlist => [$oid,$type,$value]
);
if (!defined($result)) {
printf("ERROR ($oid $host $string): %s.\n", $session->error);
$session->close;
return -1;
}
$session->close;
return $result->{$oid};
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.