Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
tslagame network usage inspector
import os
import psutil
import re
def find_procs_by_name(name):
"Return a list of processes matching 'name'."
assert name, name
ls = []
for p in psutil.process_iter():
name_, exe, cmdline = "", "", []
try:
name_ = p.name()
cmdline = p.cmdline()
exe = p.exe()
except (psutil.AccessDenied, psutil.ZombieProcess):
pass
except psutil.NoSuchProcess:
continue
if len(cmdline)!=0:
if name == name_ or cmdline[0] == name or os.path.basename(exe) == name:
conns = p.connections()
validconnAdd=[]
for conn in conns:
if len(conn.raddr)!=0:
addr = conn.raddr[0]
if isWanIp(addr):
validconnAdd.append(conn.raddr)
ls.append([name,p.pid,validconnAdd])
else:
if name == name_ or os.path.basename(exe) == name:
conns = p.connections()
validconnAdd=[]
for conn in conns:
if len(conn.raddr)!=0:
addr = conn.raddr[0]
if isWanIp(addr):
validconnAdd.append(conn.raddr)
ls.append([name,p.pid,validconnAdd])
return ls
def isWanIp(ip):
aa=re.match(r"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$",ip)
if aa.group(0)==ip:
bb=re.match(r"^(\d+)\.(\d+)",ip)
first = int(bb.group(1))
sec = int(bb.group(2))
if(first==0 or first==10 or (first==172 and sec>=16 and sec <= 31 ) or (first ==192 and sec ==168 ) or ip=='127.0.0.1'):
return False
else:
return True
else:
if ip.lower()=='localhost':
return False
raise Exception('not a valid ip address')
if __name__ == "__main__":
import time
while(1):
ls = find_procs_by_name('TslGame.exe')
# ls = find_procs_by_name('chrome.exe')
if len(ls)!=0:
print ls
time.sleep(3)
else:
time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment