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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 2022最新淘宝商品详情接口采集方法

    2022最新淘宝商品详情接口采集方法最新淘宝商品详情接口采集

    2022年7月21日
    14
  • apt一键下载所有依赖的包_apt自动安装依赖包

    apt一键下载所有依赖的包_apt自动安装依赖包这几天从书本上见识到了aptitude包管理工具的魅力,果断想在自己的UbuntuKylin16.10上玩一玩。没想到遇到了很多的问题~~~apt源更新,编辑apt源配置文件 /etc/apt/sources.list发现vi使用起来好费劲,只能用delete删除 而上下键和backspace键都没法正常使用。还有看启动栏在左侧Left 有点不习惯,也做了设置:按下Ctrl+Al…

    2025年7月1日
    5
  • 查看是否安装了webpack_封装webpack

    查看是否安装了webpack_封装webpack报错信息:解决方法:1.npminstall-gwebpack-cli2.npminstall–save-devwebpack-cli//同步到局部项目文件夹下再次检查版本成功!但是提示:webpack-dev-servernotinstalled于是再次执行命令:npminstall–save-devwebpack-dev-server成功!…

    2022年8月10日
    32
  • [已发表,转载勘误]Android upx脱壳「建议收藏」

    [已发表,转载勘误]Android upx脱壳「建议收藏」已发在https://www.anquanke.com/post/id/197643不过有部分内容发布之后无法编辑,勘误后如下。Androidupx脱壳写在前面因为我不是pc平台过来的,而是直接从Android入门的,所以upx壳其实一开始并不了解,后来接触到,但是可以直接动态调试或者做个内存快照,对我来说加没加upx其实对我逆向分析影响不大。另一方面upx壳因为开源且其实有很多脱壳的教…

    2022年7月19日
    18
  • UVa 10054 : The Necklace 【欧拉回路】

    UVa 10054 : The Necklace 【欧拉回路】

    2022年3月6日
    55
  • 微信小程序报错 errcode: 40029, errmsg: “invalid code

    微信小程序报错 errcode: 40029, errmsg: “invalid code小程序报错errcode:40029,errmsg:&quot;invalidcode,hints:[req_id:************]今天在进行小程序开发过程中,需要登录获取用户的openID信息当调用wx.login后,进一步需要根据所得的code码进行登录凭证校验微信小程序官方文档-wx.loginerrcode:40029,errmsg:&quot;i…

    2022年5月5日
    429

发表回复

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

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