zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java snmp协议_snmp属于哪一层协议

JAVA协议 属于 snmp 一层
2023-06-13 09:14:31 时间

背景

  • 控制华为交换机的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);
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/181280.html原文链接:https://javaforall.cn