From: Peng Li Date: Thu, 2 Aug 2018 10:12:28 +0000 (+0800) Subject: Fix bug - sometime ssh forwarding connection cannot establish when cpu is in hight... X-Git-Url: http://47.100.26.94:8080/?a=commitdiff_plain;h=7d3504346fbb096ed88528e3c1355417577f8d86;hp=1ed68d422ea534cd715142f2a377442009dc5c2d;p=remote-debug.git Fix bug - sometime ssh forwarding connection cannot establish when cpu is in hight loaded by other application (100% usage). Not find the root cause, related to sshpass, workaround is to use id_rsa.pub public key instead of password to authenticate if fails --- diff --git a/board/suanzi-support b/board/suanzi-support index f55b41a..8d398bd 100755 --- a/board/suanzi-support +++ b/board/suanzi-support @@ -6,7 +6,7 @@ import shlex import random from uuid import getnode as get_mac import ast -import sys +import sys, os import re MQTT_SERVER = 'mqtt.suanzi.ai' @@ -42,13 +42,44 @@ def get_mac_str(): def get_hostname(): return check_output("/bin/hostname", shell=True).strip(); +def check_ssh_connection(port): + command = '/bin/ps aux | /bin/grep -E \'ssh.*' + str(port) + '\' | /bin/grep -v grep | wc -l'; + output = check_output(command, shell=True) + print 'output: ', output + if int(output) == 0: + return False + else: + return True + +def try_use_rsa(): + pub = os.getenv('HOME') + "/.ssh/id_rsa.pub" + if not os.path.isfile(pub): + # generate id_rsa.pub + command = 'ssh-keygen -b 2048 -t rsa -f ~/.ssh/id_rsa -P ""' + print command + print call(command, shell=True) + # copy id_rsa.pub + command = 'sshpass -p' + PASSWORD + ' ssh-copy-id -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile /dev/null" ' + USER + '@' + SSH_SERVER + print command + print call(command, shell=True) + + + def exec_ssh(port): + print "#### start establish ssh forwarding connection port, ", port if port == None: raise Exception('Port not avaliable') 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) - + ret = call(shlex.split(command), shell=False) + if check_ssh_connection(port): + return ret + else: ## if sshpass fails (sometimes when another process has large CPU usage (100%) , no ssh connection, try another, + try_use_rsa() + command = 'ssh -o "PasswordAuthentication=no" -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 + ret = call(shlex.split(command), shell=False) + return ret def on_connect(client, userdata, flags, rc): client.subscribe(userdata['id']) @@ -63,6 +94,7 @@ def on_message(client, userdata, msg): if payload['command'] == 'ssh': port = getAvailablePort(SSH_SERVER, PORT_RANGE) if exec_ssh(port) == 0: + print "#### OK, SSH forwarding connection established, port, ", port response = {'from': userdata['id'], 'type':'response', 'command':payload['command'], 'data':port} client.publish(payload['from'], str(response)) else: