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

王朝other·作者佚名  2007-02-23
窄屏简体版  字體: |||超大  

下面通过一个测试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"

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航