SSH for Python – In search of API perfection
My mission is simple: Establish an SSH connection to a device and run some commands in as few lines as possible. The contenders? Paramiko, Spur and Fabric.
The Scenario
I have a network device, 192.168.1.254.
I want to log in via SSH with a username of dave and password of p@ssword123.
Once logged in, I want to execute the command display version and print the result.
Now to the code...
The Code
Paramiko
Paramiko is the go to SSH library in Python. Let's see how it shapes up in the simple scenario:
import paramiko
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
client.connect("192.168.1.254", username="dave", password="p@ssword123")
stdin, stdout, stderr = client.exec_command('display version')
for line in stdout:
print line.strip('n')
client.close()
8 lines of code. The API here is very powerful, but requires me to put up some scaffolding code (Key Management) before I actually get around to connecting an executing my command. That said, it gets the job done.
Spur
Spur is a wrapper around Continue reading








