Python-如何验证与Fabric模块的SSH连接? [英] Python - How do I authenticate SSH connection with Fabric module?
问题描述
我正在尝试使用Fabric模块通过以太网通过SSH SSH到子网上的Raspberry Pi中,但我不知道如何对连接进行身份验证.
I'm trying to SSH into a Raspberry Pi on a subnet via ethernet using the Fabric module but I can't figure out how to authenticate the connection.
到目前为止,我的代码如下
My code so far is as follows
import fabric
c = fabric.Connection(host = "192.168.3.151", port = 22, user = "pi")
c.run("touch Desktop/new_file.txt")
很显然,在上述代码中的任何地方都没有输入密码"Raspberry"来验证SSH连接.我一直在尝试了解Fabric文档,但是它超出了我的范围,所以我希望有人可以告诉我如何输入密码来验证连接(以及使用sudo
来验证任何命令).
Obviously I haven't put in my password, "Raspberry", anywhere in the above code to authenticate the SSH connection. I've been trying to understand the Fabric documentation but it's a little beyond me so I'm hoping someone can tell me how to input the password to authenticate the connection (and also authenticate any commands using sudo
).
谢谢!
推荐答案
好吧,看来您可以将选项传递给Connection
构造函数,然后再传递给SSHClient.connect
Okay, it looks like you can pass options to the Connection
constructor that will be passed on to SSHClient.connect
c = fabric.Connection("192.168.3.151", port=22, user="pi", connect_kwargs={'password': 'raspberry'})
请注意,通常以纯文本(尤其是代码)存储密码是个坏主意.
Note it's generally a bad idea to store your passwords in plain text, especially in code.
请参见 http://docs.fabfile.org/en/2.1/concepts /authentication.html 以及 http://docs.fabfile. org/en/2.1/concepts/configuration.html
这篇关于Python-如何验证与Fabric模块的SSH连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!