首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pexpect if else语句

Pexpect if else语句
EN

Stack Overflow用户
提问于 2012-02-21 09:23:21
回答 1查看 6.9K关注 0票数 2

我正在尝试创建一个脚本,清除ciscoasa错误的xlate。

为了检查错误的xlate,我在500中运行sh xlate |,如果它得到一个响应,那么它应该发送一个清除命令。如果我不这么做,ciscoasa将不允许新的vpn隧道。

我需要有关if else语句的帮助。除此之外,脚本运行得很好。下面是我的代码:

代码语言:javascript
复制
import pexpect
import re

password1="abc"
password2="abc"
router="127.0.0.20"
user="user"


#Extracting IP out of xlate command
class sendip(object):
    def ip(self,reply):
    divide=reply[15:32]
    extract_ip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', divide )
    for ip in extract_ip:
      send = 'clear local-host '+ip
      return send
clearVPN = sendip()

#ssh into ciscoasa userop5@ip
child = pexpect.spawn ('ssh ' + user + '@' + router)
child.expect ('.*assword:.*')
child.sendline (password1)

#enable mode
child.expect ('.*>.*')
child.sendline ('ena')
child.expect ('.*assword:.*')
child.sendline (password2)

# after enabling send test command
child.sendline ('terminal pager 0')
child.sendline ('show run | include http')
child.expect ('enroll*')
print child.before

# Here it sends command
child.sendline('sh xlate | i 500')
child.expect ('ciscoasa#.*')
print child.after

if child==1:  # If receive reply then extract IP and send clear memory
    child.expect('UDP.*')
    message = child.before
    child.sendline(clearVPN.ip(message)) #extract ip from there reply and send clear
    print child.before
    child.expect('clearing.*')
    print child.before
else:         # Otherwise memory is not full
    child.expect ('ciscoasa#.*')
    print 'Memory is empty'
    print child.after

child.sendline('exit')
child.close() # close ssh
EN

回答 1

Stack Overflow用户

发布于 2012-02-22 20:00:50

我要说的是,您需要在if语句之前在对child.expect的调用中使用多个模式。这样,您将能够根据对发送到终端的最后一个命令的响应输出做出决定。

有关更多信息,请查看文档中的lists if patterns部分。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9370886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档