82f547d84bf56585548298c82a850a2a53462cc4
[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 URI = 'debug@autossh.suanzi.ai'
10
11 def on_connect(client, userdata, flags, rc):
12     id = client.id
13     client.subscribe(id + "-response")
14     print("Connected with result code "+str(rc))
15     client.publish(id + '-request', 0)
16
17 def on_message(client, userdata, msg):
18     id = client.id
19     print(msg.topic+" "+str(msg.payload))
20     if msg.topic == id + '-response':
21         port = msg.payload
22         print 'run "ssh ' + URI + ' -p ' + str(port) + '" to connect to device (%s)' % id
23
24 if __name__ == '__main__':
25     parser = argparse.ArgumentParser(description='Choose an avaiable port and run ssh.')
26     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)
27     #parser.add_argument('connect',  help='list all connected devices')
28     #parser.add_argument('list',  help='list all connected devices')
29     args = parser.parse_args()
30
31     client = mqtt.Client()
32     client.id = args.id
33     client.on_connect = on_connect
34     client.on_message = on_message
35     client.connect(MQTT_SERVER, MQTT_PORT, 60)
36     client.loop_forever()