Advertisement
Vespertilio

Untitled

Jan 18th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. import psutil, time, json, ast, re
  2. from IPy import IP
  3. from tikapy import TikapyClient, ApiError, ClientError
  4. from pprint import pprint
  5.  
  6. listName = "steam-netblocks"
  7. processName = "steam_osx"
  8.  
  9. rosLogin = "god"
  10. rosPassword = "donotmasturb"
  11. rosIP = "192.168.88.1"
  12. rosPort = 8729 #without SSL
  13.  
  14. pids = []
  15. ipsInList = []
  16. for proc in psutil.process_iter():
  17.     try:
  18.         pinfo = proc.as_dict(attrs=['pid', 'name'])
  19.         if  pinfo['name'] == processName:
  20.             pids.append(pinfo['pid'])
  21.     except psutil.NoSuchProcess:
  22.         print("Process not found.")
  23.         break
  24.  
  25. while True:
  26.     for pid in pids:
  27.         p = psutil.Process(pid)
  28.  
  29.         for connection in p.connections():
  30.  
  31.             if connection.status == 'ESTABLISHED':
  32.                 remoteIP = connection.raddr[0]
  33.                 remotePort = connection.raddr[1]
  34.  
  35.                 if IP(remoteIP).iptype() != 'PRIVATE' and not (remoteIP in ipsInList):
  36.                     try:
  37.                         client = TikapyClient(rosIP)
  38.                         client.login(rosLogin, rosPassword)
  39.  
  40.                         pprint(client.talk(['/ip/firewall/address-list/add', '=list=' + listName, '=address=' + remoteIP]))
  41.  
  42.                     except ClientError as e:
  43.                         _er = str(e.__context__)
  44.                         error = json.loads(json.dumps(ast.literal_eval(_er)))
  45.  
  46.                         if re.search(r"already have such entry", error["message"]):
  47.                             ipsInList.append(remoteIP)
  48.  
  49.                         pass
  50.  
  51.                     except Exception as e:
  52.                         print(e)
  53.                         break
  54.                     print("Address: " + str(remoteIP) + " added to list. \n")
  55.  
  56.     time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement