将 SSH 密钥文件与 Fabric 结合使用
如何配置结构以使用 SSH 密钥文件连接到远程主机(例如 Amazon EC2 实例)?
How do you configure fabric to connect to remote hosts using SSH keyfiles (for example, Amazon EC2 instances)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
由于某种原因,找到一个带有 SSH 密钥文件使用示例的简单 fabfile 并不容易。我写了一篇关于它的博客文章(具有匹配的要点)。
基本上,用法是这样的:
重要的部分是设置 env.key_filename 环境变量,以便 Paramiko 配置可以在连接时查找它。
Finding a simple fabfile with a working example of SSH keyfile usage isn't easy for some reason. I wrote a blog post about it (with a matching gist).
Basically, the usage goes something like this:
The important part is setting the
env.key_filename
environment variable, so that the Paramiko configuration can look for it when connecting.这里还值得一提的是,您可以使用命令行参数来执行此操作:
Also worth mentioning here that you can use the command line args for this:
Fabric 1.4 提供的另一个很酷的功能 - Fabric 现在支持 SSH配置。
如果您的
~/.ssh/config
文件中已包含所有 SSH 连接参数,Fabric 将原生支持它,您所需要做的就是在 fabfile 的开头添加: 。
Another cool feature available as of Fabric 1.4 - Fabric now supports SSH configs.
If you already have all the SSH connection parameters in your
~/.ssh/config
file, Fabric will natively support it, all you need to do is add:at the beginning of your fabfile.
对于 fabfile 中的 fabric2,请使用以下命令:
并运行它:
更新:
对于多个主机(一台主机也可以),您可以使用以下命令:
并使用 fab 或 fab2 运行它:
For fabric2 in fabfile use the following:
and run it with:
UPDATE:
For multiple hosts (one host will do also) you can use this:
and run it with fab or fab2:
对我来说,以下内容不起作用:
或
但是,以下内容有效:
或
For me, the following didn't work:
or
However, the following did:
or
我今天必须这样做,我的 .py 文件尽可能简单,就像 @YuvalAdam 的答案中发布的文件一样,但我仍然不断收到输入密码的提示......
看看
paramiko
(fabric 用于 ssh 的库)日志,我发现了这一行:我更新了
paramiko
:现在它可以工作了。
I had to do this today, my .py file was as simple as possible, like the one posted in the answer of @YuvalAdam but still I kept getting prompted for a password...
Looking at the
paramiko
(the library used by fabric for ssh) log, I found the line:I updated
paramiko
with:And now it's working.
这些答案在 py3.7、fabric2.5.0 和 paramiko 2.7.1 上都不适用于我。
但是,在文档中使用 PKey 属性确实有效: http://docs.fabfile.org/en/2.5/concepts/authentication.html#private-key-objects
None of these answers worked for me on py3.7, fabric2.5.0 and paramiko 2.7.1.
However, using the PKey attribute in the documentation does work: http://docs.fabfile.org/en/2.5/concepts/authentication.html#private-key-objects
如上所述,Fabric 将支持 .ssh/config 文件设置,但对 ec2 使用 pem 文件似乎有问题。 IOW 正确设置的 .ssh/config 文件将通过“ssh servername”从命令行工作,并且当 env.host=['servername'] 时无法使用“fab sometask”。
通过在 fabfile.py 中指定 env.key_filename='keyfile' 并复制 .ssh/config 中已有的 IdentityFile 条目,可以克服这个问题。
这可以是 Fabric 或 paramiko,在我的例子中是 Fabric 1.5.3 和 Paramiko 1.9.0。
As stated above, Fabric will support .ssh/config file settings after a fashion, but using a pem file for ec2 seems to be problematic. IOW a properly setup .ssh/config file will work from the command line via 'ssh servername' and fail to work with 'fab sometask' when env.host=['servername'].
This was overcome by specifying the env.key_filename='keyfile' in my fabfile.py and duplicating the IdentityFile entry already in my .ssh/config.
This could be either Fabric or paramiko, which in my case was Fabric 1.5.3 and Paramiko 1.9.0.