- 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
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
#!/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
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:
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))
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)
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
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')