From: Peng Li Date: Fri, 22 Jun 2018 06:04:09 +0000 (+0800) Subject: Add install file X-Git-Url: http://47.100.26.94:8080/?a=commitdiff_plain;ds=sidebyside;h=a371c0f6b2d994a074eaeb56a2125cb4c0af85f8;p=remote-debug.git Add install file --- diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..acbf196 --- /dev/null +++ b/install.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +SERVICE='suanzi-support.service' + + +echo 'Install suanzi-support' +cp suanzi-support /usr/local/bin/ || exit 1 + +echo "Install ${SERVICE}" + +cp ${SERVICE} /lib/systemd/system || exit 1 +systemctl enable ${SERVICE} || exit 1 + +echo "Start ${SERVICE}" +systemctl start ${SERVICE} || exit 1 + +echo "Done" + + + diff --git a/mqtt-daemon.py b/mqtt-daemon.py deleted file mode 100644 index 41ed9fc..0000000 --- a/mqtt-daemon.py +++ /dev/null @@ -1,73 +0,0 @@ -#!/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 - -MQTT_SERVER = 'mqtt.suanzi.ai' -MQTT_PORT = 1883 -ALIVE_TIME = 60 * 5 - -SSH_SERVER = 'autossh.suanzi.ai' -PORT_RANGE = (20000, 30000) -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) - p = Popen(command, shell=True, stdout=PIPE, stderr=STDOUT) - pout = p.communicate()[0].strip() - if p.returncode == 0: - continue - if 'Connection refused' in pout: - return port - else: - print pout - -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) - 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') - 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') - -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) - client.connect(MQTT_SERVER, MQTT_PORT, 60) - client.loop_forever() diff --git a/suanzi-support b/suanzi-support new file mode 100755 index 0000000..41ed9fc --- /dev/null +++ b/suanzi-support @@ -0,0 +1,73 @@ +#!/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 + +MQTT_SERVER = 'mqtt.suanzi.ai' +MQTT_PORT = 1883 +ALIVE_TIME = 60 * 5 + +SSH_SERVER = 'autossh.suanzi.ai' +PORT_RANGE = (20000, 30000) +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) + p = Popen(command, shell=True, stdout=PIPE, stderr=STDOUT) + pout = p.communicate()[0].strip() + if p.returncode == 0: + continue + if 'Connection refused' in pout: + return port + else: + print pout + +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) + 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') + 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') + +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) + client.connect(MQTT_SERVER, MQTT_PORT, 60) + client.loop_forever() diff --git a/suanzi-support.service b/suanzi-support.service index 16531f3..87f0081 100644 --- a/suanzi-support.service +++ b/suanzi-support.service @@ -5,7 +5,7 @@ After=network-online.target [Service] User=debian Type=simple -ExecStart=/usr/local/bin/autossh-wrapper.py -u 'autossh' autossh.suanzi.ai +ExecStart=/usr/local/bin/suanzi-support ExecStop=/usr/bin/pkill -P $MAINPID KillMode=process Restart=always