3 from subprocess import call, Popen, PIPE, STDOUT, check_output
4 import paho.mqtt.client as mqtt
7 from uuid import getnode as get_mac
12 MQTT_SERVER = 'mqtt.suanzi.ai'
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.
19 SSH_SERVER = 'autossh.suanzi.ai'
20 PORT_RANGE = (20000, 40000)
22 PASSWORD = 'hard2guess'
24 def getAvailablePort(host, ports):
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()
32 if 'Connection refused' in pout:
39 m = re.sub('^0x|L$', '', mac)
40 return '{:0>12}'.format(m)
44 out = check_output("/bin/ls /home", shell=True).strip()
45 out = out.strip().split()
50 raise Exception('Port not avaliable')
51 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)
53 return call(shlex.split(command), shell=False)
56 def on_connect(client, userdata, flags, rc):
57 client.subscribe(userdata['id'])
58 client.subscribe('all')
59 print("Connected with result code "+str(rc))
61 def on_message(client, userdata, msg):
62 print('Receive topic:' + msg.topic + ' payload: ' +str(msg.payload))
63 payload = ast.literal_eval(str(msg.payload))
64 from_id = payload['from']
65 if payload['type'] == 'request':
66 if payload['command'] == 'ssh':
67 port = getAvailablePort(SSH_SERVER, PORT_RANGE)
68 if exec_ssh(port) == 0:
69 response = {'from': userdata['id'], 'type':'response', 'command':payload['command'], 'data':port}
70 client.publish(payload['from'], str(response))
72 raise Exception ('run ssh failed')
73 if payload['command'] == 'list':
74 response = {'from': userdata['id'], 'type':'response', 'command':payload['command'], 'data': get_users()}
75 client.publish(payload['from'], str(response))
79 if __name__ == '__main__':
82 client = mqtt.Client(userdata={'id':id})
83 client.on_connect = on_connect
84 client.on_message = on_message
85 client.connect(MQTT_SERVER, MQTT_PORT, 60)