Monday, 15 April 2013

python - Process dies, if it is run via paramiko ssh session and with "&" in the end -


I just want to run tcpdump in the background using paramiko.

Here's part of the code:

  ssh = paramiko.SSHClient () ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) ssh.connect (host, username = English , password = password) transport = Ssh.get_transport () channel = transport.open_session () channel.get_pty () channel.set_combine_stderr (True) cmd = "(Nohup Tiseepeedmp -i at one port 443, and / TAP / Deep 20150317183305940107. pcap ) & Amp; " Channel.exec_command (cmd) position = channel.recv_exit_status ()  

After executing this code, pgrep tcpdump can not

If I see & amp; sign tcpdump run properly, but my SSH shell is blocked.

How do I TCPDump is correct in the background?

I have tried what command:

  cmd = 'nohup tcpdump -i eth1 port 443 -w / tmp / dump 20150317183305940107. Pcap and \ n' cmd = ' Screen-D-M 'TCPDump-IA1 Port 443-V / TAMP / Deep 20150317183305940107.pcap' 'CMD =' Nohow sleep 5 & amp; Echo $ (date)> & gt; & Gt; With "Test.log" and "   

and Instantly get out of the remote command. Remote SSDD will be possible (depending on implementation, but openssh), all the processes that started with your order were ordered. In your case, you just have a new process nohup tcpdump which will eventually return due to & channel.recv_exit_status () will only be blocked until & Amp; nohup tcpdump proc You will not be finished with any TCPDump process.

Here's what you can do:

Since exec_command Is going to create a new thread, you can just leave it open You can proceed with other tasks. But empty buffers can make sure to stop paramiko by preventing it every now and then (for verbose remote commands).

  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 will never get out of the eth1 port 443 w / tmp / dump20150317183305940107.pcap" # command channel_tcpdump.exec_command (cmd) # instantly new Thread will come back due to birth. # Wait for some time.Sleep (15) wait for # 15 seconds _, stdout, _ = ssh.exec_command ("pgrep tcpdump") # or explicitly pkill tcpdump print stdout.read () # Other commands, various shell channel_tcpdump .close () # Close channel and remote side end your proc. Time.sleep (10)  

No comments:

Post a Comment