init commit
authorPeng Li <seudut@gmail.com>
Fri, 22 Jun 2018 04:06:28 +0000 (12:06 +0800)
committerPeng Li <seudut@gmail.com>
Fri, 22 Jun 2018 04:06:28 +0000 (12:06 +0800)
mqtt-client.py [new file with mode: 0644]
mqtt-daemon.py [new file with mode: 0644]

diff --git a/mqtt-client.py b/mqtt-client.py
new file mode 100644 (file)
index 0000000..98215fc
--- /dev/null
@@ -0,0 +1,45 @@
+#!/usr/bin/env python2
+
+from subprocess import call, Popen, PIPE, STDOUT
+import paho.mqtt.client as mqtt
+import argparse
+
+ID = 'rpdzkj'
+MQTT_SERVER = 'mqtt.suanzi.zi'
+MQTT_PORT = 1883
+
+SSH_SERVER = 'autossh.suanzi.ai'
+PORT_RANGE = (20000, 30000)
+USER = 'autossh'
+PASSWORD = 'hard2guess'
+#
+#def run_ssh (port):
+#    pass
+#
+#
+#class MyClient(mqtt.Client):
+#    def __init__(self):
+#
+#
+#
+def on_connect(client, userdata, flags, rc):
+    client.subscribe("rpdzkj-response")
+    print("Connected with result code "+str(rc))
+    client.publish('rpdzkj-request', 33)
+
+def on_message(client, userdata, msg):
+    print(msg.topic+" "+str(msg.payload))
+    if msg.topic == 'rpdzkj-rsponse':
+        print str(msg.payload)
+
+if __name__ == '__main__':
+#    parser = argparse.ArgumentParser(description='Choose an avaiable port and run autossh.')
+#    parser.add_argument('-i', '--id', dest='id', help='board unique id, rpdzkj|firefly', required=True)
+#    args = parser.parse_args()
+#    id = args.id
+#
+    client = mqtt.Client()
+    client.on_connect = on_connect
+    client.on_message = on_message
+    client.connect(MQTT_SERVER, MQTT_PORT, 60)
+    client.loop_forever()
diff --git a/mqtt-daemon.py b/mqtt-daemon.py
new file mode 100644 (file)
index 0000000..a06914a
--- /dev/null
@@ -0,0 +1,63 @@
+#!/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 random
+from uuid import getnode as get_mac
+
+ID = 'rpdzkj'
+MQTT_SERVER = 'mqtt.suanzi.zi'
+MQTT_PORT = 1883
+ALIVE_TIME = 60 * 5
+
+SSH_SERVER = 'autossh.suanzi.ai'
+PORT_RANGE = (20000, 20100)
+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")
+    print("Connected with result code "+str(rc))
+
+def on_message(client, userdata, msg):
+    print(msg.topic+" "+str(msg.payload))
+    if msg.topic == 'rpdzkj-request':
+        print str(msg.payload)
+        port = getAvailablePort(SSH_SERVER, PORT_RANGE)
+        if exec_ssh(port) == 0:
+            client.publish('rpdzkj-response', port)
+        else:
+            raise Exception ('run ssh failed')
+
+if __name__ == '__main__':
+    client = mqtt.Client()
+    client.on_connect = on_connect
+    client.on_message = on_message
+    client.connect(MQTT_SERVER, MQTT_PORT, 60)
+    client.loop_forever()