Skip to content

Instantly share code, notes, and snippets.

@pythoninthegrass
Created April 13, 2022 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pythoninthegrass/be089ffc52fb833cc9d8d21ccbaf838a to your computer and use it in GitHub Desktop.
Save pythoninthegrass/be089ffc52fb833cc9d8d21ccbaf838a to your computer and use it in GitHub Desktop.
Ingest CSVs from Cisco Meraki, then filter by IP address, and scan the hosts from the filtered CSV
#!/usr/bin/env python3
import nmap
import pandas as pd
from icecream import ic
from pathlib import Path
# verbose icecream
# ic.configureOutput(includeContext=True)
home = Path.home()
cwd = Path.cwd()
out = f"{cwd}/filtered_clients.csv"
# TODO: combine multiple CSVs
# export filtered results to csv
if not Path(cwd/'filtered_clients.csv').is_file():
files = Path(cwd/'raw').glob('*wireless_clients*.csv') # path glob csv name
files_list = [str(f) for f in files] # list of strings
# pandas loop through fn csv
# df = pd.read_csv(fn)
for f in files_list:
df = pd.read_csv(f)
df = df['IPv4 address'] # read 'IPv4 address' column
df = df.dropna() # drop NaN values
df.drop(df[df == '1.1.1.1'].index, inplace=True)
df.to_csv(out, mode='a', header=False, index=False)
# read new out csv
df = pd.read_csv(out)
# drop duplicate values
ip = df.drop_duplicates()
# create new header
ip.columns = ['IPv4 address']
# print filtered results
ip.to_csv(out, index=False)
# read filtered_clients.csv skip header and only print values
ip = pd.read_csv('filtered_clients.csv', skiprows=1)
# ip.to_string(index=False)
blankIndex=[''] * len(ip)
ip.index = blankIndex
# align rows to left
# ip.style.set_properties(**{'text-align': 'left'})
# convert df to string
ip = ip.to_string(index=False)
# single line from ip
ip = ip.replace('\n', ' ').strip()
print(ip)
# nmap scan
nm = nmap.PortScanner()
# TODO: debug nmap scan (could be how `hosts` are formatted)
# scan ip addresses (skip port scan)
print('Scanning...')
nm.scan(hosts=ip, arguments='-sP')
hosts_list = [(x, nm[x]['status']['state']) for x in nm.all_hosts()]
for host, status in hosts_list:
print('{0}:{1}'.format(host, status))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment