Paste P6507

naughty_detector.py
ActivePublic

Authored by zhuyifei1999 on Dec 31 2017, 5:19 AM.
#! /usr/bin/env python3
import datetime
import os
import time
os.chdir('/proc')
next_data = set()
while True:
last_data, next_data = next_data, set()
for proc in os.listdir():
try:
int(proc)
except ValueError:
continue
try:
with open(os.path.join(proc, 'stat'), 'rb') as f:
data = f.read().split(b' ')
except OSError:
continue
if data[2] == b'D':
try:
with open(os.path.join(proc, 'cmdline'), 'rb') as f:
cmdline = repr(f.read().rstrip(b'\x00').split(b'\x00'))
except OSError:
cmdline = '(Unknown)'
v = proc, cmdline
next_data.add(v)
if v in last_data:
print('[%s] PID %s: %s' % ((datetime.datetime.now().isoformat(),) + v))
time.sleep(10)
zhuyifei1999 updated the paste's language from autodetect to python.Jan 1 2018, 5:58 AM
zhuyifei1999 edited the content of this paste. (Show Details)
zhuyifei1999 edited the content of this paste. (Show Details)Jan 25 2018, 5:13 PM