Python中paramiko模块远程执行ssh命令nohup不生效怎么办

发布时间:2020-07-15 11:27:03 作者:清晨
来源:亿速云 阅读:996

这篇文章主要介绍Python中paramiko模块远程执行ssh命令nohup不生效怎么办,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

Python - paramiko 模块远程执行ssh 命令 nohup 不生效的问题解决

1、使用 paramiko 模块ssh 登陆到 linux 执行nohup命令不生效

# 执行命令
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()

因为执行完毕后,shell 会立即关闭通道

2、稍作修改,使用 invoke_shell

# 执行命令
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)
    # 等待命令执行完成
    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模块远程执行ssh命令nohup不生效怎么办的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. 使用 php ssh2 模块实现远程执行命令
  2. python下paramiko模块ssh登录的代码

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python paramiko nohup

上一篇:Python GUI开发,效率提升10倍的方法!

下一篇:Java程序员注意——审查Java代码的六种常见错误

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》