#!/usr/bin/env python2 from subprocess import call, Popen, PIPE, STDOUT import paho.mqtt.client as mqtt import argparse MQTT_SERVER = 'mqtt.suanzi.ai' MQTT_PORT = 1883 URI = 'debug@autossh.suanzi.ai' def on_connect(client, userdata, flags, rc): id = client.id client.subscribe(id + "-response") print("Connected with result code "+str(rc)) client.publish(id + '-request', 0) def on_message(client, userdata, msg): id = client.id print(msg.topic+" "+str(msg.payload)) if msg.topic == id + '-response': port = msg.payload print 'run "ssh ' + URI + ' -p ' + str(port) + '" to connect to device (%s)' % id if __name__ == '__main__': parser = argparse.ArgumentParser(description='Choose an avaiable port and run ssh.') parser.add_argument('-i', '--id', dest='id', help='the unique id of remote machine, such as rpdzkj-080027a6f8dc, where the rpdzkj is the user, and 080027a6f8dc is the mac addres', required=True) #parser.add_argument('connect', help='list all connected devices') #parser.add_argument('list', help='list all connected devices') args = parser.parse_args() client = mqtt.Client() client.id = args.id client.on_connect = on_connect client.on_message = on_message client.connect(MQTT_SERVER, MQTT_PORT, 60) client.loop_forever()