Update readme to support mac osx
[remote-debug.git] / board / suanzi-support
1 #!/usr/bin/env python2
2
3 from subprocess import call, Popen, PIPE, STDOUT, check_output
4 import paho.mqtt.client as mqtt
5 import shlex
6 import random
7 from uuid import getnode as get_mac
8 import ast
9 import sys, os
10 import re
11
12 MQTT_SERVER = 'mqtt.suanzi.ai'
13 MQTT_PORT = 1883
14
15 # The alive time new ssh session exist. It means if no client connect to this device through ssh tunnel in 5 minutes,
16 # this new sessin will terminate.
17 ALIVE_TIME = 60 * 5
18
19 SSH_SERVER = 'autossh.suanzi.ai'
20 PORT_RANGE = (20000, 40000)
21 USER = 'autossh'
22 PASSWORD = 'hard2guess'
23
24 def getAvailablePort(host, ports):
25     while True:
26         port = random.randint(ports[0], ports[1])
27         command = 'nc -z -v -w3 ' + host + ' ' + str(port)
28         p = Popen(command, shell=True, stdout=PIPE, stderr=STDOUT)
29         pout = p.communicate()[0].strip()
30         if p.returncode == 0:
31             continue
32         if 'Connection refused' in pout:
33             return port
34         else:
35             print pout
36
37 def get_mac_str():
38     mac = hex(get_mac())
39     m = re.sub('^0x|L$', '', mac)
40     return '{:0>12}'.format(m)
41
42 def get_hostname():
43     return check_output("/bin/hostname", shell=True).strip();
44
45 def check_ssh_connection(port):
46     command = '/bin/ps aux | /bin/grep -E \'ssh.*' + str(port) + '\' | /bin/grep -v grep | wc -l';
47     output = check_output(command, shell=True)
48     print 'output: ', output
49     if int(output) == 0:
50         return False
51     else:
52         return True
53
54 def try_use_rsa():
55     pub = os.getenv('HOME') + "/.ssh/id_rsa.pub"
56     if not os.path.isfile(pub):
57         # generate id_rsa.pub
58         command = 'ssh-keygen -b 2048 -t rsa -f ~/.ssh/id_rsa -P ""'
59         print command
60         print call(command, shell=True)
61     # copy id_rsa.pub
62     command = 'sshpass -p' + PASSWORD + ' ssh-copy-id -o "StrictHostKeyChecking=no"  -o "UserKnownHostsFile /dev/null" ' + USER + '@' + SSH_SERVER
63     print command
64     print call(command, shell=True)
65
66
67
68 def exec_ssh(port):
69     print "#### start establish ssh forwarding connection port, ", port
70     if port == None:
71         raise Exception('Port not avaliable')
72     command = 'sshpass -p' + PASSWORD + ' ssh -o "StrictHostKeyChecking=no"  -o "UserKnownHostsFile /dev/null" -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -fCR ' + str(port) +':localhost:22 ' + USER + '@' + SSH_SERVER + ' sleep ' + str(ALIVE_TIME)
73     print command
74     ret = call(shlex.split(command), shell=False)
75     if check_ssh_connection(port):
76         return ret
77     else: ## if sshpass fails (sometimes when another process has large CPU usage (100%) , no ssh connection, try another, 
78         try_use_rsa()
79         command = 'ssh -o "PasswordAuthentication=no" -o "StrictHostKeyChecking=no"  -o "UserKnownHostsFile /dev/null" -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -fCR ' + str(port) +':localhost:22 ' + USER + '@' + SSH_SERVER + ' sleep ' + str(ALIVE_TIME)
80         print command
81         ret = call(shlex.split(command), shell=False)
82         return ret
83
84 def on_connect(client, userdata, flags, rc):
85     client.subscribe(userdata['id'])
86     client.subscribe('all')
87     print("Connected with result code "+str(rc))
88
89 def on_message(client, userdata, msg):
90     print('Receive topic:' + msg.topic + ' payload: ' +str(msg.payload))
91     payload = ast.literal_eval(str(msg.payload))
92     from_id = payload['from']
93     if payload['type'] == 'request':
94         if payload['command'] == 'ssh':
95             port = getAvailablePort(SSH_SERVER, PORT_RANGE)
96             if exec_ssh(port) == 0:
97                 print "#### OK, SSH forwarding connection established, port, ", port
98                 response = {'from': userdata['id'], 'type':'response', 'command':payload['command'], 'data':port}
99                 client.publish(payload['from'], str(response))
100             else:
101                 raise Exception ('run ssh failed')
102         if payload['command'] == 'list':
103             response = {'from': userdata['id'], 'type':'response', 'command':payload['command'], 'data': get_hostname()}
104             client.publish(payload['from'], str(response))
105
106
107
108 if __name__ == '__main__':
109     id = get_mac_str()
110     print 'Mac: ', id
111     client = mqtt.Client(userdata={'id':id})
112     client.on_connect = on_connect
113     client.on_message = on_message
114     client.connect(MQTT_SERVER, MQTT_PORT, 60)
115     client.loop_forever()