subprocess.Popen ssh隧道启动第二个进程,该进程需要用大头针回答提示

时间:2018-10-10 09:08:57

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

我正在使用subprocess.Popen创建一个ssh隧道。但是,要成功创建此隧道,我使用的是Yubikey,它需要一个引脚来释放成功进行身份验证的密钥,该密钥是ssh config内置的。下面的代码是我所能得到的。

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)

当我运行它时,出现以下两个提示。

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

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

无论如何,在使用诸如getpass之类的设置之前先设置引脚,然后将其直接传递给第二个进程。当前,第二个进程(pin提示)正在中断我的应用程序的其余部分,因此我想控制它?

1 个答案:

答案 0 :(得分:0)

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

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