X-Git-Url: http://47.100.26.94:8080/?a=blobdiff_plain;f=suanzi-support;h=eca6e9bbbc5370c046c94d9e3867666f6fdc4405;hb=0afa5b864bd65a10205ce13beb5918cf22b45471;hp=41ed9fc07feba7d15ead030fde7f52ef66afc564;hpb=a371c0f6b2d994a074eaeb56a2125cb4c0af85f8;p=remote-debug.git diff --git a/suanzi-support b/suanzi-support index 41ed9fc..eca6e9b 100755 --- a/suanzi-support +++ b/suanzi-support @@ -1,29 +1,29 @@ #!/usr/bin/env python2 -# dependencies: -# 1. pip install paho-mqtt -# 2. apt-get install sshpass -# from subprocess import call, Popen, PIPE, STDOUT import paho.mqtt.client as mqtt import shlex -import os import random from uuid import getnode as get_mac +import ast +import sys MQTT_SERVER = 'mqtt.suanzi.ai' MQTT_PORT = 1883 + +# The alive time new ssh session exist. It means if no client connect to this device through ssh tunnel in 5 minutes, +# this new sessin will terminate. ALIVE_TIME = 60 * 5 SSH_SERVER = 'autossh.suanzi.ai' -PORT_RANGE = (20000, 30000) +PORT_RANGE = (20000, 40000) USER = 'autossh' PASSWORD = 'hard2guess' def getAvailablePort(host, ports): while True: port = random.randint(ports[0], ports[1]) - command = 'nc -z -v -w5 ' + host + ' ' + str(port) + command = 'nc -z -v -w3 ' + host + ' ' + str(port) p = Popen(command, shell=True, stdout=PIPE, stderr=STDOUT) pout = p.communicate()[0].strip() if p.returncode == 0: @@ -33,41 +33,46 @@ def getAvailablePort(host, ports): else: print pout +def get_mac_str(): + mac = hex(get_mac()) + return '{:0>12}'.format(mac[2:-1]) + def exec_ssh(port): if port == None: raise Exception('Port not avaliable') - #command = 'sshpass -p' + PASSWORD + ' ssh -o "ServerAliveInterval 30" -o "ServerAliveCountMax 1" -fCNR ' + str(port) +':localhost:22 ' + USER + '@' + SSH_SERVER - command = 'sshpass -p' + PASSWORD + ' ssh -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -fCR ' + str(port) +':localhost:22 ' + USER + '@' + SSH_SERVER + ' sleep ' + str(ALIVE_TIME) + 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) print command return call(shlex.split(command), shell=False) def on_connect(client, userdata, flags, rc): - #client.subscribe("rpdzkj-request") - client.subscribe(client.id + '-request') + client.subscribe(userdata['id']) + client.subscribe('all') print("Connected with result code "+str(rc)) def on_message(client, userdata, msg): - print(msg.topic+" "+str(msg.payload)) - if msg.topic == client.id + '-request': - print str(msg.payload) - port = getAvailablePort(SSH_SERVER, PORT_RANGE) - if exec_ssh(port) == 0: - client.publish(client.id + '-response', port) - else: - raise Exception ('run ssh failed') + print('Receive topic:' + msg.topic + ' payload: ' +str(msg.payload)) + payload = ast.literal_eval(str(msg.payload)) + from_id = payload['from'] + if payload['type'] == 'request': + if payload['command'] == 'ssh': + port = getAvailablePort(SSH_SERVER, PORT_RANGE) + if exec_ssh(port) == 0: + response = {'from': userdata['id'], 'type':'response', 'command':payload['command'], 'data':port} + client.publish(payload['from'], str(response)) + else: + raise Exception ('run ssh failed') + if payload['command'] == 'list': + response = {'from': userdata['id'], 'type':'response', 'command':payload['command'], 'data':'OK'} + client.publish(payload['from'], str(response)) + -class MyClient(mqtt.Client): - def __init__(self, id): - mqtt.Client.__init__(self) - self.id = id - self.on_connect = on_connect - self.on_message = on_message if __name__ == '__main__': - mac = hex(get_mac()) - id = os.getenv('USER') + '-' + '{:0>12}'.format(mac[2:13]) - print 'id is: ' , id - client = MyClient(id) + id = get_mac_str() + print 'Mac: ', id + client = mqtt.Client(userdata={'id':id}) + client.on_connect = on_connect + client.on_message = on_message client.connect(MQTT_SERVER, MQTT_PORT, 60) client.loop_forever()