GatherInformationOfIpA.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import subprocess
  2. # a function that gathers more information about a given IP Address
  3. def gatherInformationOfIpA(ipToCheck):
  4. descr = []
  5. country = []
  6. source = []
  7. autSys = []
  8. nothingFound = False
  9. descrFound = False
  10. countryFound = False
  11. sourceFound = False
  12. # execute 'whois' on the command line and save output to t
  13. t = subprocess.run(['whois', ipToCheck], stdout=subprocess.PIPE)
  14. # save generated output of shell command to a file
  15. with open("output.txt", "w") as output:
  16. output.write(t.stdout.decode('utf-8'))
  17. # parse information, like Description, Country, Source and if found the ASN
  18. with open("output.txt", "r", encoding="utf-8", errors='replace') as ripeDb:
  19. ipInfos = [line.split() for line in ripeDb if line.strip()]
  20. for i, row in enumerate(ipInfos):
  21. if any("inetnum" in s for s in row):
  22. if ipToCheck >= row[1] and ipToCheck <= row[3]:
  23. for local in range(1, 20):
  24. if ("descr:" in ipInfos[i + local]) and not descrFound:
  25. descr.extend(ipInfos[i + local][1:])
  26. descrFound = True
  27. continue
  28. if ("country:" in ipInfos[i + local]) and not countryFound:
  29. country.extend(ipInfos[i + local][1:])
  30. countryFound = True
  31. continue
  32. if ("source:" in ipInfos[i + local]) and not sourceFound:
  33. source.extend(ipInfos[i + local][1:])
  34. sourceFound = True
  35. break
  36. if any("origin" in s for s in row):
  37. autSys.extend(row[1:])
  38. break
  39. if not descrFound or not countryFound or not sourceFound:
  40. nothingFound = True
  41. # print information (which use of this information is wanted? Output, Returned?)
  42. if not nothingFound:
  43. print("#############################################")
  44. print("More Information about", ipToCheck)
  45. print("Description: ", ' '.join(descr) if descr else "unknown")
  46. print("Country: ", ' '.join(country) if country else "unknown")
  47. print("Source: ", ' '.join(source) if source else "unknown")
  48. print("AS Number: ", ' '.join(autSys) if autSys else "unknown")
  49. print("#############################################")
  50. print("\n")
  51. else:
  52. print("IP-Address", ipToCheck, "is not assigned by IANA yet\n")
  53. # in case it should be stored to a file
  54. with open("information.txt", "w") as info:
  55. info.write("#############################################\n")
  56. info.write("More Information about" + ipToCheck + "\n")
  57. info.write("Description: " + ' '.join(descr) + "\n" if descr else "unknown" + "\n")
  58. info.write("Country: " + ' '.join(country) + "\n" if country else "unknown" + "\n")
  59. info.write("Source: " + ' '.join(source) + "\n" if source else "unknown" + "\n")
  60. info.write("AS Number: " + ' '.join(autSys) + "\n" if autSys else "unknown" + "\n")
  61. info.write("#############################################\n")