Python Fabric和密码提示

我看到有关于结构和密码的一些问题.我知道如果我将-I传递给fabric,那么我输入的密码将被传递给环境变量“password”.问题是我在远程服务器上运行ssh命令到另一台远程服务器时提示输入密码.

但是,我不希望提示输入密码.无论我尝试做什么,我都会被提示.所以这里有一小段代码:

elif "test" in run('hostname -d'):
                print(blue("Gathering Knife info"))
                run("ssh mychefserver knife node show `hostname`.test.dmz")

我输入密码时工作正常.问题是,我不想输入我的密码.也许这是因为在远程主机上启动了另一个ssh连接,并且结构无法对此做任何事情.

我可以让脚本与远程主机断开连接,在本地运行ssh命令,然后重新连接到远程主机以完成脚本……但这看起来很愚蠢.建议?

Getpass信息:

Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23) 
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from getpass import getpass
>>> getpass('test: ')
test: 
'This is a test'

最佳答案

from subprocess import Popen, PIPE
from getpass import getpass

x = Popen('ssh root@host', stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)

print x.stdout.readline()
_pass = getpass('Enter your superduper password:')
x.stdin.write(_pass)
print x.stdout.readline()

一旦连接,您仍然可以通过x.stdin.write(…)输入内容,就好像您在另一台机器上一样,那么,这应该有用吗?

DEBUG(只需启动一个cmd promt,导航到你的python目录并编写Python):

C:\Users>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> from getpass import getpass
>>> getpass('test: ')
test:
'This is a test'
>>>
点赞