Python - How do I authenticate SSH connection with Fabric module?

拥有回忆 提交于 2019-12-24 08:49:01

问题


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")  

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).

Thanks!


回答1:


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.

See http://docs.fabfile.org/en/2.1/concepts/authentication.html as well as http://docs.fabfile.org/en/2.1/concepts/configuration.html



来源:https://stackoverflow.com/questions/51237956/python-how-do-i-authenticate-ssh-connection-with-fabric-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!