找到了SNMP4J的开发包但是不会用
那位大哥能给个例子指导一下
谢谢了
package mst_lcd;
import org.snmp4j.*;
import org.snmp4j.mp.*;
import org.snmp4j.smi.*;
import org.snmp4j.transport.*;
import org.snmp4j.event.ResponseEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Check_for_machine_test extends JFrame{
String[][] machine_oid;
private long time0;
private CommunityTarget target;
private OctetString community;
JPanel contentPane=(JPanel)getContentPane();
JLabel jl1=new JLabel("IP:");
JLabel jl2=new JLabel("OID:");
JLabel j15=new JLabel("Port:");
JLabel jl3=new JLabel("Set Value:");
JLabel jl4=new JLabel("Get Value:");
JTextField jf1=new JTextField("159.226.220.31");
JTextField jf2=new JTextField("1.3.6.1.4.1.25449.30.1.2.0");
JTextField jf6=new JTextField("10001");
JTextField jf3=new JTextField();
JTextField jf4=new JTextField();
JTextField jf5=new JTextField();
JTextField jf7=new JTextField();
JButton set=new JButton("SET");
JButton get=new JButton("GET");
JButton getnext=new JButton("GETNEXT");
JTextField jf8=new JTextField();
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));
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){
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);
iii=1;
long ll=System.currentTimeMillis()-l;
System.out.println("所用时间<=>:"+ll+" "+iii);
//jf5.setText("发送请求...");
response = responseEvent.getResponse();
ll=System.currentTimeMillis()-l;
if(response!=null){
if(response.getErrorStatus()==response.noError){
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("接收到的数据");
}
}
else{
jf8.setText(response.getErrorStatusText());
System.out.println(response.getErrorStatusText());}
}
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;
}
}
有没有用过SNMP4J的
看看这个程序阿
给点建议撒