SSH Connection Script

Ever have a shitty environment where you have tons of devices that you need to connect to and don’t feel like typing out (and invariably mis-typing the hostname)?  Then this dirt simple script might be what you are looking for.

#!/usr/bin/env python
#
# requires file 'hosts' to exist in same directory as the script
# can accept either ip addresses or hostnames in this file

import os
import pwd

user = pwd.getpwuid( os.getuid() )[ 0 ]

f = open('hosts', 'r')

hosts = f.readlines()

id=0

print "=== SSH Console v1 ===nn"
for host in hosts:
        print str(id)+": "+host.rstrip("n")
        id += 1 

print "n"
choice = raw_input('Host >')
choice = int(choice)

os.system("ssh "+user+"@"+hosts[choice])

Just make sure the host file exists and has a hostname on each line rather than smashed together with some delimiter.  Its not elegant, has zero configuration but so far folks at work have found it somewhat handy to save some time, maybe you will too.

Close Bitnami banner
Bitnami