Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19881492
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: Python/Ruby

2011-05-31 16:05:18

中使用Pexpect进行命令行自动化测试

2011-05-31 磁针石

#
承接软件自动化实施与培训等gtalk ouyangchongwu#gmail.com qq 37391319 博客:testing.blog.chinaunix.net

#版权所有,转载刊登请来函联系

#自动化测试和python群组:

#python qq group: 深圳自动化测试python群:113938272

#武冈深圳qq群:66250781

Pexpect 是一个用来启动子程序并对其进行自动控制的 Python 模块。 Pexpect 可以用来和像 sshftppasswdtelnet 等命令行程序进行自动交互

这里演示了pexpect测试某通信产品的实例,更多的pexpect介绍,请参见IBM的介绍文章: 以及

下面实例为针对CLI的操作脚本:

#!/usr/bin/env python

import pexpect,time,sys

 

def showInfo(child,command):

    child.send("home\r")

    child.expect ('.*-->.*')

    child.send("network-element ne-1\r")

    child.expect ('.*-->.*')

    child.send(command + "\r")

    child.expect ('.*-->.*')

    child.send("admin-state management\r")

    child.expect ('.*-->.*')

    time.sleep(5)

    child.send("reboot warm\r")

    print child.after

    child.expect ('.*-->.*')

    print child.after

 

ssh_newkey = 'Are you sure you want to continue connecting'

child = pexpect.spawn('ssh root@'+sys.argv[1])

i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password:'])

# timeout

if i == 0: # Timeout

    print 'ERROR!'

    print 'SSH could not login. Here is what SSH said:'

    print child.before, child.after

# Add public key

if i == 1: # SSH does not have the public key. Just accept it.

    child.sendline ('yes')

    print child.after

    child.expect ('.*word: ')

# enter password.

print child.before

child.send("ChgMeNOW\r")

print child.after

child.expect ('.*-->.*')

 

showInfo(child,"configure stu stu-1-1-1")

showInfo(child,"configure xg-1x xg_1x-1-1-24")

showInfo(child,"configure ge-10s ge_10s-1-1-16")

showInfo(child,"configure swf140g swf140g-1-1-1")

showInfo(child,"configure nemi nemi-1-1-1 ")

 

child.interact()

 

下面实例为针对各个板卡(linux系统)操作的脚本:

 

#!/usr/bin/env python

import pexpect,time,sys,re

 

def ssh2(child):

    ssh_newkey = 'Are you sure you want to continue connecting'

    i = child.expect([pexpect.TIMEOUT, ssh_newkey, '] #'])

    # timeout

    if i == 0: # Timeout

        print 'ERROR!'

        print 'SSH could not login. Here is what SSH said:'

        print child.before, child.after

    # Add public key

    if i == 1: # SSH does not have the public key. Just accept it.

        child.sendline ('yes')

        print child.after

    if i ==2:

        child.send("\r")

    return child

 

child = pexpect.spawn('ssh -p33 root@' + sys.argv[1])

child = ssh2(child)

child.expect ('] #')

child.send("arp | grep 4081 | awk '{print $1}'\r")

child.expect ('] #')

ips = re.findall("\d+.\d+.\d+.\d+",child.before)

print '*'*20 + "ips" + '*'*20

print ips

 

for ip in ips:

    child.send( "ssh " + ip + "\r")

    child = ssh2(child)

    child.expect ('] #')

    print child.before

    child.send(sys.argv[2] + "\r")

    child.expect ('] #')

    print child.before

    child.send("exit\r")

    child.expect ('] #')

 

child.send(sys.argv[2] + "\r")

child.expect ('] #')

print child.before

child.send("\r")

child.interact()

阅读(7275) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~