largetable.java
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:7k
源码类别:

SNMP编程

开发平台:

C/C++

  1. /*$Id: largetable.src,v 1.3 2002/09/09 05:35:19 tonyjpaul Exp $*/
  2. /*
  3.  * @(#)largetable.java
  4.  * Copyright (c) 1996-2002 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the COPYRIGHTS file for more details.
  6.  */
  7. /** 
  8.  *  An example of using the SnmpTablePanel bean which is designed for large
  9.  *  table support.
  10.  *<img SRC="images/v3only.jpg" ALT="v3 only"> [-v] <version>      - version(v1 / v2 / v3). By default v1.
  11.  * [-c] <community>    - community String. By default "public".
  12.  * [-p] <port>         - remote port no. By default 161.
  13.  * [-r] <Retries>      - Retries. By default 0.      
  14.  * [-t] <Timeout>      - Timeout. By default 5000ms.
  15.  *<img SRC="images/v3only.jpg" ALT="v3 only"> [-u] <username>     - The v3 principal/userName
  16.  * [-a] <autProtocol>  - The authProtocol(MD5/SHA). Mandatory if authPassword is specified
  17.  * [-w] <authPassword> - The authentication password.
  18.  * [-s] <privPassword> - The privacy protocol password. Must be accompanied with auth password and authProtocol fields.
  19.  * [-n] <contextName>  - The snmpv3 contextName to be used.
  20.  * [-i] <contextID>    - The snmpv3 contextID to be used.
  21.  * [-m] <mibs>  Mandatory - The mibs to be loaded.
  22.  * host Mandatory      - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
  23.  * tableOID  Mandatory - Give the Object Identifier of a Table.
  24.  **/
  25. import javax.swing.*;
  26. import javax.swing.table.*;
  27. import java.awt.*;
  28. import java.awt.event.*;
  29. import com.adventnet.snmp.beans.*;
  30. import com.adventnet.snmp.ui.*;
  31. import javax.swing.event.*;
  32. import java.io.*;
  33. public class largetable extends JFrame {    
  34.     
  35.     private static final int VERSION = 0;
  36.     private static final int COMMUNITY = 1;
  37.     private static final int PORT = 2;
  38.     private static final int RETRIES = 3;
  39.     private static final int TIMEOUT = 4;
  40.     private static final int USER_NAME = 5;
  41.     private static final int AUTH_PROTOCOL = 6;
  42.     private static final int AUTH_PASSWORD = 7;
  43.     private static final int PRIV_PASSWORD = 8;
  44.     private static final int CONTEXTNAME = 9;
  45.     private static final int CONTEXTID = 10;
  46.     
  47.     SnmpTablePanel tablePanel;
  48.     
  49.  /** <img SRC="images/v3only.jpg" ALT="v3 only"> Supply command line arguments to this constructor **/
  50.     public largetable(String host, String community, String mibs,
  51.                     String tableOID, int port, int retries, int timeout) {
  52.       
  53.              this("v1", host, community, mibs, tableOID, port, retries, timeout, "", "", "", "","","");
  54.   } /*<img SRC="images/v3only.jpg" ALT="v3 only"> largetable */
  55.   
  56.  /** <img SRC="images/v3only.jpg" ALT="v3 only"> Supply command line arguments to this constructor **/
  57.     public largetable(String version, String host, String community, String mibs, String tableOID, int port, int retries,int timeout, String userName, String authProtocol, String authPassword, String privPassword,String contextName,String contextID) {
  58.                                  
  59.     super("SnmpTablePanel");
  60.     tablePanel = new SnmpTablePanel();
  61.     tablePanel.setTargetHost (host);
  62.     tablePanel.setCommunity (community);
  63.     tablePanel.setTargetPort (port);
  64.     tablePanel.setRetries (retries);
  65.     tablePanel.setTimeout (timeout);
  66.  //To load MIBs from compiled file
  67.  tablePanel.setLoadFromCompiledMibs(true);
  68.     if(version.equals("v3"))
  69.         tablePanel.setSnmpVersion(SnmpTarget.VERSION3);
  70.     else if(version.equals("v2"))
  71.         tablePanel.setSnmpVersion(SnmpTarget.VERSION2C);
  72.     else tablePanel.setSnmpVersion(SnmpTarget.VERSION1);
  73.     if(version.equals("v3")){
  74.         tablePanel.setPrincipal(userName);
  75.         if(authProtocol.compareTo("MD5") == 0)
  76.                 tablePanel.setAuthProtocol(SnmpTarget.MD5_AUTH);
  77.         else if(authProtocol.compareTo("SHA") == 0)
  78.                 tablePanel.setAuthProtocol(SnmpTarget.SHA_AUTH);
  79.            else tablePanel.setAuthProtocol(SnmpTarget.NO_AUTH);
  80.         tablePanel.setAuthPassword(authPassword);
  81.            tablePanel.setPrivPassword(privPassword);
  82.            tablePanel.setContextName(contextName);
  83.            tablePanel.setContextID(contextID);
  84.        }
  85.     try {
  86.         System.out.println("Loading MIBs: "+mibs);
  87.         tablePanel.loadMibs(mibs);
  88.         System.out.println("Done.");
  89. if(tablePanel.getSnmpVersion() == SnmpTarget.VERSION3){
  90. tablePanel.create_v3_tables();
  91. }
  92.         tablePanel.setTableOID (tableOID);
  93.     } catch (Exception ex) {
  94.         System.err.println("Error: "+ex);
  95.         System.exit(1);
  96.     }
  97.     getContentPane().add(tablePanel);
  98.     setVisible(true);
  99.     show();    
  100.     }
  101.     
  102.     
  103.   /**  The main method to start the program **/
  104.     public static void main(String[] args) {
  105.     // Take care of getting options
  106.         String usage = "largetable [-v version(v1/v2/v3)] [-c community] [-p port] [-r retries] [-t timeout] [-u userName] [-a authProtocol] [-w authPassword] [-s privPassword] [-n contextname] [-i contextID] host tableOID MIB_files ";
  107.         String options[] = { "-v", "-c", "-p", "-r", "-t", "-u", "-a", "-w", "-s","-n","-i" };
  108.         String values[] = { null, null, null, null, null, null, null, null, null, null,null};
  109.     ParseOptions opt = new ParseOptions(args,options,values, usage);
  110.     // check for at least hostname and one OID in remaining arguments
  111.     if (opt.remArgs.length<3) opt.usage_error();
  112.     String host = opt.remArgs[0];
  113.     String oidTable = opt.remArgs[1];
  114.     String community = "public";
  115.     String mibs = opt.remArgs[2];
  116.     String version ="v1";
  117.     String userName = "";
  118.     String authProtocol = new String("NO_AUTH");
  119.     String authPassword = new String ("");
  120.     String privPassword = new String ("");
  121. String contextName = new String (""); 
  122. String contextID = new String (""); 
  123.     if (values[COMMUNITY] != null) community = values[COMMUNITY];
  124.     int port = 161, retries=0,timeout = 1;
  125.     try {
  126.         if (values[PORT] != null) 
  127.             port = ( Integer.parseInt(values[PORT]) );
  128.         if (values[RETRIES] != null) 
  129.             retries = ( Integer.parseInt(values[RETRIES]) );
  130.         if (values[TIMEOUT] != null) 
  131.             timeout = ( Integer.parseInt(values[TIMEOUT]) );
  132.     } catch (NumberFormatException ex) {
  133.         System.err.println("Invalid Integer Argument "+ex);
  134.     }
  135.     if(values[VERSION] != null)
  136.         version = values[VERSION];
  137.         if((version.equals("v3")) && (values[USER_NAME] == null)) opt.usage_error();
  138.     if(values[USER_NAME] != null)
  139.         userName = values[USER_NAME];
  140.     if(values[AUTH_PROTOCOL] != null)
  141.         authProtocol = values[AUTH_PROTOCOL];
  142.     if(values[AUTH_PASSWORD] != null)
  143.         authPassword = values[AUTH_PASSWORD];
  144.     if(values[PRIV_PASSWORD] != null)
  145.         privPassword = values[PRIV_PASSWORD];
  146.     if(values[CONTEXTNAME] != null)
  147.         contextName = values[CONTEXTNAME];
  148.     if(values[CONTEXTID] != null)
  149.         contextID = values[CONTEXTID];
  150.       JFrame frame = new largetable(version, host, community, mibs, oidTable, port, retries, timeout,
  151.           userName, authProtocol, authPassword, privPassword,contextName,contextID);
  152.     WindowListener l = new WindowAdapter() 
  153.         {
  154.         public void windowClosing(WindowEvent e) { System.exit(0);}
  155.         };
  156.     frame.addWindowListener(l);
  157.     frame.setSize(650,350);
  158.     frame.setVisible(true);
  159.     
  160.     }
  161.     
  162. }