8a053d0d2de6dd6c26d6b79ecd35af4e0f9f3829
[remote-debug.git] / sztool
1 #!/usr/bin/env python2
2
3 from subprocess import call, Popen, PIPE, STDOUT
4 import paho.mqtt.client as mqtt
5 import argparse
6
7 MQTT_SERVER = 'mqtt.suanzi.ai'
8 MQTT_PORT = 1883
9
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()
14     id = args.id
15
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)
20
21     def on_message(client, userdata, msg):
22         print(msg.topic+" "+str(msg.payload))
23         if msg.topic == id + '-response':
24             port = msg.payload
25             print 'port is', str(port)
26             print 'run ssh debug@autossh.suanzi.ai -p ' + str(port) + ' to connect to  ' + id + ' device'
27
28     client = mqtt.Client()
29     client.on_connect = on_connect
30     client.on_message = on_message
31     client.connect(MQTT_SERVER, MQTT_PORT, 60)
32     client.loop_forever()