-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnectionManager.java
More file actions
executable file
·114 lines (100 loc) · 3.07 KB
/
ConnectionManager.java
File metadata and controls
executable file
·114 lines (100 loc) · 3.07 KB
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
/* Establishes Connection to SNMP Agents
* and kills them if requested
*/
import java.net.InetAddress;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import com.adventnet.snmp.snmp2.*;
import com.adventnet.snmp.snmp2.usm.USMUserEntry;
import com.adventnet.snmp.snmp2.usm.USMUtils;
public class ConnectionManager {
//Variables needed
private SnmpSession session = null;
private SnmpAPI api =new SnmpAPI();
private InetAddress rhost = null;
private String userName = null;
private String password = null;
private SnmpPDU pdu = null;
private int port=161; //Hard coded Port number !
//ENgineID to String
HexBinaryAdapter hbAdapter=new HexBinaryAdapter();
//For returning multiple parameters
private Object connectionDetails[]= new Object [3];;
//For V3
private byte [] snmpID = null;
//Constructor for Port number
public ConnectionManager(int port){
this.port=port;
}
//The main connection class
public Object[] establishConnection(String ip, String userName, String password) throws SnmpException{
String engineIDString="";
try{
//Set the variables for this case userName==communityString (for v1/v2c)
rhost=InetAddress.getByName(ip);
this.userName=userName;
this.password=password;
//Open the Connection
session = new SnmpSession(api);
session.setVersion(SnmpAPI.SNMP_VERSION_3);
session.setRetries(5);
//Open the session
session.open();
//Create the PDU
pdu = new SnmpPDU();
//V3 stuff
engineIDString=initv3Parameter();
// set remote Host
UDPProtocolOptions opt = (UDPProtocolOptions)session.getProtocolOptions();
opt.setRemoteAddress(rhost);
opt.setRemotePort(port);
pdu.setProtocolOptions(opt);
//throw exception to calling class
}catch(Exception e){
throw new SnmpException("Agent not answering");
}
//return session PDu and EngineID
connectionDetails[0]=session;
connectionDetails[1]=pdu;
connectionDetails[2]=engineIDString;
return connectionDetails;
}
//Kill a connection
public boolean terminateConnection(SnmpSession session){
try{
session.close();
}catch(Exception e){
System.out.println("Connection Manager terminateConnection");
e.printStackTrace();
return false;
}
return true;
}
//only internally used
private String initv3Parameter() throws SnmpException{
String engineIDString="";
try{
//DELTE
//Get the ID
SnmpEngineEntry engineID=new SnmpEngineEntry(rhost.getHostAddress(),161);
//session, timeout retries
snmpID=engineID.discoverSnmpEngineID(session,1,2);
//get the engine ID
engineIDString=hbAdapter.marshal(snmpID);
//SNMPv3 stuff
pdu.setUserName(userName.getBytes());
USMUtils.init_v3_parameters(
userName,
USMUserEntry.MD5_AUTH,
password,
null,
rhost.getHostAddress(),
port,
session);
pdu.setContextID(snmpID);
//throw exception to calling class
}catch(Exception e){
throw new SnmpException("Device does not answer");
}
return engineIDString;
}
}