简体   繁体   English

正则表达式以匹配pxssh中的提示

[英]Regexp to match prompt in pxssh

I am using pxssh in Python3.6 to play with SSH by doing a script. 我使用pxsshPython3.6做一个脚本SSH玩。

Everything works fine but I just have one little issue. 一切正常,但我只有一个小问题。

The prompt on the machine I'm logging on SSH change according to some commands I'm sending (not all!) 我正在登录SSH的计算机上的提示会根据我发送的某些命令(不是全部)更改。

Here is the code of my script 这是我的脚本代码

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')

Here are some examples of prompts : Something # Something (toto) # Something (tata) # Something (...) # 以下是一些提示示例: Something # Something (toto) # Something (tata) # Something (...) #

So I was wondering if it was possible to do a regexp to match this so that when I call s.before , I won't get the prompt. 所以我想知道是否可以做一个正则表达式来匹配它,以便当我调用s.before ,我不会得到提示。

I know that in Ruby I could do something like this 我知道在Ruby中我可以做这样的事情

(Something \\(.+\\)# )

Is Python and/or pxssh supporting this ? Python和/或pxssh支持吗?

Well it seems a simple : 好吧,这似乎很简单:

prompt = r'(Something .+ # )'

instead of : 代替 :

prompt = 'Something # '

is enough. 足够。

Should have tried it earlier.. :) 应该早点尝试过.. :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM