Created
December 7, 2017 08:08
-
-
Save sandyUni/10e9a7a4daa42057c8fafbfe1f5351af to your computer and use it in GitHub Desktop.
tslagame network usage inspector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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