Created
April 5, 2013 10:30
-
-
Save blackraccoon000/5318276 to your computer and use it in GitHub Desktop.
はだかの隊長日記様より拝借
http://blog.r-unit.co.jp/archives/274 2013/04/05
ローカル環境でループバックアドレスを用いて使用できる事を確認
snmp4jとmibbleのjarファイル等が必要
詳細は参考URLより
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package snmpfirst; | |
| /** | |
| * Created with IntelliJ IDEA. | |
| * User: Yutaka Fujii | |
| * Date: 13/04/05 | |
| * Time: 11:52 | |
| * To change this template use File | Settings | File Templates. | |
| */ | |
| import net.percederberg.mibble.Mib; | |
| import net.percederberg.mibble.MibLoader; | |
| import net.percederberg.mibble.MibValueSymbol; | |
| import org.snmp4j.CommunityTarget; | |
| import org.snmp4j.PDU; | |
| import org.snmp4j.Snmp; | |
| import org.snmp4j.TransportMapping; | |
| import org.snmp4j.event.ResponseEvent; | |
| import org.snmp4j.mp.SnmpConstants; | |
| import org.snmp4j.smi.*; | |
| import org.snmp4j.transport.DefaultUdpTransportMapping; | |
| import java.util.Vector; | |
| public class SnmpGet { | |
| public static void main(String[] args) { | |
| try { | |
| // SNMPエージェントを設定 | |
| Address targetAddress = GenericAddress.parse("udp:"+"127.0.0.1"+"/161"); | |
| TransportMapping transport = new DefaultUdpTransportMapping(); | |
| Snmp snmp = new Snmp(transport); | |
| transport.listen(); | |
| // ターゲットを設定(Snmp4J) | |
| CommunityTarget target = new CommunityTarget(); | |
| target.setCommunity(new OctetString("public")); | |
| target.setAddress(targetAddress); | |
| target.setRetries(3); | |
| target.setTimeout(1000 * 20); | |
| target.setVersion(SnmpConstants.version1); | |
| System.out.println(target); | |
| // SNMPv2 のMIBをロード(mibble) | |
| MibLoader mibLoader = new MibLoader(); | |
| Mib mib = mibLoader.load("SNMPv2-MIB"); | |
| PDU pdu = new PDU(); | |
| // OIDを指定 | |
| // Sysname | |
| pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"))); | |
| // SysDescr | |
| pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.1.0"))); | |
| // sysLocation | |
| pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.6.0"))); | |
| // エージェントからの戻りを解析 | |
| ResponseEvent respEvnt = snmp.get(pdu, target); | |
| System.out.println("snmp send:" + respEvnt.getRequest()); | |
| if (respEvnt != null && respEvnt.getResponse() != null) { | |
| Vector recVBs = respEvnt.getResponse().getVariableBindings(); | |
| // この設定のまま行うと『3』 | |
| // System.out.println("size:"+recVBs.size()); | |
| for (int i = 0; i < recVBs.size(); i++) { | |
| VariableBinding recVB = (VariableBinding) recVBs.elementAt(i); | |
| MibValueSymbol vSynbol = mib.getSymbolByOid(recVB.getOid().toString()); | |
| // System.out.println("get_OID:"+recVB.getOid()); | |
| System.out.println(vSynbol.getName()+":"+recVB.getVariable()); | |
| } | |
| } | |
| // System.out.println("snmp end"); | |
| snmp.close(); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment