抓包

进行linux系统抓包,并且定时分隔防止文件太大

#!/usr/bin/env python
# -*- coding:utf-8 -*- import os
import sys
import logging
import threading
import time
import datetime # 工作目录
WORKSPACE = '/root/workspace'
# 间隔时间 分钟
INTERVAL = 10 def daemon():
import os
# create - fork 1
try:
pid = os.fork()
if pid > 0:
return pid
except OSError as error:
logging.error('fork #1 failed: %d (%s)' % (error.errno, error.strerror))
return -1
# it separates the son from the father
os.chdir(WORKSPACE)
os.setsid()
os.umask(0)
# create - fork 2
try:
pid = os.fork()
if pid > 0:
return pid
except OSError as error:
logging.error('fork #2 failed: %d (%s)' % (error.errno, error.strerror))
return -1
sys.stdout.flush()
sys.stderr.flush()
si = open("/dev/null", 'r')
so = open("/dev/null", 'ab')
se = open("/dev/null", 'ab', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
return 0 def tcpdump(filename):
logging.info('start')
os.system(f'tcpdump -i any -w {filename}.pcap') def run():
while True:
try:
filename = str(time.strftime('%Y-%m-%d_%H:%M', time.localtime()))
logging.info(f'tcpdump {filename}')
threading.Thread(target=tcpdump,args=(filename,)).start()
time.sleep(INTERVAL * 60)
os.system("ps -ef | grep tcpdump | awk '{print $2}' | xargs kill -9")
except Exception as e:
logging.error(e) def main():
pid = daemon()
if pid:
return pid
run() if __name__ == '__main__':
logging.basicConfig(filename="tcpdump.log", level=logging.INFO,
format="%(asctime)s [%(filename)s:%(lineno)d] %(message)s", datefmt="%m/%d/%Y %H:%M:%S [%A]")
main()

  

Python开发【笔记】:抓包(实时分隔)的更多相关文章

  1. python编写网络抓包分析脚本

    python编写网络抓包分析脚本 写网络抓包分析脚本,一个称手的sniffer工具是必不可少的,我习惯用Ethereal,简单,易用,基于winpcap的一个开源的软件 Ethereal自带许多协议的 ...

  2. python+pcap+dpkt 抓包小实例

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ 网络数据包捕获与分析程序 """ imp ...

  3. python开发笔记-通过xml快捷获取数据

    今天在做下python开发笔记之如何通过xml快捷获取数据,下面以调取nltk语料库为例: import nltk nltk.download() showing info https://raw.g ...

  4. python爬虫——论抓包的正确姿势和学好Javascript的重要性(1)

    没事想爬下数据,就入了scrapy坑,跟着https://zhuanlan.zhihu.com/data-factory这篇教程走,中间被小数量的网站坑过,不过还是写出了爬虫~~ 切糕王子:毫无防御, ...

  5. python+pcap+dpkt抓包小实例

    通过pcap与dpkt抓包解包示例: #!/usr/bin/env python # -*- coding: utf-8 -*- """ 网络数据包捕获与分析程序 &qu ...

  6. python开发笔记-python调用webservice接口

    环境描述: 操作系统版本: root@9deba54adab7:/# uname -a Linux 9deba54adab7 --generic #-Ubuntu SMP Thu Dec :: UTC ...

  7. 微信开发-charles抓包

    在微信开发过程中有一块不能使用开发者工具进行调试,需要查看请求的返回,故使用了charles抓包工具. 环境配置 1.http://www.charlesproxy.com/getssl/ 下载cha ...

  8. Java高效开发-fiddler抓包工具

    1.简介 Fiddler是最常用的抓包工具之一,只要打开之后就能够实现数据包抓取,关闭之后会自动取消代理,非常方便本地调试 2.下载 阿里云盘地址:https://www.aliyundrive.co ...

  9. Python开发笔记:网络数据抓取

    网络数据获取(爬取)分为两部分: 1.抓取(抓取网页) · urlib内建模块,特别是urlib.request · Requests第三方库(中小型网络爬虫的开发) · Scrapy框架(大型网络爬 ...

  10. python开发笔记-Python3.7+Django2.2 Docker镜像搭建

    目标镜像环境介绍: 操作系统:ubuntu16.04 python版本:python 3.7.4 django版本:2.2 操作步骤: 1.  本地安装docker环境(略)2. 拉取ubunut指定 ...

随机推荐

  1. [转]ztree出现$.fn.zTree is undefined错误的解决办法。

    原文地址:https://blog.csdn.net/smallboy2011/article/details/20554269 问题描述,在一个界面使用ztree创建树,提示TypeError: $ ...

  2. .io域名在申请SSL证书时被坑

    注:标题其实与最后内容不符,只是描述一个事实 - 遇到坑了,不代表观点 - io域名坑我. .io后缀的域名是英属印度洋领地的国别域名,由NIC.io(runby Internet Computer ...

  3. Spring 自动转配类 在类中使用@Bean 注解进行转配但是需要排除该类说明

    在spring中可以使用 @Component @Configuration @Bean(实例化后返回该bean)进行类实例的自动装配. 需求: 排除指定需要自动转配的类. 说明: 1.在以上注解中  ...

  4. [原创]MSP430FR4133练习(一):GPIO输入电平状态判断

    硬件环境:MSP430FR4133 LANCHPAD开发板 软件环境:IARV7.10 For 430 源代码: #include "driverlib.h" void main( ...

  5. RDP 数据库简介

    在扩增子数据分析中,有时会发现多个OTU 注释到了同一个species ,  为什么会出现这种情况呢? 首先既然在OTU水平能分开,说明序列的相似度小于97%,  同一个物种的同一个基因的片段相似度会 ...

  6. tcp 三次握手 转

    转载 记得刚毕业找工作面试的时候,经常会被问到:你知道“3次握手,4次挥手”吗?这时候我会“胸有成竹”地“背诵”前期准备好的“答案”,第一次怎么怎么,第二次……答完就没有下文了,面试官貌似也没有深入下 ...

  7. How to set asp.net Identity cookies expires time

    If IsPersistent property of AuthenticationProperties is set to false, then the cookie expiration tim ...

  8. [UFLDL] ConvNet

    二十三(Convolution和Pooling练习)  三十八(Stacked CNN简单介绍) 三十六(关于构建深度卷积SAE网络的一点困惑) 五十(Deconvolution Network简单理 ...

  9. [Bayes] Understanding Bayes: Visualization of the Bayes Factor

    From: https://alexanderetz.com/2015/08/09/understanding-bayes-visualization-of-bf/ Nearly被贝叶斯因子搞死,找篇 ...

  10. WP8.1学习系列(第二十七章)——ListView和GridView入门

    快速入门:添加 ListView 和 GridView 控件 (XAML)   在本文中 先决条件 选择 ListView 或 GridView 将项添加到项集合 设置项目源 指定项目的外观 指定视图 ...