0
這個問題是這個問題的後續:Using paramiko to send commands to an open shell that has an interactive element所以請在這裏回答之前閱讀。用交互式元素實現paramiko的超時
我已經成功地將數據發送到數組的系統shell,但是我需要幫助瞭解如何在數組花費太長時間來運行我發送的腳本時執行超時。 Paramiko的exec_command
有一個timeout=
參數,但這不會有幫助,因爲我發送的唯一命令是"script"
,它立即返回並等待通道上的輸入,並且當我嘗試執行時似乎將該函數的其餘部分打破爲沒有從陣列返回。
該陣列然後應該處理我發送它的腳本,並通過stdout
返回輸出,但是,如果數組需要很長時間,我沒有辦法超時連接,它阻止了其餘的劇本。
這裏是我的代碼:
def run_script(self, script_name):
""" Run a script on the remote array and return the stdout
"""
try:
common_functions_file = os.path.join(SCRIPT_DIR, 'zfs_common_functions.js')
common_functions = open(common_functions_file).read().splitlines()
# add common_functions to the top of the script
script_contents = common_functions + open(script_name).read().splitlines()
stdin, stdout, stderr = self._ssh.exec_command('script')
for line in script_contents:
# Skip lines with comments
if re.match("^//", line):
continue
stdin.write(line)
stdin.write('\n')
stdin.write('.\n')
stdin.flush()
error = stderr.readlines()
if len(error) == 0:
try:
output = ''.join(stdout.readlines())
if(re.search('aksh', output)):
logger.warn("ZFS Shell Error: %s" % output)
return None
return output
except Exception as e:
logger.exception(e)
else:
logger.error(error)
return None
except paramiko.SSHException as e:
logger.warn(
"Couldn't execute script on array %s: %s" % (array.name, e))
except AttributeError as e:
logger.exception(e)
raise
except Exception:
raise