Search code examples
pythonsshraspberry-piparamikofabric

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!


Solution

  • 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