Tuesday, April 15, 2008

pyssh

it's not that i can't remember flags to a command line application, but it just takes too long to type everything. thus, i have made an attempt to shorten the typing time taken to ssh into a box, whether or not it requires an ssh key. here, i will share with you my python ssh wrapper. how it works: basically, the only command line argument you give it is the location of your ssh key (if needed). it asks for username and hostname; anything else would take too much time. enjoy!


#!/usr/bin/python
import os, sys
run=os.system

# process cli args
try:
keyfile = sys.argv[1]
usekey = 0
except:
print "no arguments given, assuming no key needed!"
usekey = 1

# request username and hostname
try:
user=str(raw_input('user: '))
host=str(raw_input('host: '))
except EOFError:
print r" "
print r"caught eof"
sys.exit(122)
except KeyboardInterrupt:
print r" "
print r"caught interrupt"
sys.exit(122)

# combine arguments and flags
if usekey == 0:
sshargs=" -i "+keyfile+" -l "+user+" "+host

if usekey == 1:
sshargs=" -l "+user+" "+host

# in a shell, run ssh and fg /w ssh args
run('/usr/bin/ssh'+sshargs)
sys.exit(0)