Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_software(ip, port):
- """
- The function gets the software name by it's ip and port
- :param ip: IP Address
- :param port: Port
- :return: Software or 'Unknown'
- """
- software = ""
- address = ip + ":" + str(port)
- connections = popen("netstat -nb").readlines()[4:]
- # Squizing the lines, I want to empty lines here
- for i in range(len(connections)):
- connections[i] = connections[i].replace("\n", "")
- # Looking for each ip address
- for i in range(len(connections)):
- line = connections[i]
- # If it is not a software name
- if not(line.startswith(" [")):
- # Slicing 32:54 to get the exact ip (Does not matter if also the port is 4 or 5 digits!)
- line = connections[i][32:54]
- # The line may include some extra spaces, because of the port length change,
- # so checking whether the address is inside
- if address in line:
- # Software name is in the next line, slicing from 2:-1 to get just it's name
- return connections[i + 1][2:-1]
- return "Unknown"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement