简体   繁体   English

在两个不同的端口上两次使用pexpect pxssh

[英]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. 我正在尝试使用pxssh通过SSH连接到远程设备,但是这些设备之一是SSH端口2222,一个是22。我知道我可以将pxssh.py中的硬编码值更改为22或2222,但是我不能知道我可以同时做这两个事情。

I looked at the pxssh login() function and tried putting 'port' after password, so when calling pxssh I could specify the port required eg 我查看了pxssh login()函数,并尝试将“端口”放在密码后面,因此在调用pxssh时,我可以指定所需的端口,例如

pxssh.py file pxssh.py文件

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 从我的文件中调用pxssh登录

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 然后我以为我可以复制pxssh并拥有pxssh22.py和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 我如何使用pxssh来访问端口2222和22。谢谢

You can provide port as an optional parameter when calling the login method. 您可以在调用login方法时将port作为可选参数提供。

  s = pxssh.pxssh()
  s.login(server, username, password, port=2222)

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. 由于没有其他人有任何想法,我通过移动pxssh.py文件中的port参数,然后重新排列s.login函数调用以包含port来解决此问题。

modified pxssh.py file 修改后的pxssh.py文件

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)

According to documentation , we can simply use default arguments and port of our will. 根据文档 ,我们可以简单地使用默认参数和遗嘱端口。

host = 127.0.0.1
user = root
password = toor

s.login(host,user,password, terminal_type='ansi', original_prompt='[#$]', login_timeout=10, port=2022)

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

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