package com.bean;
import java.io.IOException;
import java.util.ArrayList;
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;
import com.sict.MainFrame;
public class GetSnmp {
private Snmp snmp = null;
private Address targetAddress = null;
MainFrame mf;
public void initComm(String ip) throws IOException {
// 设置Agent方的IP和端口
targetAddress = GenericAddress.parse("udp:"+ip+"/161");
TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
transport.listen();
}
public String[] sendPDU(String reqOid) throws IOException {
// 设置 target
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
// 通信不成功时的重试次数
target.setRetries(2);
// 超时时间
target.setTimeout(1500);
target.setVersion(SnmpConstants.version1);
// 创建 PDU
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(reqOid)));
// MIB的访问方式
pdu.setType(PDU.GETNEXT);
// 向Agent发送PDU,并接收Response
ResponseEvent respEvnt = snmp.send(pdu, target);
// 解析Response
String[] outinfo = new String[]{"",""};
if (respEvnt != null && respEvnt.getResponse() != null) {
Vector<VariableBinding> recVBs = respEvnt.getResponse().getVariableBindings();
if(recVBs.size()>0){
VariableBinding recVB = recVBs.elementAt(0);
System.out.println(recVB.getOid() + " : " + recVB.getVariable());
outinfo[0] = recVB.getOid().toString();
outinfo[1] = recVB.getVariable().toString();
}
}
return outinfo;
}
public String getAll() throws IOException {
// 设置 target
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
// 通信不成功时的重试次数
target.setRetries(2);
// 超时时间
target.setTimeout(1500);
target.setVersion(SnmpConstants.version1);
// 创建 PDU
PDU pdu = new PDU();
// MIB的访问方式
pdu.setType(PDU.GETNEXT);
String stroid = "0.0";
String snmpvalue = "";
int j = 0;
String all="";
System.out.println();
int eofmib = 0;
while(eofmib != 5){
j = j + 1;
//System.out.println(j);
pdu.clear();
pdu.add(new VariableBinding(new OID(stroid)));
// 向Agent发送PDU,并接收Response
ResponseEvent respEvnt = snmp.send(pdu, target);
// 解析Response
stroid = "oid:";
// mf.textBox.append(all);
if (respEvnt != null && respEvnt.getResponse() != null) {
Vector<VariableBinding> recVBs = respEvnt.getResponse().getVariableBindings();
if(recVBs.size()>0){
VariableBinding recVB = recVBs.elementAt(0);
//System.out.println(recVB.getOid() + " : " + recVB.getVariable());
stroid = recVB.getOid().toString();
snmpvalue = recVB.getVariable().toString();
eofmib = recVB.getSyntax();
if(eofmib != 5)
{
all=all+stroid+"/n";
all=all+snmpvalue+"/n";
System.out.println(all);
}
}
}
else{
System.out.println("尚未安装snmp协议");
all="尚未安装snmp协议!";
return all;
}
}
return all;
}
public String getSnmpAll(String ip) {
String name=ip;
String all="";
try{
initComm(name);
all=getAll();
} catch (IOException e) {e.printStackTrace();}
return all;
}
public String[] getValue(String ip) {
String name=ip;
String snmpinfo[] = new String[]{"0.0",""};
int ii = 0;
String returnValue[]=new String[10];
try {
do{
initComm(name);
snmpinfo = sendPDU(snmpinfo[0]);
returnValue[ii]=snmpinfo[1];
ii = ii + 1;
}
while(ii < 10);
} catch (IOException e) {e.printStackTrace();}
return returnValue;
}
public ArrayList getPartMIB(String stroids,int oidindex) throws IOException {
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(SnmpConstants.version1);
PDU pdu = new PDU();
pdu.setType(PDU.GETNEXT);
String stroid = stroids;
int i = 0;
Boolean iscomplete = false;
ArrayList<MIB> outmib = new ArrayList<MIB>();
oidindex = oidindex-1;
String[] arrind = stroids.split("//.");
String strind = arrind[oidindex];
while(!iscomplete){
pdu.clear();
pdu.add(new VariableBinding(new OID(stroid)));
ResponseEvent respEvnt = snmp.send(pdu, target);
if (respEvnt != null && respEvnt.getResponse() != null) {
Vector<VariableBinding> recVBs = respEvnt.getResponse().getVariableBindings();
if(recVBs.size()>0){
VariableBinding recVB = recVBs.elementAt(0);
String[] strtemp = recVB.getOid().toString().split("//.");
if((recVB.getSyntax() != 5)&&(strtemp[oidindex].equals(strind)))
{
//outmib.add(recVB.getOid().toString() + "|" + recVB.getVariable().toString());
MIB mib=new MIB();
mib.setDesc(recVB.getVariable().toString());
mib.setOid(recVB.getOid().toString());
outmib.add(mib);
i = i + 1;
stroid = recVB.getOid().toString();
}
else
{iscomplete = true;}
}
}
else{
System.out.println("There is no Snmp protocol.");
iscomplete = true;
}
}
return outmib;
}
public ArrayList getMIBsystem(){
ArrayList arrmibsys = new ArrayList();
try{
arrmibsys = getPartMIB("1.3.6.1.2.1.1",7);
return arrmibsys;
} catch (IOException e) {e.printStackTrace();return null;}
}
public ArrayList getMIBif(){
ArrayList arrmibif = new ArrayList();
try{
arrmibif = getPartMIB("1.3.6.1.2.1.2",7);
return arrmibif;
} catch (IOException e) {e.printStackTrace();return null;}
}
public ArrayList getMIBip(){
ArrayList arrmibip = new ArrayList();
try{
arrmibip = getPartMIB("1.3.6.1.2.1.4.22.1",9);
return arrmibip;
} catch (IOException e) {e.printStackTrace();return null;}
}
public ArrayList getMIBicmp(){
ArrayList arrmibic = new ArrayList();
try{
arrmibic = getPartMIB("1.3.6.1.2.1.5",7);
return arrmibic;
} catch (IOException e) {e.printStackTrace();return null;}
}
public ArrayList getMIBtcp(){
ArrayList arrmibtc = new ArrayList();
try{
arrmibtc = getPartMIB("1.3.6.1.2.1.6",7);
return arrmibtc;
} catch (IOException e) {e.printStackTrace();return null;}
}
public ArrayList getMIBudp(){
ArrayList arrmibud = new ArrayList();
try{
arrmibud = getPartMIB("1.3.6.1.2.1.7",7);
return arrmibud;
} catch (IOException e) {e.printStackTrace();return null;}
}
public ArrayList getMIBegp(){
ArrayList arrmibeg = new ArrayList();
try{
arrmibeg = getPartMIB("1.3.6.1.2.1.8",7);
return arrmibeg;
} catch (IOException e) {e.printStackTrace();return null;}
}
public ArrayList getMIBsnmp(){
ArrayList arrmibsn = new ArrayList();
try{
arrmibsn = getPartMIB("1.3.6.1.2.1.11",7);
return arrmibsn;
} catch (IOException e) {e.printStackTrace();return null;}
}
public ArrayList getMIBhost(){
ArrayList arrmibho = new ArrayList();
try{
arrmibho = getPartMIB("1.3.6.1.2.1.25.2.3",9);
return arrmibho;
} catch (IOException e) {e.printStackTrace();return null;}
}
public ArrayList getMIBcommon(){
ArrayList arrmibco = new ArrayList();
try{
arrmibco = getPartMIB("1.3.6.1.4.1.77.1.1",9);
return arrmibco;
} catch (IOException e) {e.printStackTrace();return null;}
}
public ArrayList getMIBserver(){
ArrayList arrmibse = new ArrayList();
try{
arrmibse = getPartMIB("1.3.6.1.4.1.77.1.2",9);
return arrmibse;
} catch (IOException e) {e.printStackTrace();return null;}
}
public ArrayList getMIBworkstation(){
ArrayList arrmibws = new ArrayList();
try{
arrmibws = getPartMIB("1.3.6.1.4.1.77.1.3",9);
return arrmibws;
} catch (IOException e) {e.printStackTrace();return null;}
}
public ArrayList getMIBdomain(){
ArrayList arrmibdo = new ArrayList();
try{
arrmibdo = getPartMIB("1.3.6.1.4.1.77.1.4",9);
return arrmibdo;
} catch (IOException e) {e.printStackTrace();return null;}
}
public static void main(String[] args){
GetSnmp gg = new GetSnmp();
try{gg.initComm("192.168.132.80");}catch(IOException e){e.printStackTrace();}
ArrayList jj = gg.getMIBip();
int num=0;
for(int i=jj.size()/2;i<jj.size()-1;i++){
//num+=Integer.parseInt(((MIB)((jj.get(i)))).getDesc());
// System.out.println(num);
}
ArrayList j=gg.getMIBip();
for(int i=0;i<j.size();i++){
System.out.println(((MIB)((jj.get(i)))).getOid()+"|"+((MIB)((jj.get(i)))).getDesc()+" &&&&&&&&&&&&&&&&&&&&&"+i+"***********************");
}
}
public ArrayList getMIBmemo(){
ArrayList arrmibho = new ArrayList();
try{
arrmibho = getPartMIB("1.3.6.1.2.1.25.2.2",9);
return arrmibho;
} catch (IOException e) {e.printStackTrace();return null;}
}
public ArrayList getMIBdeviCount(){
ArrayList arrmibho = new ArrayList();
try{
arrmibho = getPartMIB("1.3.6.1.2.1.25.3.2.1.1",11);
return arrmibho;
} catch (IOException e) {e.printStackTrace();return null;}
}
public ArrayList getMIBdevi(){
ArrayList arrmibho = new ArrayList();
try{
arrmibho = getPartMIB("1.3.6.1.2.1.25.3.2.1",10);
return arrmibho;
} catch (IOException e) {e.printStackTrace();return null;}
}
public void init(MainFrame mf){this.mf=mf;}
}
如果想要得结果不能满足要求,可以适当调整OID的位数来截取,直到满意为止。