【问题标题】:Regexp to match prompt in pxssh正则表达式匹配 pxssh 中的提示
【发布时间】:2018-09-07 06:37:21
【问题描述】:

我在Python3.6 中使用pxssh 通过编写脚本来玩SSH。

一切正常,但我只有一个小问题。

我正在登录 SSH 的机器上的提示会根据我发送的一些命令而改变(不是全部!)

这是我的脚本代码

from pexpect import pxssh
from codecs import encode

ip = xxx.xxx.xxx.xxx
username = 'user'
password = 'pass'
prompt = 'Something # '

s = pxssh.pxssh()

def send_cmd(s, cmd):
    "A simple generic function to send a command via SSH and printing it's result"
    s.sendline(cmd)
    s.prompt(timeout=1)
    print('*'*20)
    print((s.before).decode("utf-8"))
    return

if not s.login (ip, username, password, auto_prompt_reset=False):
    print('SSH session failed on login')
    print(str(s))
else:
    print('SSH session login successful')
    s.PROMPT = prompt
    send_cmd(s, 'commands')
    send_cmd(s, 'end')
    s.logout()
    print('Logged out of SSH session')

以下是一些提示示例: Something # Something (toto) # Something (tata) # Something (...) #

所以我想知道是否可以做一个正则表达式来匹配这个,这样当我调用s.before 时,我不会得到提示。

我知道在 Ruby 中我可以做这样的事情

(Something \(.+\)# )

Python 和/或 pxssh 是否支持此功能?

【问题讨论】:

    标签: python prompt pxssh


    【解决方案1】:

    嗯,看起来很简单:

    prompt = r'(Something .+ # )'
    

    而不是:

    prompt = 'Something # '
    

    够了。

    应该早点尝试过.. :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-08
      相关资源
      最近更新 更多