PythonFreak PythonFreak - 1 year ago 128
Python Question

How can i pass the ip address to a nmap scanner?

I have a code which basically returns 3 things

ip, instance_tag_name, availability_zone
as follows :

def call_me ():
regions = ['us-west-2','eu-central-1','ap-southeast-1']
combined_shit = []
for region in regions:
instance_information = {}
ip_dict = {}
client = boto3.client('ec2',aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY,region_name=region,)
addresses_dict = client.describe_addresses().get('Addresses')

for address in addresses_dict:

if address.get('InstanceId'):
instance_information[address['InstanceId']] = [address.get('PublicIp')]
# print instance_information

dex_dict = client.describe_tags().get('Tags')
for dex in dex_dict:
if instance_information.get(dex['ResourceId']):
for instance in instance_information:

if len(instance_information[instance]) == 2:
ip_dict[instance_information[instance][0]] = instance_information[instance][1]
ip_dict[instance_information[instance][0]] = ''
# print (json.dumps(instance_information,indent=4))
#print type(instance_information)
ip_list = [i[0] for i in instance_information.values()]
tag_list = [i[1] for i in instance_information.values()]
region_list = []
for i in instance_information.values():
#print (ip_list)
combined_result = list(zip(ip_list, tag_list, region_list))
return str(combined_shit)

function returns something like this :

[[('', ' Exclusive LB', ''), ('', 'pdx-cloud-1-LB', 'us-west-2'), ('', 'SDlusive LB', ''),........]]

I have a Nmap parser written which basically takes in the IP addresses and scans it, now how can i pass the above list of addresses to function such that it reads on the IP's address ?

The nmap caller looks like

net_range = call_me()
r =

The run() method looks like :

def run(self, targets="" ,options="-Pn -p 80,443,22,8980,5601,3306,9200,8080,3128"):
#start a new nmap scan on localhost with some specific options

syslog.syslog("Scan started")
parsed = None
nmproc = NmapProcess(targets,options)
rc =
while nmproc.is_running()

So the question is how can i pass just the IP's to this caller & there is yet another reason to keep the rest information. But first if this can be solved

Answer Source

I think your NMAP run requires a comma separated list of IPs. Here's how to do it.

r =  

net_range is a list of tuples which have IPs at index 0. We get the list of IPs and join it to create a comma separated string of IPs.

Change your run as follows.

def run(self, data ,options="-Pn  -p 80,443,22,8980,5601,3306,9200,8080,3128"):
    #start a new nmap scan on localhost with some specific options
    targets=','.join(list(itertools.chain(*[[j[0] for j in i] for i in data])))
    tags = list(itertools.chain(*[[j[1] for j in i] for i in data]))
    regions = list(itertools.chain(*[[j[2] for j in i] for i in data]))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download