- #mib.properties
- #Fri Jul 11 15:57:28 CST 2008
- 1.3.6.1.2.1.1.6.0=beijing
- 1.3.6.1.2.1.1.8.0=test
- 1.3.6.1.2.1.1.5.0=admin
- 1.3.6.1.2.1.1.7.0=8899
#mib.properties
#Fri Jul 11 15:57:28 CST 2008
1.3.6.1.2.1.1.6.0=beijing
1.3.6.1.2.1.1.8.0=test
1.3.6.1.2.1.1.5.0=admin
1.3.6.1.2.1.1.7.0=8899
mib搞的两天不是很明白,于是自己定义了个配置文件来充当mib库,mib.properties
下面的例子是服务器端,也就是manager:
- import java.io.IOException;
-
- import java.util.Vector;
-
- import org.snmp4j.CommunityTarget;
-
- import org.snmp4j.PDU;
-
- import org.snmp4j.Snmp;
-
- import org.snmp4j.TransportMapping;
-
- import org.snmp4j.event.ResponseEvent;
-
- import org.snmp4j.mp.SnmpConstants;
-
- import org.snmp4j.smi.Address;
-
- import org.snmp4j.smi.GenericAddress;
-
- import org.snmp4j.smi.OID;
-
- import org.snmp4j.smi.OctetString;
-
- import org.snmp4j.smi.VariableBinding;
-
- import org.snmp4j.transport.DefaultUdpTransportMapping;
-
- public class SnmpUtil {
- private Snmp snmp = null;
- private Address targetAddress = null;
-
- public void initComm() throws IOException {
-
- targetAddress = GenericAddress.parse("udp:192.168.10.191/161");
- TransportMapping transport = new DefaultUdpTransportMapping();
- snmp = new Snmp(transport);
- transport.listen();
- }
-
- public ResponseEvent sendPDU(PDU pdu) throws IOException {
-
- CommunityTarget target = new CommunityTarget();
- target.setCommunity(new OctetString("public"));
- target.setAddress(targetAddress);
-
- target.setRetries(2);
-
- target.setTimeout(1500);
- target.setVersion(SnmpConstants.version1);
-
- return snmp.send(pdu, target);
- }
-
- public void setPDU() throws IOException {
-
- PDU pdu = new PDU();
- pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 7,
- 0 }), new OctetString("8899")));
- pdu.setType(PDU.SET);
- sendPDU(pdu);
- }
- public void getPDU() throws IOException {
-
- PDU pdu = new PDU();
- pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 6,
- 0 })));
- pdu.setType(PDU.GET);
- readResponse(sendPDU(pdu));
- }
-
- public void readResponse(ResponseEvent respEvnt) {
-
- System.out.println("----------解析Response-------------");
- if (respEvnt != null && respEvnt.getResponse() != null) {
- Vector recVBs = respEvnt.getResponse()
- .getVariableBindings();
- for (int i = 0; i < recVBs.size(); i++) {
- VariableBinding recVB = recVBs.elementAt(i);
- System.out
- .println(recVB.getOid() + " : " + recVB.getVariable());
- }
- }
-
- }
-
- public static void main(String[] args) {
- System.out.println("----------start-------------");
- try {
- SnmpUtil util = new SnmpUtil();
- util.initComm();
- util.setPDU();
- util.getPDU();
- } catch (IOException e) {
-
- e.printStackTrace();
-
- }
-
- }
-
- }