简体   繁体   中英

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

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM