Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mac address and mac vendor #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions xml2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,28 @@ def get_host_data(root):
hosts = root.findall('host')
for host in hosts:
addr_info = []
mac_address = ""
mac_vendor = ""

# Ignore hosts that are not 'up'
if not host.findall('status')[0].attrib['state'] == 'up':
continue

# Get IP address and host info. If no hostname, then ''
ip_address = host.findall('address')[0].attrib['addr']

try:
# Get MAC address and host info. If no hostname, then ''
mac_address = host.findall('address')[1].attrib['addr']
except IndexError:
mac_address = ''

try:
# Get MAC vendor and host info. If no hostname, then ''
mac_vendor = host.findall('address')[1].attrib['vendor']
except IndexError:
mac_vendor = ''

host_name_element = host.findall('hostnames')
try:
host_name = host_name_element[0].findall('hostname')[0].attrib['name']
Expand Down Expand Up @@ -90,7 +105,7 @@ def get_host_data(root):
script_output = ''

# Create a list of the port data
port_data.extend((ip_address, host_name, os_name,
port_data.extend((ip_address, mac_address, mac_vendor, host_name, os_name,
proto, port_id, service, product,
servicefp, script_id, script_output))

Expand All @@ -99,7 +114,7 @@ def get_host_data(root):

# If no port information, just create a list of host information
except IndexError:
addr_info.extend((ip_address, host_name))
addr_info.extend((ip_address, mac_address, mac_vendor, host_name))
host_data.append(addr_info)
return host_data

Expand All @@ -124,7 +139,7 @@ def parse_to_csv(data):
csv_file = open(csv_name, 'w', newline='')
csv_writer = csv.writer(csv_file)
top_row = [
'IP', 'Host', 'OS', 'Proto', 'Port',
'IP', 'MAC', 'Vendor', 'Host', 'OS', 'Proto', 'Port',
'Service', 'Product', 'Service FP',
'NSE Script ID', 'NSE Script Output', 'Notes'
]
Expand Down Expand Up @@ -232,7 +247,7 @@ def print_data(data):

def main():
"""Main function of the script."""
for filename in args.filename:
for filename in args.filename:

# Checks the file path
if not os.path.exists(filename):
Expand Down