TCL/EXPECT自動化測試腳本實例六 --- SNMP community長度測試

發表于:2009-06-24來源:作者:點擊數: 標簽:自動化SNMPTCLEXPECTcommunity
本文通過一個 測試 SNMP community最大長度的腳本,介紹 自動測試 SNMP agent的方法。 下面通過一個測試SNMP community最大長度的腳本,介紹一下net-snmp工具。 net-snmp是一組基于命令行的snmp manager工具,可以在命令行下進行snmp get, snmp set, snmp wal
本文通過一個測試SNMP community最大長度的腳本,介紹自動測試SNMP agent的方法。

    下面通過一個測試SNMP community最大長度的腳本,介紹一下net-snmp工具。

    net-snmp是一組基于命令行的snmp manager工具,可以在命令行下進行snmp get, snmp set, snmp walk等操作,支持snmp v1/v2c/v3。原來的名字叫做ucd-snmp,也已經被移植到windows NT上。
它的主頁在http://net-snmp.sourceforge.net/

    由于它可以在命令行下進行SNMP操作,所以可以和TCL/expect很好的結合,完成自動化測試的功能。
下面的腳本(snmp.exp),不斷的增加SNMP community,長度從1到256,每增加一個community,就調用snmp-get來進行SNMP get操作,如果get成功,說明此community有效;反之,就說明community已經超出了設備支持的最大長度。
這個腳本使用前面講到的test.exp調用,調用方法是:
./test.exp -ssnmp.exp script
對它稍加修改,也可以直接在命令行中調用,此處不再贅述。

代碼如下:
# $Id$

proc snmpCommTest {comm} {
    global g_devip

    spawn snmpget -c $comm -v 2c -r 2 $g_devip system.sysUpTime.0
    expect {
        "system.sysUpTime.0*" {
            return 1
        }
        "*Timeout*" {
            return 0
        }
    }

    return 1
}

set spawn_id [login $g_devip $g_user $g_passwd]
if {$spawn_id == 0} {
    errLog "login error\n"
    return 0
}

set cmdCommAdd "create snmp community %s rw\n"
set cmdCommDel "delete snmp community %s\n"
set cmdHostAdd "create snmp host ip 192.168.1.2 community %s\n"
set cmdHostDel "delete snmp host ip 192.168.1.2 community %s\n"
set comm ""

for {set i 1} {$i < 256} {incr i} {
    set comm "a$comm"
    set cmd [format $cmdCommAdd $comm]
    exp_send $cmd
    expect {
        "Error*" {
            errLog "create comm len $i error"
            continue
        }
        timeout {
            errLog "create comm len $i timeout"
            continue
        }
        "Entry Created"
    }
    set cmd [format $cmdHostAdd $comm]
    exp_send $cmd
    expect {
        "Error*" {
            errLog "create host error"
            continue
        }
        timeout {
            errLog "create host timeout"
            continue
        }
        "Entry Created"
    }

    set rc [snmpCommTest $comm]
    if {$rc == 0} {
        errLog "community len $i failed"
    }

    set cmd [format $cmdHostDel $comm]
    exp_send $cmd
    expect "Entry Deleted"
    set cmd [format $cmdCommDel $comm]
    exp_send $cmd
    expect "Entry Deleted"

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97