大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
背景
- 控制华为交换机的poe供电与断电来重启PAD
snmp协议使用
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import lombok.extern.slf4j.Slf4j;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
/** * * @description 华为交换机工具类 */
@Slf4j
public class HuaWeiSwitchesSnmpConnection {
private static final String USERNAME = "tuantimingcheng";
private static final int DEFAULT_VERSION = SnmpConstants.version2c;
private static final String DEFAULT_PROTOCOL = "udp";
private static final int DEFAULT_PORT = 161;
private static final long DEFAULT_TIMEOUT = 3 * 1000L;
private static final int DEFAULT_RETRY = 3;
/** * 根据ip 和 团体名称创建 团体Target * @param ip * @param community * @return */
private static CommunityTarget createTarget(String ip, String community) {
Address targetAddress = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip + "/" + DEFAULT_PORT);
CommunityTarget target = new CommunityTarget(targetAddress, new OctetString(community));
target.setVersion(DEFAULT_VERSION);
target.setTimeout(DEFAULT_TIMEOUT);
target.setRetries(DEFAULT_RETRY);
return target;
}
/** * 获取对应ip下交换机oid的状态 * * @param ip * @param oid * @return */
public static int getStatus(String ip, String oid) {
int status = 0;
CommunityTarget target = createTarget(ip, USERNAME);
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(oid)));
pdu.setType(PDU.GET);
try {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
ResponseEvent respEvent = snmp.send(pdu, target);
if (respEvent != null && respEvent.getResponse() != null) {
status = respEvent.getResponse().get(0).getVariable().toInt();
} else {
// TODO 此处异常状态,需要log
log.error(
"SNMP getStatus request time out, Response is null. [ip = " + ip + ", community = "
+ SnmpDevice.PDU_READ_USER + "]");
}
snmp.close();
} catch (IOException e) {
e.printStackTrace();
// TODO 此处异常状态,需要log[exception];
log.error("SNMP getStatus Exception:" + e);
}
return status;
}
/* 查询ip下oid对应的端口集合 */
public static List<String> getOidAndPortList(String ip, String oid) {
CommunityTarget target = createTarget(ip, USERNAME);
PDU pdu = new PDU();
List<String> oidAndVariableList = new ArrayList<>();
String currentOid = oid;
Vector<VariableBinding> recVBs;
try {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
while (true) {
pdu.add(new VariableBinding(new OID(currentOid)));
pdu.setType(PDU.GETNEXT);
ResponseEvent respEvent = snmp.send(pdu, target);
if (respEvent != null && respEvent.getResponse() != null) {
recVBs = (Vector<VariableBinding>) respEvent.getResponse()
.getVariableBindings();
VariableBinding variableBinding = recVBs.get(recVBs.size() - 1);
currentOid = variableBinding.getOid().toString();
if (!oid.equals(currentOid
.substring(0, currentOid.lastIndexOf(".")))) {
break;
}
}
}
for (int i = 0; i < recVBs.size(); i++) {
if (i != recVBs.size() - 1) {
oidAndVariableList.add(recVBs.get(i).getOid() + "," + recVBs.get(i).getVariable());
}
}
snmp.close();
} catch (IOException e) {
e.printStackTrace();
// TODO 此处异常状态,需要log[exception];
log.error("SNMP getStatus Exception:" + e);
}
return oidAndVariableList;
}
/** * 发送命令方法 * * @param ip 交换机ip * @param oid 发送命令的oid * @param command 命令参数 * @return */
public static int setCommand(String ip, String oid, Integer command) {
int status = 0;
CommunityTarget target = createTarget(ip, USERNAME);
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(oid), new Integer32(command)));
pdu.setType(PDU.SET);
try {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
ResponseEvent respEvent = snmp.send(pdu, target);
if (respEvent != null && respEvent.getResponse() != null) {
int result = respEvent.getResponse().get(0).getVariable().toInt();
if (result == command) {
// 命令执行成功!
status = 1;
}
} else {
// TODO 此处异常状态,需要log
System.out.println(
"SNMP sent command request time out, Response is null. [ip = " + ip + ", community = "
+ SnmpDevice.PDU_WRITE_USER + "]");
}
snmp.close();
} catch (IOException e) {
e.printStackTrace();
// TODO 此处异常状态,需要log[exception];
System.out.println("SNMP sent command Get Exception:" + e);
}
return status;
}
private static final String HUA_WEI_OID = "1.3.6.1.4.1.2011.5.25.195.3.1.6";
private static final String HUA_WEI_OID_SWITCH = "1.3.6.1.4.1.2011.5.25.195.3.1.15.23";
/* 切换管理方式 */
private static final String HUA_WEI_MANAGEMENT = "1.3.6.1.4.1.2011.5.25.195.2.1.8.0";
public static void main(String[] args) {
String ip = "192.168.0.196";
//切换管理方式 1.手动管理 2.自动管理
HuaWeiSwitchesSnmpConnection.setCommand(ip, HUA_WEI_MANAGEMENT, 1);
//控制开关 1.关机 2.开机
HuaWeiSwitchesSnmpConnection.setCommand(ip, HUA_WEI_OID_SWITCH, 1);
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HuaWeiSwitchesSnmpConnection.setCommand(ip, HUA_WEI_OID_SWITCH, 2);
//获取Oid和port
/*List<String> status = HuaWeiSwitchesSnmpConnection.getOidAndPortList(ip, HUA_WEI_OID_SWITCH); for (String message : status) { System.out.println(message); }*/
int status1 = HuaWeiSwitchesSnmpConnection.getStatus(ip, HUA_WEI_OID_SWITCH);
System.out.println(status1);
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/181280.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...