Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果进程通过paramiko ssh会话运行,并且最后带有“&”,则该进程将终止_Python_Linux_Ssh_Paramiko_Tcpdump - Fatal编程技术网

Python 如果进程通过paramiko ssh会话运行,并且最后带有“&”,则该进程将终止

Python 如果进程通过paramiko ssh会话运行,并且最后带有“&”,则该进程将终止,python,linux,ssh,paramiko,tcpdump,Python,Linux,Ssh,Paramiko,Tcpdump,我只想使用paramiko在后台运行tcpdump 以下是代码的一部分: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, username=login, password=password) transport = ssh.get_transport() channel = transport.open_session() channel.

我只想使用paramiko在后台运行tcpdump

以下是代码的一部分:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=login, password=password)
transport = ssh.get_transport()
channel = transport.open_session()
channel.get_pty()
channel.set_combine_stderr(True)

cmd = "(nohup tcpdump -i eth1  port 443 -w /tmp/dump20150317183305940107.pcap) &"
channel.exec_command(cmd)
status = channel.recv_exit_status()
执行此代码后,pgrep tcpdump不返回任何内容

如果我删除并签署tcpdump正确运行,但我的ssh shell被阻止

如何在后台正确运行tcpdump

我试过的命令是:

cmd = 'nohup tcpdump -i eth1  port 443 -w /tmp/dump20150317183305940107.pcap &\n'
cmd = "screen -d -m 'tcpdump -i eth1  port 443 -w /tmp/dump20150317183305940107.pcap'"
cmd = 'nohup sleep 5 && echo $(date) >> "test.log" &'
使用&立即退出远程命令。因此,远程sshd可能取决于实现,但openssh确实会杀死从命令调用开始的所有进程。在您的情况下,您只需要生成一个新的进程nohup tcpdump,它将在结束时立即返回。channel.recv_exit_状态将仅在&oooperation的退出代码准备就绪(立即生效)之前阻塞。然后,您的代码终止,终止ssh会话,这将使远程sshd杀死生成的nohup tcpdump进程。这就是为什么最终没有tcpdump进程

以下是您可以做的:

由于exec_命令将为您的命令生成一个新线程,所以您可以让它保持打开状态并继续执行其他任务。但要确保不时清空冗余远程命令的缓冲区,以防止paramiko暂停

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=login, password=password)
transport = ssh.get_transport()
channel_tcpdump = transport.open_session()
channel_tcpdump.get_pty()
channel_tcpdump.set_combine_stderr(True)

cmd = "tcpdump -i eth1  port 443 -w /tmp/dump20150317183305940107.pcap"        # command will never exit
channel_tcpdump.exec_command(cmd)  # will return instantly due to new thread being spawned.
# do something else
time.sleep(15)      # wait 15 seconds
_,stdout,_ = ssh.exec_command("pgrep tcpdump") # or explicitly pkill tcpdump
print stdout.read()     # other command, different shell
channel_tcpdump.close()     # close channel and let remote side terminate your proc.
time.sleep(10)
使用&立即退出远程命令。因此,远程sshd可能取决于实现,但openssh确实会杀死从命令调用开始的所有进程。在您的情况下,您只需要生成一个新的进程nohup tcpdump,它将在结束时立即返回。channel.recv_exit_状态将仅在&oooperation的退出代码准备就绪(立即生效)之前阻塞。然后,您的代码终止,终止ssh会话,这将使远程sshd杀死生成的nohup tcpdump进程。这就是为什么最终没有tcpdump进程

以下是您可以做的:

由于exec_命令将为您的命令生成一个新线程,所以您可以让它保持打开状态并继续执行其他任务。但要确保不时清空冗余远程命令的缓冲区,以防止paramiko暂停

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=login, password=password)
transport = ssh.get_transport()
channel_tcpdump = transport.open_session()
channel_tcpdump.get_pty()
channel_tcpdump.set_combine_stderr(True)

cmd = "tcpdump -i eth1  port 443 -w /tmp/dump20150317183305940107.pcap"        # command will never exit
channel_tcpdump.exec_command(cmd)  # will return instantly due to new thread being spawned.
# do something else
time.sleep(15)      # wait 15 seconds
_,stdout,_ = ssh.exec_command("pgrep tcpdump") # or explicitly pkill tcpdump
print stdout.read()     # other command, different shell
channel_tcpdump.close()     # close channel and let remote side terminate your proc.
time.sleep(10)

只需要添加一个sleep命令

cmd = "(nohup tcpdump -i eth1  port 443 -w /tmp/dump20150317183305940107.pcap) &"  
改为

cmd = "(nohup tcpdump -i eth1  port 443 -w /tmp/dump20150317183305940107.pcap) &; sleep 1" 

只需要添加一个sleep命令

cmd = "(nohup tcpdump -i eth1  port 443 -w /tmp/dump20150317183305940107.pcap) &"  
改为

cmd = "(nohup tcpdump -i eth1  port 443 -w /tmp/dump20150317183305940107.pcap) &; sleep 1" 

这是行不通的。它在任何操作系统上对你有用吗?在哪?这不起作用。它在任何操作系统上对你有用吗?在哪方面?