2

ciscoasa の不良 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
4

1 に答える 1

1

必要なのは、ステートメントchild.expectの前の呼び出しで複数のパターンを使用することです。ifこのようにして、端末に送信された最後のコマンドに対する応答出力に基づいて決定を下すことができます。

詳細については、ドキュメントの「パターンの場合」セクションのリストを参照してください。

于 2012-02-22T12:00:49.577 に答える