Skip to content

Instantly share code, notes, and snippets.

@evan-burke
Created January 9, 2020 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evan-burke/06cdc710483f6e7f76a78d23e73f6d25 to your computer and use it in GitHub Desktop.
Save evan-burke/06cdc710483f6e7f76a78d23e73f6d25 to your computer and use it in GitHub Desktop.
ssh with Fabric using an SSH key
# Fabric can be used to run commands on a remote system over SSH.
# Sadly, its docuentation is a bit short for connecting using a private key file.
# Other options exist too, like this one using an SSH config file - https://gist.github.com/aubricus/5157931
import fabric
# Openssh formatted private key:
keyfile = "/path/to/your/privkey"
host = "fqdn"
user = "myname"
with fabric.Connection(host=host,
user=user,
connect_kwargs={
"key_filename": keyfile
}
) as sshconn:
res = sshconn.run('uname -s')
print(res.stdout)
# > Linux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment