Paste P6507

naughty_detector.py
ActivePublic

Authored by zhuyifei1999 on Sun, Dec 31, 5:19 AM.
1#! /usr/bin/env python3
2
3import os
4import time
5
6os.chdir('/proc')
7
8next_data = set()
9
10while True:
11last_data, next_data = next_data, set()
12
13for proc in os.listdir():
14try:
15int(proc)
16except ValueError:
17continue
18
19try:
20with open(os.path.join(proc, 'stat'), 'rb') as f:
21data = f.read().split(b' ')
22except OSError:
23continue
24
25if data[2] == b'D':
26try:
27with open(os.path.join(proc, 'cmdline'), 'rb') as f:
28cmdline = repr(f.read().rstrip(b'\x00').split(b'\x00'))
29except OSError:
30cmdline = '(Unknown)'
31
32v = proc, cmdline
33next_data.add(v)
34
35if v in last_data:
36print('PID %s: %s' % v)
37
38time.sleep(10)
bd808 added a subscriber: bd808.Sun, Dec 31, 5:20 AM
zhuyifei1999 updated the paste's language from autodetect to python.Mon, Jan 1, 5:58 AM
zhuyifei1999 edited the content of this paste. (Show Details)