gpt4 book ai didi

python - 期待 if else 语句

转载 作者:太空宇宙 更新时间:2023-11-04 06:24:23 31 4
gpt4 key购买 nike

我正在尝试创建一个脚本来清除 ciscoasa bad xlate。

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

我需要有关 if else 语句的帮助。否则,脚本工作正常。这是我的代码:

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

最佳答案

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

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

关于python - 期待 if else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9370886/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com