Chinaunix首页 | 论坛 | 博客
  • 博客访问: 411366
  • 博文数量: 101
  • 博客积分: 2324
  • 博客等级: 大尉
  • 技术积分: 887
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-19 19:28
文章分类

全部博文(101)

文章存档

2012年(3)

2011年(60)

2010年(38)

分类: 系统运维

2010-11-23 15:28:55

 
 

agent代理端代码:

Java代码
  1. import java.util.*;   
  2. /**
  3. * agent代理端
  4. * leo
  5. * 20080710
  6. */  
  7. import org.snmp4j.*;   
  8. import org.snmp4j.smi.*;   
  9.   
  10. import snmputil.Config;   
  11.   
  12. public class OTAAgent {   
  13.   
  14.     public static class Handler implements org.snmp4j.CommandResponder {   
  15.         protected java.lang.String mAddress = null;   
  16.         protected int mPort = 0;   
  17.         protected java.lang.String mMyCommunityName = null;   
  18.         protected org.snmp4j.TransportMapping mServerSocket = null;   
  19.         protected org.snmp4j.Snmp mSNMP = null;   
  20.   
  21.         public Handler() {   
  22.          }   
  23.   
  24.         public void configure() {   
  25.              mAddress = "192.168.10.191";   
  26.              mPort = 161;   
  27.              mMyCommunityName = "OAagent";   
  28.          }   
  29.   
  30.         public void start() {   
  31.             try {   
  32.                  mServerSocket = new org.snmp4j.transport.DefaultUdpTransportMapping(   
  33.                         new org.snmp4j.smi.UdpAddress(java.net.InetAddress   
  34.                                  .getByName(mAddress), mPort));   
  35.                  mSNMP = new org.snmp4j.Snmp(mServerSocket);   
  36.                  mSNMP.addCommandResponder(this);   
  37.                  mServerSocket.listen();   
  38.              } catch (java.net.UnknownHostException vException) {   
  39.                  System.out.println(vException);   
  40.              } catch (java.io.IOException vException) {   
  41.                  System.out.println(vException);   
  42.              }   
  43.          }   
  44.   
  45.         public synchronized void processPdu(   
  46.                  org.snmp4j.CommandResponderEvent aEvent) {   
  47.              java.lang.String vCommunityName = new java.lang.String(aEvent   
  48.                      .getSecurityName());   
  49.              System.out.println("Community name " + vCommunityName);   
  50.              org.snmp4j.PDU vPDU = aEvent.getPDU();   
  51.              Config config=new Config();   
  52.             if (vPDU == null) {   
  53.                  System.out.println("Null pdu");   
  54.              } else {   
  55.                  System.out.println("(rcv) " + vPDU.toString());   
  56.                 switch (vPDU.getType()) {   
  57.                 case org.snmp4j.PDU.GET:   
  58.                 case org.snmp4j.PDU.GETNEXT:   
  59.                     break;   
  60.                 case org.snmp4j.PDU.SET:   
  61.                      System.out.println("------SET----------");   
  62.                      String reciv=vPDU.get(0).getVariable().getSyntaxString();   
  63.                      System.out.println("----set------"+vPDU.get(0).toString());   
  64.                      String setoid=vPDU.get(0).toString();   
  65.                      System.out.println("-----set-----"+setoid.substring(0, setoid.indexOf("=")-1));   
  66.                      System.out.println("-----set-----"+setoid.substring(setoid.indexOf("=")+1));   
  67.                      config.setValueByOID(setoid.substring(0, setoid.indexOf("=")-1).trim(), setoid.substring(setoid.indexOf("=")+1).trim());   
  68.                  }   
  69.                  org.snmp4j.mp.StatusInformation statusInformation = new org.snmp4j.mp.StatusInformation();   
  70.                  org.snmp4j.mp.StateReference ref = aEvent.getStateReference();   
  71.                 try {   
  72.                      System.out.println("Sending response");   
  73.                      vPDU.setType(PDU.RESPONSE);   
  74.                        
  75.                      OID oid=vPDU.get(0).getOid();   
  76.                      String setoid=vPDU.get(0).toString();   
  77.                      System.out.println("----get------"+setoid.substring(0, setoid.indexOf("=")-1));   
  78.                      System.out.println("-----get-----"+setoid.substring(setoid.indexOf("=")+1));   
  79.                      vPDU.set(0, new VariableBinding(oid,   
  80.                             new OctetString(config.getValueByOID(setoid.substring(0, setoid.indexOf("=")-1).trim()))));   
  81.   
  82.                      aEvent.getMessageDispatcher().returnResponsePdu(   
  83.                              aEvent.getMessageProcessingModel(),   
  84.   
  85.                              aEvent.getSecurityModel(),   
  86.                              aEvent.getSecurityName(),   
  87.   
  88.                              aEvent.getSecurityLevel(), vPDU,   
  89.                              aEvent.getMaxSizeResponsePDU(), ref,   
  90.   
  91.                              statusInformation);   
  92.                  } catch (org.snmp4j.MessageException vException) {   
  93.                      System.out.println(vException);   
  94.                  }   
  95.              }   
  96.          }   
  97.      }   
  98.   
  99.     public static void main(String argv[]) {   
  100.          Handler h = new Handler();   
  101.         /** 初始化参数 * */  
  102.          h.configure();   
  103.          h.start();   
  104.         /** Do nothing loop * */  
  105.         while (true) {   
  106.              System.out.println("----------loop-------------");   
  107.             synchronized (OTAAgent.class) {   
  108.                 try {   
  109.                      OTAAgent.class.wait();   
  110.                  } catch (Exception e) {   
  111.                  }   
  112.              }   
  113.          }   
  114.      }   
  115. }  


阅读(4466) | 评论(1) | 转发(2) |
给主人留下些什么吧!~~

chinaunix网友2010-11-24 14:33:12

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com