1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
   |  
package netwokTools;
 
import java.io.IOException;
import java.net.InetAddress;
import java.util.Calendar;
import java.util.Date;
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.event.ResponseListener;
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 SNMPCommands implements ResponseListener {
	String resp="";
	String targetAddress;
 
	public SNMPCommands(String address) {
		this.targetAddress = address;
	}
 
	public void send(int[] OIDvar, int ReqType) {
		Address targetAddress = GenericAddress.parse("udp:"+this.targetAddress+"/161");
		TransportMapping transport = null;
		try {
			transport = new DefaultUdpTransportMapping();
		} catch (IOException e1) {
			System.out.println("Error here");
		}
		Snmp snmp = new Snmp(transport);
 
		try {
			snmp.listen();
		} catch (IOException e) {
			e.printStackTrace();
		}
 
		//	setting up target
		   CommunityTarget target = new CommunityTarget();
		   target.setCommunity(new OctetString("public"));
		   target.setAddress(targetAddress);
		   target.setRetries(2);
		   target.setTimeout(1500);
		   target.setVersion(SnmpConstants.version1);
		   target.setTimeout(5000);
 
		   // creating PDU
		   PDU pdu = new PDU();
		   pdu.add(new VariableBinding(new OID(OIDvar)));
		   pdu.setType(ReqType);
 
		   //The Send Action
		   try {
			   snmp.send(pdu, target, null, this);
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
 
	public String getOS() {
		send(new int[] {1,3,6,1,2,1,1,1,0}, PDU.GET);
		while (resp.equals(""));
		String full = resp.split(" = ")[1];
		int begin = full.indexOf("Software: ");
		int end = full.indexOf("Version");
		return full.substring(begin+"Software: ".length(),end+"Version ".length()+4);
	}
 
	public Date getSystemConnectionPeriod() {
		int[] oid = new int[] {1,3,6,1,2,1,1,3,0};
		this.send(oid, PDU.GET);
		while (this.resp.equals(""));
		String val = resp.split(" = ")[1];
		String args[] = (val.substring(0,val.length()-3)).split(":");
		Date D = new Date(100000);
		D = this.makeDate(args);
		return D;
	}
 
 
	public static void main(String[] args) {
		SNMPCommands com = new SNMPCommands("10.50.21.22");
		SNMPCommands com2 = new SNMPCommands("10.50.21.22");
 
 
		System.out.println(com.getOS());
		System.out.println(com2.getSystemConnectionPeriod());
	}
 
	public void onResponse(ResponseEvent event) {
		// Always cancel async request when response has been received
		// otherwise a memory leak is created! Not canceling a request
		// immediately can be useful when sending a request to a broadcast
		// address.
		((Snmp)event.getSource()).cancel(event.getRequest(), this);
		this.resp = event.getResponse().get(0).toString();
	}
 
	@SuppressWarnings("deprecation")
	public Date makeDate(String[] args) {
		Calendar C = Calendar.getInstance();
		C.setTimeInMillis(System.currentTimeMillis());
		Date D = new Date(C.getTime().getYear(), C.getTime().getMonth(), C.getTime().getDate());
		D.setHours(Integer.parseInt(args[0]));
		D.setMinutes(Integer.parseInt(args[1]));
		D.setSeconds(Integer.parseInt(args[2]));
		return D;
	}
 
} | 
Partager