Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

SNMP using perl/cgi

by msk_0984 (Friar)
on Aug 14, 2006 at 09:14 UTC ( #567172=perlquestion: print w/ replies, xml ) Need Help??
msk_0984 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks

i am writing a perl /cgi program to implement the concept of SNMP Get request and the cgi page which takes IP address, Community, OID values ...once user clicks submit button then u have to show the result of that particular OID but when i am trying the create a session object i am getting an error snmp session cannot be created. i am new to SNMP and i think i am missing out some thing very fundamental i have tried it out in different ways and in a normal snmp program i could create the session easily and get the OIDs information so i need ur help. my code is

#!/usr/bin/perl ## CGI /Perl Prog to get the OID and ## using get _req getting the reqested data use strict; use Net::SNMP; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser warningsToBrowser); ## Used to see i +f any errors in fix it out my ($IP,$Cmty,$Oid); my $ob = new CGI; print $ob->header; print $ob->start_html(-title=>"SNMP Get_Request" ); print "<Center>"; print $ob->h2("<i><u>SNMP Get Requested Information</u></i>"); print $ob->hr; if ( $ob->param() ) { $IP = $ob->param('ip'); chomp($IP); $Cmty = $ob->param('community'); chomp($Cmty); $Oid = $ob->param('oid'); chomp($Oid); print $ob->b("INFO"); print $ob->em(" IP : $IP Community: $Cmty Oid : $Oid <br> "); my $sesobj = &createSession($ob,$IP,$Cmty); print $ob->em("Session Obj : $sesobj"); } else { print $ob->start_form(-name=>"get_req",-method=>"POST", -action=>"http://localhost/cgi-bin/cgiprog +/snmp/get_req.cgi"); print $ob->em("IP Address "); print $ob->textfield(-name=>"ip"); print $ob->br($ob->br); print $ob->em("Community "); print $ob->textfield(-name=>"community"); print $ob->br($ob->br); print $ob->em("Object ID "); print "&nbsp &nbsp"; print $ob->textfield(-name=>"oid"); print $ob->br($ob->br); print " &nbsp &nbsp &nbsp &nbsp "; print $ob->submit(-name=>"Submit", -value=>"Submit" ); print " &nbsp &nbsp &nbsp &nbsp "; print $ob->reset; } print $ob->end_form; print $ob->end_html; sub createSession() { my ($object, $ip, $cmty) = @_; ## print $object->em("IP : $ip <br> Cmty : $cmty <br> " ); ## Che +cking whether val r obtained here or not chomp($ip); chomp($cmty); my ($session, $error) = Net::SNMP->session(-hostname => $ip, -community=> $cmty, -port => '161' ); if (!defined ($session)) { print ("Err Creation Session Obj: $session->error "); exit 1; } else { print "Session created "; } }
I am getting the error

Error :Creation Of Session Object ->error

Thanks In Advance

Work Hard Party Harderrr!!
Sushil Kumar

Comment on SNMP using perl/cgi
Download Code
Re: SNMP using perl/cgi
by gellyfish (Monsignor) on Aug 14, 2006 at 09:21 UTC

    In the first instance you want to change the error report on not being able to create the SNMP session to something like:

    print ("Err Creation Session Obj: $error ");
    as a) you have just determined that $session is undef so you can't call a method on it and b) the method call wouldn't be interpolated in a string anyway as you are seeing. $error here is returned by session and has a meaningful value if the session creation failed.

    /J\

      Yes

      Your are right when i hav changed it to

      print ("Err Creation Session Obj: $error ");
      I get the output as

      Err Creation Session Obj: Failed to open UDP/IPv4 socket: Permission denied

      Update : But how should i overcome this prob please help me out.

      Work Hard Party Harderrr!!
      Sushil Kumar

        It's not immediately obvious what the origin of this message is from the Net::SNMP source, normally I wouldn't expect EPERM from simply try to create a UDP socket, but as you don't say what your OS is I can't be sure. You might want to get more diagnostic information by adding -debug => 0x04, to the arguments to session(). Also can you confirm whether the program works properly from the command line or not.

        /J\

        /usr/bin/perl implies some sort of Unix. You are trying to listen to port number 161. On most unices, only the root user is allowed to open ports lower than 1024. You need to be root or use a different port. (Note that running CGI programs as root is not a GoodIdea(tm).


        The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://567172]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-11-23 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (134 votes), past polls