我看到有几个关于结构和密码的问题。我知道如果我将 -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 连接,而 fabric 对此无能为力。
我可以让脚本与远程主机断开连接,在本地运行 ssh 命令,然后重新连接到远程主机以完成脚本……但这看起来很愚蠢。有什么建议吗?
获取通行证信息:
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'
>>>
关于Python Fabric 和密码提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16529037/