3 from subprocess import call, Popen, PIPE, STDOUT
4 import paho.mqtt.client as mqtt
7 MQTT_SERVER = 'mqtt.suanzi.ai'
10 if __name__ == '__main__':
11 parser = argparse.ArgumentParser(description='Choose an avaiable port and run autossh.')
12 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)
13 args = parser.parse_args()
16 def on_connect(client, userdata, flags, rc):
17 client.subscribe(id + "-response")
18 print("Connected with result code "+str(rc))
19 client.publish(id + '-request', 0)
21 def on_message(client, userdata, msg):
22 print(msg.topic+" "+str(msg.payload))
23 if msg.topic == id + '-response':
25 print 'port is', str(port)
26 print 'run ssh user@autossh.suanzi.ai -p ' + str(port) + ' to connect to ' + id + ' device'
28 client = mqtt.Client()
29 client.on_connect = on_connect
30 client.on_message = on_message
31 client.connect(MQTT_SERVER, MQTT_PORT, 60)