LoginSignup
24
22

More than 5 years have passed since last update.

Windows標準機能だけでsnmpgetする(PowerShell)

Last updated at Posted at 2016-04-23

やりたいこと

Windowsで、Net-SNMP等SNMPのツールが入っていない環境でもsnmpgetのテスト
が出来ないかと思って調べていると「olePrn.OleSNMP」というCOMオブジェクト
を利用すると出来そうなことがわかりました。

 参考:スクリプト Network Device discovery via PowerShell (SCOM)

これを使って、複数の機器にsnmpgetしていき、sysnameがとれたかどうかの
結果一覧を取得をしてみます。
今回はPowerShellを使いますが、おそらくVBSでも可能だと思います。

コード

snmpgettest.ps1
# テストする対象のIPアドレス。ここでは配列で定義。
$targetip =@(
    "192.168.0.1",
    "192.168.0.2",
    "192.168.0.3",
    "192.168.0.4",
    "192.168.0.5"
)

# SNMPコミュニティ名
$community = "public"
$resluts = @()

foreach($ip in $targetip) {
    $snmp = new-object -ComObject olePrn.OleSNMP 
    # SNMP接続
    # open("IPアドレス","コミュニティ名","リトライ回数","タイムアウト")
    $snmp.open($ip, $community, 2, 1000) 

    try {
        # sysnameを取得
        $sysname = $snmp.Get(".1.3.6.1.2.1.1.5.0")
        $result  = $true
    } catch [Exception] {
        # 応答がない場合(IP不到達、コミュニティ名誤り、OID誤り)
        $sysname = $null
        $result  = $false
    } 

    $obj = New-Object PSObject -Property @{
        ip      = $ip;
        sysname = $sysname;
        result  = $result
    }
    $resluts += $obj
}

# 結果の表示
$resluts | Format-table -Property ip, sysname, result -AutoSize

結果例

結果例
ip          sysname  result
--          -------  ------
192.168.0.1 router1    True
192.168.0.2 router2    True
192.168.0.3           False
192.168.0.4 Server1    True
192.168.0.5 Server2    True

できました。

補足

SNMPのバージョンは v1 のようです。
Image2.png

24
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
22