Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. def get_software(ip, port):
  2. """
  3. The function gets the software name by it's ip and port
  4.  
  5. :param ip: IP Address
  6. :param port: Port
  7. :return: Software or 'Unknown'
  8. """
  9.  
  10. software = ""
  11. address = ip + ":" + str(port)
  12. connections = popen("netstat -nb").readlines()[4:]
  13.  
  14. # Squizing the lines, I want to empty lines here
  15. for i in range(len(connections)):
  16. connections[i] = connections[i].replace("\n", "")
  17.  
  18. # Looking for each ip address
  19. for i in range(len(connections)):
  20. line = connections[i]
  21.  
  22. # If it is not a software name
  23. if not(line.startswith(" [")):
  24. # Slicing 32:54 to get the exact ip (Does not matter if also the port is 4 or 5 digits!)
  25. line = connections[i][32:54]
  26.  
  27. # The line may include some extra spaces, because of the port length change,
  28. # so checking whether the address is inside
  29. if address in line:
  30. # Software name is in the next line, slicing from 2:-1 to get just it's name
  31. return connections[i + 1][2:-1]
  32.  
  33. return "Unknown"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement