PythonFreak PythonFreak - 5 months ago 23
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 ():
ACCESS_KEY= "XXXXXXXXXXXX"
SECRET_KEY= "XXXXXXXXXXXXXXXXXXX"
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']):
instance_information[dex['ResourceId']].append(dex.get('Value'))
for instance in instance_information:

if len(instance_information[instance]) == 2:
ip_dict[instance_information[instance][0]] = instance_information[instance][1]
else:
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():
try:
region_list.append(i[3])
except:
region_list.append("")
#print (ip_list)
combined_result = list(zip(ip_list, tag_list, region_list))
combined_shit.append(combined_result)
return str(combined_shit)


The
combined_shit
function returns something like this :

[[('52.1.113.171', ' Exclusive LB', ''), ('52.26.2.83', 'pdx-cloud-1-LB', 'us-west-2'), ('51.25.14.146', '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 = s.run(net_range)


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 = nmproc.run()
nmproc.run_background()
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

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

r = s.run(net_range)  

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]))