TCL/EXPECT自动化测试脚本实例六 --- SNMP community长度测试

来源:互联网 发布:魔兽世界多玩数据库 编辑:程序博客网 时间:2024/04/30 00:01
下面通过一个测试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"
}
原创粉丝点击