3
0

GatherInformationOfIpA.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import subprocess
  2. import os as os
  3. # a function that gathers more information about a given IP Address
  4. def gatherInformationOfIpA(ipToCheck, keepInformation=False):
  5. '''
  6. This functin gathers some information of an IP Address, like Organization, Country, Source of Information
  7. and the ASN. The command line funciton 'whois' is required
  8. :param ipToCheck: String with the IP Address, which is checked output
  9. :param keepInformation: true, if the parsed information should be stored in a file
  10. '''
  11. descr = []
  12. country = []
  13. source = []
  14. autSys = []
  15. nothingFound = False
  16. descrFound = False
  17. countryFound = False
  18. sourceFound = False
  19. inRange = False
  20. originFound = False
  21. ripe = False
  22. # execute 'whois' on the command line and save output to t
  23. t = subprocess.run(['whois', ipToCheck], stdout=subprocess.PIPE)
  24. # save generated output of shell command to a file
  25. with open("../../resources/output.txt", "w") as output:
  26. output.write(t.stdout.decode('utf-8'))
  27. # parse information, like Description, Country, Source and if found the ASN
  28. with open("../../resources/output.txt", "r", encoding="utf-8", errors='replace') as ripeDb:
  29. ipInfos = [line.split() for line in ripeDb if line.strip()]
  30. # check if IP is from RIPE
  31. for i, row in enumerate(ipInfos):
  32. if any("RIPE" in s for s in row) or any ("Ripe" in s for s in row):
  33. ripe = True
  34. break
  35. if ripe:
  36. # parse information about ip
  37. for i, row in enumerate(ipInfos):
  38. if any("inetnum" in s for s in row) and not inRange:
  39. # check whether ipToCheck is in range of the current found inetnum or NetRange
  40. if ipToCheck >= row[1] and ipToCheck <= row[3]:
  41. inRange = True
  42. if any("descr:" in s for s in row) and not descrFound:
  43. descr.extend(ipInfos[i][1:])
  44. descrFound = True
  45. continue
  46. if any("country:" in s for s in row) and not countryFound:
  47. country.extend(ipInfos[i][1:])
  48. countryFound = True
  49. continue
  50. if any("source:" in s for s in row) and not sourceFound:
  51. source.extend(ipInfos[i][1:])
  52. sourceFound = True
  53. continue
  54. if any("origin" in s for s in row) and not originFound:
  55. autSys.extend(row[1:])
  56. originFound = True
  57. continue
  58. if inRange and descrFound and countryFound and sourceFound and originFound:
  59. break
  60. else:
  61. # parse information about ip
  62. for i, row in enumerate(ipInfos):
  63. if any("inetnum" in s for s in row) or any("NetRange" in s for s in row) and not inRange:
  64. # check whether ipToCheck is in range of the current found inetnum or NetRange
  65. if ipToCheck >= row[1] and ipToCheck <= row[3]:
  66. inRange = True
  67. if (any("descr:" in s for s in row) or any("Organization:" in s for s in row)) and not descrFound:
  68. descr.extend(ipInfos[i][1:])
  69. descrFound = True
  70. continue
  71. if (any("country:" in s for s in row) or any("Country:" in s for s in row)) and not countryFound:
  72. country.extend(ipInfos[i][1:])
  73. countryFound = True
  74. continue
  75. if (any("source:" in s for s in row) or any("Ref:" in s for s in row)) and not sourceFound:
  76. source.extend(ipInfos[i][1:])
  77. sourceFound = True
  78. continue
  79. if (any("origin" in s for s in row) or any("OriginAS:" in s for s in row)) and not originFound:
  80. autSys.extend(row[1:])
  81. originFound = True
  82. continue
  83. if inRange and descrFound and countryFound and sourceFound and originFound:
  84. break
  85. if not descrFound and not countryFound and not sourceFound and not originFound and not inRange:
  86. nothingFound = True
  87. # print information (which use of this information is wanted? Output, Returned?)
  88. if not nothingFound:
  89. print("#############################################")
  90. print("More Information about", ipToCheck)
  91. print("Description: ", ' '.join(descr) if descr else "unknown")
  92. print("Country: ", ' '.join(country) if country else "unknown")
  93. print("Source: ", ' '.join(source) if source else "unknown")
  94. print("AS Number: ", ' '.join(autSys) if autSys else "unknown")
  95. print("#############################################")
  96. print("\n")
  97. else:
  98. print("IP-Address", ipToCheck, "is not assigned by IANA yet\n")
  99. # in case it should be stored to a file
  100. if keepInformation and not nothingFound:
  101. with open("../../resources/information.txt", "w") as info:
  102. info.write("#############################################\n")
  103. info.write("More Information about" + ipToCheck + "\n")
  104. info.write("Description: ")
  105. info.write(' '.join(descr) + "\n" if descr else "unknown" + "\n")
  106. info.write("Country: ")
  107. info.write(' '.join(country) + "\n" if country else "unknown" + "\n")
  108. info.write("Source: ")
  109. info.write(' '.join(source) + "\n" if source else "unknown" + "\n")
  110. info.write("AS Number: ")
  111. info.write(' '.join(autSys) + "\n" if autSys else "unknown" + "\n")
  112. info.write("#############################################\n")
  113. os.remove("../../resources/output.txt")