⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 check_for_machine_test.java

📁 Check_for_machine_test是snmpServer客户端
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        final String slot4v35loop1="1.3.6.1.4.1.25449.30.1.9.4.2.0";
        final String slot4v35loop2="1.3.6.1.4.1.25449.30.2.9.4.2.0";
        //参数配置
        final String slot4v35para1="1.3.6.1.4.1.25449.30.1.9.4.3.0";
        final String slot4v35para2="1.3.6.1.4.1.25449.30.2.9.4.3.0";
        //双路V.35卡
        //告警屏蔽
        final String slot42v35war1="1.3.6.1.4.1.25449.30.1.9.7.1.0";
        final String slot42v35war2="1.3.6.1.4.1.25449.30.2.9.7.1.0";
        //环回设置
        final String slot42v35loop1="1.3.6.1.4.1.25449.30.1.9.7.2.0";
        final String slot42v35loop2="1.3.6.1.4.1.25449.30.2.9.7.2.0";
        //参数配置
        final String slot42v35para1="1.3.6.1.4.1.25449.30.1.9.7.3.0";
        final String slot42v35para2="1.3.6.1.4.1.25449.30.2.9.7.3.0";
        //四路V.35卡
        //告警屏蔽
        final String slot44v35war1="1.3.6.1.4.1.25449.30.1.9.8.1.0";
        final String slot44v35war2="1.3.6.1.4.1.25449.30.2.9.8.1.0";
        //环回设置
        final String slot44v35loop1="1.3.6.1.4.1.25449.30.1.9.8.2.0";
        final String slot44v35loop2="1.3.6.1.4.1.25449.30.2.9.8.2.0";
        //参数配置
        final String slot44v35para1="1.3.6.1.4.1.25449.30.1.9.8.3.0";
        final String slot44v35para2="1.3.6.1.4.1.25449.30.2.9.8.3.0";
    String[] oid={board_switch1,board_loop1,board_war1,board_e11,board_channel1,opt01,opt11,
    slot14e1war1,slot14e1loop1,slot1v35war1,slot1v35loop1,slot1v35para1,slot12v35war1,slot12v35loop1,
    slot12v35para1,slot14v35war1,slot14v35loop1,slot14v35para1,slot24e1war1,slot24e1loop1,slot2v35war1,
    slot2v35loop1,slot2v35para1,slot22v35war1};
    public Check_for_machine_test() {
        contentPane.setLayout(new GridLayout(8,2));
        contentPane.add(jl1);
        contentPane.add(jf1);
        contentPane.add(jl2);
        contentPane.add(jf2);

        contentPane.add(j15);
        contentPane.add(jf6);

        contentPane.add(jl3);
        contentPane.add(jf3);
        contentPane.add(jl4);
        contentPane.add(jf4);
        contentPane.add(set);
        contentPane.add(jf5);
        contentPane.add(get);
        contentPane.add(jf7);
        contentPane.add(getnext);
        contentPane.add(jf8);

        set.setBackground(Color.lightGray);
        set.setBorder(BorderFactory.createRaisedBevelBorder());
        jf4.setEditable(false);
        jf4.setBorder(BorderFactory.createLoweredBevelBorder());
        jf5.setEditable(false);
        jf5.setBorder(BorderFactory.createLoweredBevelBorder());
        jf7.setEditable(false);
        jf7.setBorder(BorderFactory.createLoweredBevelBorder());
        jf8.setEditable(false);
        jf8.setBorder(BorderFactory.createLoweredBevelBorder());
        jf4.setFont(new Font("新宋体",Font.PLAIN,13));
        jf5.setFont(new Font("新宋体",Font.PLAIN,13));
        jf8.setFont(new Font("新宋体",Font.PLAIN,13));
        try{
            f = new FileInputStream("MYMIB.PROPERTIES");
            p.load(f);
        }
        catch(Exception e){e.printStackTrace();}
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        setTitle("SNMP 设置程序");
        setSize(400,200);
        setLocation(300,300);
        setVisible(true);
        set.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                jf4.setText("");
                jf5.setText("");
                jf7.setText("");
                jf8.setText("");
                Set(jf1.getText(),"public",1000,jf2.getText(),jf6.getText(),jf3.getText(),false,false);
            }
        });
        get.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                jf4.setText("");
                jf5.setText("");
                jf7.setText("");
                Set(jf1.getText(),"public",1000,jf2.getText(),jf6.getText(),jf3.getText(),true,false);
            }
        });
        getnext.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                jf4.setText("");
                jf5.setText("");
                jf7.setText("");
                jf8.setText("");
                Set(jf1.getText(),"public",1000,jf2.getText(),jf6.getText(),jf3.getText(),true,true);
            }
        });
     }
     public static void main(String[] arg){
         Check_for_machine_test sfm=new Check_for_machine_test();
         //sfm.Set("159.226.220.44","public",1500,"1.3.6.1.2.1.0","161");
     }
    public byte[] get_byte(String value){
         byte[] b=null;//可扩展的服务器
         int times=0;
         char[] c=value.toCharArray();
         for(int i=0;i<c.length;i++){
             if(c[i]==' '){
                 times++;
             }
         }
         if(times==0){
             b=new byte[1];
             b[0]=(byte)Integer.parseInt(value,16);
         }
         else{
             b=new byte[times+1];
             for(int i=0;i<b.length-1;i++){
                 String s_4=value.substring(0,value.indexOf(" "));
                 int a=Integer.parseInt(s_4,16);
                 value=value.substring(value.indexOf(" ")+1);
                 b[i]=(byte)a;
             }
             b[times]=(byte)Integer.parseInt(value,16);
         }
         return b;
     }


     public void Set(String udpaddress,String community,int time,String OID,
                     String code,String value,boolean is_get,boolean is_getnext){
         int ii=0;

             //OID=jf2.getText();
         try{//存在一些问题,即对收到的恢复信息的处理
             Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
             target = new CommunityTarget();
             target.setCommunity(new OctetString(community));
             target.setVersion(SnmpConstants.version1);
             target.setAddress(new UdpAddress(udpaddress+"/"+code));
             target.setRetries(1);
             target.setTimeout(time);
             long l=System.currentTimeMillis();
             int iii=0;
             snmp.listen();
             PDU request = new PDU();
             if(!is_getnext){
             if(is_get){
                 request.setType(PDU.GET);
                 request.add(new VariableBinding(new OID(OID)));
             }
             else{
                 request.setType(PDU.SET);
                 request.add(new VariableBinding(new OID(OID),new OctetString(get_byte(value))));
             }}
             else{
                 request.setType(PDU.GETNEXT);
                 request.add(new VariableBinding(new OID(OID)));
             }
             //request.setRequestID(new Integer32(-1111));
             //request.add(new VariableBinding(new OID(OID),new Integer32(Integer.parseInt("01111000011110000",2))));//这里应该添加索要设置的值
             System.out.println("发送的UDP:" + request);
             PDU response = null;
             ResponseEvent responseEvent = snmp.send(request, target);
             response = responseEvent.getResponse();
             if(response!=null){
                 if(response.getErrorStatus()==response.noError){
                     responseEvent.getResponse().getVariableBindings();
                     String pause=responseEvent.getResponse().getVariableBindings().toString();
                     /*getvalue的值为对应OID的值*/
                     String getvalue=pause.substring( pause.indexOf("= ")+2,pause.indexOf(']'));
                     String oid=pause.substring(pause.indexOf("VBS[")+2,pause.indexOf("=")-1);
                     System.out.println(oid+"::");
                     System.out.println(response);
                     if(!is_getnext)
                     if(is_get){
                         jf7.setText(change(getvalue));
                         jf5.setText("接收到的数据");
                     }
                     else{
                         jf5.setText("修改后的数据");
                         jf4.setText(change(getvalue));
                     }
                 else{
                     jf2.setText(oid);
                     jf7.setText(change(getvalue));
                     jf5.setText("接收到的数据");
                     System.out.println(ii);
                     ii++;
                 }
                 }
                 else{
                     jf8.setText(response.getErrorStatusText());
                     System.out.println(response.getErrorStatusText());
                     if(response.getErrorStatusText().equals("General variable binding error")){
                         //break;
                     }
                 }
             }
             else{
                 System.out.println("修改失败,设备连接超时");
                 jf5.setText("修改失败,连接设备超时");
             }
             iii=0;
         }
         catch(Exception e){e.printStackTrace();}
     }
     public String change(String ss){
         byte[] b=ss.getBytes();
         if(b.length==1){
             ss=String.valueOf(Integer.toHexString(b[0]));
         }
         return ss;
     }
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -