Add more info in the response, correct readme
authorPeng Li <seudut@gmail.com>
Sat, 28 Jul 2018 07:01:17 +0000 (15:01 +0800)
committerPeng Li <seudut@gmail.com>
Sat, 28 Jul 2018 07:01:17 +0000 (15:01 +0800)
README.md
board/suanzi-support
sztool

index e14e5cd..1e11da3 100644 (file)
--- a/README.md
+++ b/README.md
@@ -7,6 +7,9 @@
     - run `./add-user.sh` to create a user, used for remote ssh, named debug, and its password is suanzikeji 
     - run `./install.sh` to install the daemon and service file
 
+- On local machine
+    - run `pip install paho-mqtt`
+
 ## Usage
 
 ### list all connected device
@@ -31,6 +34,21 @@ timeout
 
 The output
 
+
+```
+debian@[~/project/remote-ssh]>> ./sztool list                                                                                                                                                                                  
+Mac:  080027a6f8dc
+
+Found 3 connected devices:
+Connected with result code 0
+
+ Device ID              Data
+------------------------------
+00044b8d6310            OK
+502b73d42c31            OK
+b0f1ec7a62ce            OK
+```
+
 ```
 debian@[~/project/remote-ssh]>> ./sztool connect 2acdeb93b285                                                                                                                                                                 
 Mac:  080027a6f8dc
index eca6e9b..3795d83 100755 (executable)
@@ -1,12 +1,13 @@
 #!/usr/bin/env python2
 
-from subprocess import call, Popen, PIPE, STDOUT
+from subprocess import call, Popen, PIPE, STDOUT, check_output
 import paho.mqtt.client as mqtt
 import shlex
 import random
 from uuid import getnode as get_mac
 import ast
 import sys
+import re
 
 MQTT_SERVER = 'mqtt.suanzi.ai'
 MQTT_PORT = 1883
@@ -35,7 +36,14 @@ def getAvailablePort(host, ports):
 
 def get_mac_str():
     mac = hex(get_mac())
-    return '{:0>12}'.format(mac[2:-1])
+    m = re.sub('^0x|L$', '', mac)
+    return '{:0>12}'.format(m)
+
+
+def get_users():
+    out = check_output("/bin/ls /home", shell=True).strip()
+    out = out.strip().split()
+    return " ".join(out)
 
 def exec_ssh(port):
     if port == None:
@@ -63,7 +71,7 @@ def on_message(client, userdata, msg):
             else:
                 raise Exception ('run ssh failed')
         if payload['command'] == 'list':
-            response = {'from': userdata['id'], 'type':'response', 'command':payload['command'], 'data':'OK'}
+            response = {'from': userdata['id'], 'type':'response', 'command':payload['command'], 'data': get_users()}
             client.publish(payload['from'], str(response))
 
 
diff --git a/sztool b/sztool
index 0c4fd7c..efd4837 100755 (executable)
--- a/sztool
+++ b/sztool
@@ -15,7 +15,6 @@ URI = 'debug@autossh.suanzi.ai'
 
 def get_all_connected(host, port, user, password):
     command = 'sshpass -p' + password + ' ssh -q -o "StrictHostKeyChecking=no"  -o "UserKnownHostsFile /dev/null" ' + user + '@' + host + '  netstat -tn | grep ' + str(port) + ' | grep ESTABLISHED | wc -l'
-    #print command
     output = check_output(shlex.split(command), shell=False)
     return int(output)
 
@@ -26,11 +25,8 @@ def on_connect(client, userdata, flags, rc):
         payload = {'from':userdata['id'], 'type':'request', 'command':'ssh'}
         client.publish(userdata['to'], str(payload))
     elif userdata['action'] == 'list':
-        #timer = threading.Timer(20, timeout)
-        #timer.start()
         payload = {'from':userdata['id'], 'type':'request', 'command':'list'}
         client.publish(userdata['to'], str(payload))
-        #print '-' * 30
         print'{0}\t\t{1}'.format(' Device ID ', 'Data')
         print '-' * 30
 
@@ -55,12 +51,6 @@ def get_mac_str():
     mac = hex(get_mac())
     return '{:0>12}'.format(mac[2:-1])
 
-#def timeout():
-#    print 'timeout'
-#    client.disconnect()
-#    sys.exit(0)
-#
-
 if __name__ == '__main__':
     parser = argparse.ArgumentParser(description='Choose an avaiable port and run ssh.', add_help=False)
     parser.add_argument('--help', action='help')