【问题标题】:subprocess.Popen ssh tunnel launches 2nd process which requires answering a prompt with a pinsubprocess.Popen ssh 隧道启动第二个进程,该进程需要使用 pin 回答提示
【发布时间】:2019-03-15 03:35:45
【问题描述】:

我正在使用 subprocess.Popen 创建一个 ssh 隧道。然而,为了成功创建这个隧道,我使用了一个 Yubikey,它需要一个 pin 来释放成功验证的密钥,内置 ssh 配置。下面的代码是我能得到的。

def launch_tunnel(self):
    try:
        enterpin = getpass.getpass()
        bytepin = str.encode(enterpin)
        launchtunnel = subprocess.Popen('ssh tunnel command', 
                                        shell=True, 
                                        stdout=subprocess.PIPE, 
                                        stdin=subprocess.PIPE
                                        stderr=subprocess.PIPE).communicate(input=bytepin)
    except Exception as e:
        print(e)

当我运行它时,我得到以下 2 个提示。

Password:
Enter PIN for 'PIV_II (PIV Card Holder pin)':

第一个是 getpass.getpass(),第二个是另一个需要 Yubikey Pin 的进程。很明显 .communicate() 在这里不起作用,据我所知,这是因为 ssh 进程产生了另一个需要 ssh 身份验证的进程(pin 提示符)。

是否可以在使用 getpass 之类的方法之前设置引脚并将其直接传递给第二个进程。目前这第二个进程(pin 提示)正在中断我的应用程序的其余部分,所以我想控制它?

【问题讨论】:

    标签: python-3.x ssh subprocess popen tunnel


    【解决方案1】:

    我找到了部分解决方案。在 () 中没有任何内容的情况下使用通信()会在提示符处停止进程,因此我不需要在启动子进程之前实现 getpass。它给了我另一个问题,但我将把它带到一个新线程。

    launchtunnel = subprocess.Popen(checktunnelexists[1], shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
    launchtunnel.communicate()[0]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 2016-02-26
      • 2019-10-28
      • 2019-03-02
      • 2016-07-26
      • 1970-01-01
      • 2012-06-05
      相关资源
      最近更新 更多