5

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!

Geshode
  • 3,600
  • 6
  • 18
  • 32
Matt
  • 1,368
  • 1
  • 26
  • 54
  • There is a `--prompt-for-passphrase` option, but it doesn't appear to work for me. I'm just getting an AuthenticationException when I run your code. You could try using key authentication instead – Peter Gibson Jul 09 '18 at 04:08
  • @Peter Gibson is there not a way to input the password in plain text within the python script so that it autofills the password prompt without using key authentication? – Matt Jul 09 '18 at 04:10

1 Answers1

8

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

Peter Gibson
  • 19,086
  • 7
  • 60
  • 64
  • 1
    Brilliant - I've been trying to figure that out all day thank you! I'll take a look to try to understands how to log in more securely but this is a start. I'm just a beginner so this module may be a bit out of my depth to be honest. – Matt Jul 09 '18 at 04:21