如何让Python paramiko模块SSH执行nohup命令后不产生输出?
- 内容介绍
- 文章标签
- 相关推荐
本文共计501个文字,预计阅读时间需要3分钟。
使用Python的paramiko模块执行SSH命令时,若遇到nohup命令不生效的问题,可以尝试以下方法解决:
1. 确保nohup命令正确执行:在本地终端执行`nohup command > nohup.out 2>&1 &`,检查命令是否正确运行。
2.使用paramiko模块的SSHClient对象连接到远程服务器,然后执行nohup命令。
3.在执行nohup命令时,确保命令后面加上`&`符号,表示在后台执行。
示例代码如下:
python
import paramiko创建SSH对象ssh=paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
连接到服务器ssh.connect(hostname='your_host', port=22, username='your_username', password='your_password')
执行nohup命令stdin, stdout, stderr=ssh.exec_command('nohup command > nohup.out 2>&1 &')
获取命令执行结果output=stdout.read().decode()error=stderr.read().decode()
print(Output:, output)print(Error:, error)
关闭连接ssh.close()
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 不生效的问题的文章就介绍到这了,更多相关Python paramiko 模块远程执行ssh 命令 nohup 不生效内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
本文共计501个文字,预计阅读时间需要3分钟。
使用Python的paramiko模块执行SSH命令时,若遇到nohup命令不生效的问题,可以尝试以下方法解决:
1. 确保nohup命令正确执行:在本地终端执行`nohup command > nohup.out 2>&1 &`,检查命令是否正确运行。
2.使用paramiko模块的SSHClient对象连接到远程服务器,然后执行nohup命令。
3.在执行nohup命令时,确保命令后面加上`&`符号,表示在后台执行。
示例代码如下:
python
import paramiko创建SSH对象ssh=paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
连接到服务器ssh.connect(hostname='your_host', port=22, username='your_username', password='your_password')
执行nohup命令stdin, stdout, stderr=ssh.exec_command('nohup command > nohup.out 2>&1 &')
获取命令执行结果output=stdout.read().decode()error=stderr.read().decode()
print(Output:, output)print(Error:, error)
关闭连接ssh.close()
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 不生效的问题的文章就介绍到这了,更多相关Python paramiko 模块远程执行ssh 命令 nohup 不生效内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

