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

java snmp协议_snmp属于哪一层协议背景控制华为交换机的poe供电与断电来重启PADsnmp协议使用importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importjava.util.Vector;importlombok.extern.slf4j.Slf4j;importorg.snmp4j.CommunityTarget;importorg.snmp4j.PDU;importorg.snmp4j.Snmp;

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新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);
  }
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号