nmap-get.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/python
  2. import sys
  3. import json
  4. from rich.console import Console
  5. from rich.table import Table
  6. import argparse
  7. # Setup Table
  8. console = Console()
  9. table = Table(show_header=True, header_style="bold blue")
  10. table.add_column("Host", style="dim")
  11. table.add_column("IP", style="dim")
  12. table.add_column("Port")
  13. table.add_column("Service")
  14. table.add_column("Version", justify="left")
  15. def pprint_table(hostlist, iponly):
  16. global table
  17. for host in hostlist:
  18. if iponly:
  19. print(host["ip"])
  20. else:
  21. for port in host["ports"]:
  22. table.add_row(
  23. host["hostname"],
  24. host["ip"],
  25. str(port["port_number"]),
  26. port["service"],
  27. port["version"]
  28. )
  29. if not iponly:
  30. console.print(table)
  31. def filter_by_port(port):
  32. global hosts
  33. out = []
  34. ports = []
  35. for host in hosts:
  36. for p in host["ports"]:
  37. if port == str(p["port_number"]) and p["state"] == "open":
  38. ports.append(p)
  39. host["ports"] = ports
  40. ports = []
  41. out.append(host)
  42. return out
  43. def filter_by_version(version):
  44. global hosts
  45. out = []
  46. ports = []
  47. for host in hosts:
  48. for p in host["ports"]:
  49. if version.lower() in p["version"].lower():
  50. ports.append(p)
  51. host["ports"] = ports
  52. ports = []
  53. out.append(host)
  54. return out
  55. # Setup Argument Parser
  56. parser = argparse.ArgumentParser(description='Filtering nmap')
  57. parser.add_argument('file', action='store', nargs='?',
  58. help='Input File')
  59. parser.add_argument('--port', dest='port', action='store',
  60. help='Filter by port number')
  61. parser.add_argument('--version', dest='version', action='store',
  62. help='Filter by version string')
  63. parser.add_argument('--ip', dest='ip', action='store_true',
  64. help='Only print the ips')
  65. args = parser.parse_args()
  66. if args.file:
  67. with open(args.file, "r") as inp_file:
  68. hosts = json.loads(inp_file.read())
  69. else:
  70. hosts = json.loads(sys.stdin.read())
  71. if args.port:
  72. pprint_table(filter_by_port(args.port), args.ip)
  73. elif args.version:
  74. pprint_table(filter_by_version(args.version), args.ip)
  75. else:
  76. pprint_table(hosts, args.ip)