简体   繁体   English

从python切换到另一台机器时转移标准输入

[英]diverting the stdin when ssh-ing to another machine from python

I am trying to use SSH as a socks proxy to another machine, then ask the user if he wants to proceed. 我试图将SSH用作另一台计算机的袜子代理,然后询问用户是否要继续。 so I use: 所以我用:

proxy_cmd = "ssh -o 'StrictHostKeyChecking no' -i " + key_filename + ' -D 9998 ubuntu@' + ip_address    
subprocess.Popen(proxy_cmd, shell=True,  stdout=subprocess.PIPE)

if not raw_input('would you like to proceed?(y)')=='y':
    sys.exit()

and I get: 我得到:

IOError: [Errno 11] Resource temporarily unavailable

I assume that's because the ssh is open and it is capturing stdin or something. 我认为那是因为ssh已打开并且正在捕获stdin或其他东西。 I just don't know how to bypass this (I have no need to send input to the ssh, I just want it open for Selenium to use later) How can I do this? 我只是不知道如何绕过此操作(我不需要将输入发送到ssh,我只希望它开放供Selenium稍后使用)我该怎么做?

If you want to keep stdin available to your Python program, then you'll have to redirect stdin for the ssh process even if you have no intention of using it, with something like... 如果您想让stdin可用于您的Python程序,那么即使您无意使用它,也必须将stdin重定向到ssh进程,例如...

subprocess.Popen(proxy_cmd,
                 shell=True,
                 stdin=subprocess.PIPE,
                 stdout=subprocess.PIPE)

Note that the subprocess module will retain a reference to the Popen object in the subprocess._active list, but you may also want to bind the resulting Popen object to a variable so you can perform operations on it later. 需要注意的是, subprocess模块将保留到参考Popen在对象subprocess._active名单,但你也可能要产生的绑定Popen对象变量这样你就可以在以后的操作。

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

相关问题 从python执行scp时出错 - error when exec'ing scp from python Python-从列表中提取最低整数,然后将其“弹出”到另一个 - Python - Pulling lowest integer from a list and then “pop”ing it to another 在python中从另一个字典的一个子对象弱引用一个项目 - Weakref-ing an item from one dict in a subdict of another in python 从另一个进程(PHP)流式传输时,从 Python 中的标准输入读取速度非常慢 - Very slow reading from stdin in Python when streamed from another process (PHP) Python:从stdin读取时的UnicodeEncodeError - Python: UnicodeEncodeError when reading from stdin Python:从标准输入读取时的vi模式 - Python : vi mode when reading from stdin “在没有paramiko的情况下通过python运行ssh时,不会分配伪终端,因为stdin不是终端” - “Pseudo-terminal will not be allocated because stdin is not a terminal” when running ssh through python without paramiko 在没有SSH的情况下从远程计算机触发python脚本 - Trigger a python script from a remote machine without SSH 从Python进行Stdin重定向 - Stdin redirection from Python 如何在一台机器上运行Python代码并通过SSH访问另一台机器的文件? - How to run Python codes on one machine and access files of another machine via SSH?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM