pythonpromptpxssh

Regexp to match prompt in pxssh


I am using pxssh in Python3.6 to play with SSH by doing a script.

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

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 (...) #

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.

I know that in Ruby I could do something like this

(Something \(.+\)# )

Is Python and/or pxssh supporting this ?


Solution

  • Well it seems a simple :

    prompt = r'(Something .+ # )'
    

    instead of :

    prompt = 'Something # '
    

    is enough.

    Should have tried it earlier.. :)