亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解決Python paramiko 模塊遠程執(zhí)行ssh 命令 nohup 不生效的問題

 更新時間:2020年07月14日 14:11:22   作者:簡簡單單OnlineZuozuo  
這篇文章主要介紹了解決Python paramiko 模塊遠程執(zhí)行ssh 命令 nohup 不生效的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

Python - paramiko 模塊遠程執(zhí)行ssh 命令 nohup 不生效的問題解決

1、使用 paramiko 模塊ssh 登陸到 linux 執(zhí)行nohup命令不生效

# 執(zhí)行命令
def command(ssh_config, cmd, result_print=None, nohup=False):
  ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect(hostname=ssh_config.hostname, port=ssh_config.port, username=ssh_config.username,
        password=ssh_config.password)
  print(ssh_config.hostname + '@' + ssh_config.username, ': ', cmd)
  stdin, stdout, stderr = ssh.exec_command(cmd)
  result = stdout.read()
  if result_print:
    lines = read_unicode(result)
    for line in lines:
      print(line)
  ssh.close()

因為執(zhí)行完畢后,shell 會立即關閉通道

2、稍作修改,使用 invoke_shell

# 執(zhí)行命令
def command(ssh_config, cmd, result_print=None, nohup=False):
  ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect(hostname=ssh_config.hostname, port=ssh_config.port, username=ssh_config.username,
        password=ssh_config.password)
  print(ssh_config.hostname + '@' + ssh_config.username, ': ', cmd)
  if nohup:
    cmd += ' & \n '
    invoke = ssh.invoke_shell()
    invoke.send(cmd)
    # 等待命令執(zhí)行完成
    time.sleep(2)
  else:
    stdin, stdout, stderr = ssh.exec_command(cmd)
    result = stdout.read()
    if result_print:
      lines = read_unicode(result)
      for line in lines:
        print(line)
  ssh.close()

到此這篇關于解決Python paramiko 模塊遠程執(zhí)行ssh 命令 nohup 不生效的問題的文章就介紹到這了,更多相關Python paramiko 模塊遠程執(zhí)行ssh 命令 nohup 不生效內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論