So I found nmap and print(nm.csv()) help needed to print out to csv.file that gave me the code I am working with now. The last few lines of code where the def
is.
But the csv file that is generated has all the data in one column. Is there a way to force it to put each data set in its own column? Or should I stick with using Excel to separate after the fact?
The data I am "printing" is formatted like this; and I believe is a key pair.
'host;hostname;hostname_type;protocol;port;name;state;product;extrainfo;reason;version;conf;cpe'
...
import sys
import os
import nmap
from datetime import datetime
import time
import csv
#Ask for user input and assign IP\subnet and port range.
print("_" * 40) #style options
ip_range = '192.168.3.132' #str(input('\nEnter an IP or subnet: '))
user_ports = '100-200' #str(input('\nEnter Ports to scan: '))
print("\nScanning IP " + ip_range + ". For ports " + user_ports + ".")
#add banner containing time stamps.
before = datetime.now()
print("_" * 40) #style options
print("\nScanning Target "+ip_range)
print('\n') #style options
print("Time Started: "+ str(datetime.now().strftime('%m-%d-%Y %H:%M:%S'))+'\n')
print("~" * 40)#style options
print('\n')#style options
#name scan configuration
nmap_scan = nmap.PortScanner()
nmap_scan.scan(str(ip_range), str(user_ports))
#printing out ports that are up
for host in nmap_scan.all_hosts():
print("Host: %s (%s)" % (host, nmap_scan[host].hostname()))
print("state: %s" % nmap_scan[host].state())
for proto in nmap_scan[host].all_protocols():
print("\n")
print("Protocol used: %s" % proto)
lport = sorted(nmap_scan[host][proto].keys())
for port in lport:
print("Port: %s\tState: %s" % (port, nmap_scan[host][proto][port]['state']))
print('\n')#style options
#export nmap scan to csv file
#export nmap scan to csv file
def save_csv_data(nmap_scan_csv, path='.'):
with open(path + '/output.csv', 'w') as output:
output.write(nmap_scan_csv)
if (len(sys.argv) > 1 and sys.argv[1]):
save_csv_data(nmap_scan.csv(), path=sys.argv[1])
else:
save_csv_data(nmap_scan.csv())
print("_" * 50)#style options
print('\n\n')#style options
#Finished time
after = datetime.now()
print("Time finished: "+ str(datetime.now().strftime('%m-%d-%Y %H:%M:%S')))
#time elapsed during scan
print('\n')#style options
delta = after-before
print("Time elapsed: " + str(delta))
'''