【问题标题】:How to run a tshark command in the background and exit it using subprocess in python如何在后台运行 tshark 命令并使用 python 中的子进程退出它
【发布时间】:2019-01-08 18:49:59
【问题描述】:

我想在通过 telnet 连接到远程主机设备时,使用 Wireshark 的命令行风格 tshark 进行数据包捕获。我想调用我为捕获而编写的函数:

def wire_cap(ip1,ip2,op_fold,file_name,duration):  # invoke tshark to capture traffic during session
    if duration == 0:
        cmd='"tshark" -i 1 -P -w '+ op_fold+file_name+'.pcap src ' + str(ip1) + ' or src '+ str(ip2)
    else:
        cmd='"tshark" -i 1 -a duration:'+str(duration)+' -P -w '+ op_fold+file_name+'.pcap src ' + str(ip1) + ' or src '+ str(ip2)

    p = subprocess.Popen(cmd, shell=True,stderr=subprocess.PIPE)
    while True:
        out = p.stderr.read(1)
        if out == '' and p.poll() != None:
            break
        if out != '':
            sys.stdout.write(out)
            sys.stdout.flush()

出于调试目的,我想在后台运行此函数,方法是在需要时调用它,并在获得捕获时停止它。比如:

Start a thread or a background process called wire_capture
//Do something here
Stop the thread or the background process wire_capture

通过阅读,我意识到 thread.start_new_thread()threading.Thread() 似乎只有在我知道捕获的持续时间(退出条件)时才适用。我尝试使用thread.exit(),但它的行为类似于sys.exit(),并完全停止了程序的执行。我也试过threading.Event()如下:

if cap_flg:
    print "Starting a packet capture thread...."
    th_capture = threading.Thread(target=wire_cap, name='Thread_Packet_Capture', args=(IP1, IP2, output, 'wire_capture', 0, ))
    th_capture.setDaemon(True)
    th_capture.start()

.
.
.
.
.

if cap_flg:
    thread_kill = threading.Event()
    print "Exiting the packet capture thread...."
    thread_kill.set()
    th_capture.join()

我想知道如何在我想停止进程时停止它(就像可以添加退出条件以便我可以退出线程执行)。我试过的上面的代码似乎不起作用。

【问题讨论】:

  • 也许问题是setDaemon() 被忽略,如docs 中所述:主线程不是守护线程,因此在主线程中创建的所有线程默认为 daemon =错误的。当没有活着的非守护线程时,整个 Python 程序退出。
  • @LuisMuñoz 我错过了那部分。可能这就是它不在后台运行的原因。但是,我想知道是否有替代threading 的方法。有什么线索吗?
  • 不幸的是,在 python 上没有太多线程经验。也许从辅助线程启动该线程会有所帮助。
  • 向 popen 询问 pid 然后直接杀死它怎么样?

标签: multithreading python-2.7 subprocess background-process tshark


【解决方案1】:

threading.Event() 方法是在正确的轨道上,但是您需要事件在两个线程中都可见,因此您需要在启动第二个线程之前创建它并传递它:

if cap_flg:
    print "Starting a packet capture thread...."
    thread_kill = threading.Event()
    th_capture = threading.Thread(target=wire_cap, name='Thread_Packet_Capture', args=(IP1, IP2, output, 'wire_capture', 0, thread_kill))
    th_capture.setDaemon(True)
    th_capture.start()

while 循环中,让观察线程在每次迭代时检查事件,如果设置了则停止循环(也可能终止它启动的tshark)。您还需要确保进程不会永远等待进程的输出,并忽略终止事件,仅在有数据可用的情况下从管道读取:

def wire_cap(ip1,ip2,op_fold,file_name,duration,event):  # invoke tshark to capture traffic during session
    if duration == 0:
        cmd='"tshark" -i 1 -P -w '+ op_fold+file_name+'.pcap src ' + str(ip1) + ' or src '+ str(ip2)
    else:
        cmd='"tshark" -i 1 -a duration:'+str(duration)+' -P -w '+ op_fold+file_name+'.pcap src ' + str(ip1) + ' or src '+ str(ip2)

    p = subprocess.Popen(cmd, shell=True,stderr=subprocess.PIPE)
    while not event.is_set():
        # Make sure to not block forever waiting for 
        # the process to say things, so we can see if
        # the event gets set. Only read if data is available.
        if len(select.select([p.stderr], [], [], 0.1)) > 0:
            out = p.stderr.read(1)
            if out == '' and p.poll() != None:
                break
            if out != '':
                sys.stdout.write(out)
                sys.stdout.flush()
    p.kill()

然后真正告诉线程停止你只需设置事件:

if cap_flg:
    print "Exiting the packet capture thread...."
    thread_kill.set()
    th_capture.join()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多