def get_tunnel_port(keyfile, masterport, masteruser, masterhost): file = "%s/.pcm_tunnel_port" % os.environ['HOME'] tmptunnelport = None if os.path.exists(file): try: tmptunnelport = int(open(file).read().strip()) except: pass tunnelport = None while not tunnelport: # Pick a random tunnel port between 20,000-30,000 if tmptunnelport: tunnelport = tmptunnelport tmptunnelport = None else: tunnelport = 20000 + int(10000*random.random()) print "Testing port %s" % tunnelport data = check_output( "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null %s -p %s %s@%s 'netstat -lnp 2>/dev/null| grep %s'" % ( keyfile, masterport, masteruser, masterhost, tunnelport )) if data: tunnelport = None #Cache the tunnel, so we reuse the same port and minimze the need #to log about new tunnels fd = open(file, 'w') fd.write(str(tunnelport)) fd.close() return tunnelport
def get_key(): return check_output("ifconfig | grep HWaddr | md5sum")[:-2]