Pexpect if else 语句

发布于 2025-01-07 07:25:19 字数 1563 浏览 2 评论 0原文

我正在尝试创建一个清除 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

I am trying to create a script which clears ciscoasa bad xlate.

To check bad xlate I run sh xlate | in 500 and if it gets a reply then it should send a clear command. If I don't do it, ciscoasa won't allow new vpn tunnels.

I need help with the if else statement. Otherwise, the script is working fine. Here is my code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏了南城 2025-01-14 07:25:19

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

有关详细信息,请查看文档中的列出 if 模式部分。

I'd say that what you need is use multiple patterns in the call to child.expect before the if statement. This way you'll be able to make a decision based on the response output to the last command send to the terminal.

For more information please have look at the lists if patterns section in the documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文