Python Fabric 和密码提示

标签 python ssh 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 连接,而 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/

相关文章:

linux - 程序仍在运行时如何查看 nohup 文件?

linux - 将我自己的 Git 服务器与我的 Windows Eclipse IDE 链接起来

ubuntu - 在 Fabric 脚本中调用 bash 别名

python - 如何让Fabric在得到退出状态: 1?后继续运行下一条命令

Python basemap "Assertion failed"

python - 在 Python 中计算概率

python - pandas 日期范围意外行为

git - mvn release 使用特定的私钥推送到 git

python - 无法使用fabric下载文件

python - 如何显示 PuLP 中定义的(原始)线性程序的对偶