【发布时间】:2017-06-02 08:13:22
【问题描述】:
我正在使用 paramiko 进行 ssh 并等待正在检查字符串结尾的提示。
实际结尾的字符串如下:
RP/0/RSP0/CPU0:asr1#
我在代码中使用检查endswith是
#
下面是代码:
import paramiko
import re
import time
hostname = "10.10.10.10"
net_username = "user"
net_password = "password"
remote_conn_pre = paramiko.SSHClient()
remote_conn_pre.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
remote_conn_pre.connect(hostname, username=net_username, password=net_password,look_for_keys=False, allow_agent=False)
remote_conn = remote_conn_pre.invoke_shell()
buff = ''
while not buff.endswith('#'):
resp = remote_conn.recv(9999)
buff += resp
print(resp)
remote_conn.send("\n")
buff = ''
while not buff.endswith('#'):
resp = remote_conn.recv(9999)
buff += resp
print(resp)
remote_conn.send("ping 172.16.35.22\n")
time.sleep(2)
buff = ''
while not buff.endswith('#'):
resp = remote_conn.recv(9999)
buff += resp
print resp
即使我使用“#”检查结尾,一切正常,但我想在这里仔细检查。我这样做是正确的还是我们有其他更好的选择来实现这一目标
我的意思是结尾是字符串“RP/0/RSP0/CPU0:asr1#”。
"RP/" is constant
如何使用它来匹配
"RP/anything#"
【问题讨论】:
-
re.compile(r"(RP/[a-z]*#{1})")这不是最好的。但它可以帮助你
标签: python regex python-2.7 paramiko