pythonsshpxssh

using pexpect pxssh twice on two different ports


I am trying to connect via SSH using pxssh to a remote device, however one of these devices is SSH port 2222 and one is 22. I know I can change the hardcoded value in pxssh.py to either 22 or 2222 but I don't know how I can do both at the same time.

I looked at the pxssh login() function and tried putting 'port' after password, so when calling pxssh I could specify the port required e.g

pxssh.py file

def login (self, server, username, password='', port, terminal_type='ansi',original
            _prompt=r"[#$]", login_timeout=10,
            auto_prompt_reset=True, ssh_key=None, quiet=True,
            sync_multiplier=1, check_local_ip=True):

calling pxssh login from my file

s.login(server, username, password, port) 

however that error'ed with

SyntaxError: non-default argument follows default argument

I then thought I could duplicate pxssh and have pxssh22.py and pxssh2222.py

if something:
     from pexpect import pxssh22
else:
     from pexpect import pxssh2222

each specifying a different hard coded port number however that kept erroring with when called:

try:
    s = pxssh2222.pxssh()
    s.login(server, username, password)

except pxssh2222.ExceptionPxssh as e:
NameError: global name 'pxssh2222' is not defined

How could I use pxssh to access both port 2222 and 22. Thanks


Solution

  • As no one else has any ideas, I fixed this by moving the port argument in the pxssh.py file and then rearranging the s.login function call to include port.

    modified pxssh.py file

    def login (self, server, username, port, password='', terminal_type='ansi',
                original_prompt=r"[#$]", login_timeout=10,
                auto_prompt_reset=True, ssh_key=None, quiet=True,
                sync_multiplier=1, check_local_ip=True):
    

    modified login call to allow for port argument.

    s.login(sshIP, "root", sshPort, sshPass)