利用Linux lsof组件监控某个进程的网络连接状态

import time
import os
while True:
    time.sleep(1)
    # 输入lsof命令过滤进程号
    result = os.popen("lsof -i | grep 51851").read()
    # 如果有结果则写入并中断循环,否则继续
    if result:
        print(result)
        wrout = open("pidcon.result", "a")
        wrout.write(time.strftime("%Y-%m-%d %H:%M:%S")+" "+result)
        wrout.close()
        break
    else:
        print("没有连接")
        print(time.strftime("%Y-%m-%d %H:%M:%S"))
Last modification:February 28th, 2021 at 02:54 pm
硬币投入口