如何一次将列表一个元素的内容传递给方法?

How to pass the content of list one element at a time to a method?

我有一个 libnmap 扫描器脚本,它基本上通过从 AWS 收集所有 EIP 并一个一个扫描它们来工作,收集所有 EIP 的函数如下所示:

def gather_public_ip():
    ACCESS_KEY = config.get('aws','access_key')
    SECRET_KEY = config.get('aws','secret_key')
    regions = regions = ['us-west-2','eu-central-1','ap-southeast-1']
    all_EIP = []
    for region in regions:
       client = boto3.client('ec2',aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY,region_name=region,)
       addresses_dict = client.describe_addresses()
       for eip_dict in addresses_dict['Addresses']:
         if 'PrivateIpAddress' in eip_dict:
            print eip_dict['PublicIp']
            all_EIP.append(eip_dict['PublicIp'])
       print all_EIP
       return all_EIP

这个函数基本上是 returns 我的一个列表,看起来像

['22.22.124.141', '22.21.149.191', '22.11.132.122', '22.11.227.241', '22.34.28.112', '22.34.211.227', '22.27.21.233',  '22.24.199.122', '22.11.113.171', '22.21.11.8', '22.33.31.14', '22.37.19.213', '22.24.121.112', '22.32.121.132', '22.24.21.1', '22.34.72.198']

我调用上述方法的主要函数然后将其传递给我的实际扫描仪函数,如下所示:

  s = Scanner(config)

        # Execute Scan and Generate latest report
        net_range =  gather_public_ip() # config.get('sources','networks')  ## Call DEF
        #print type(net_range)
        r = s.run(net_range)
        s.save()   # save to pickle

扫描仪 class 看起来像:

class Scanner(object):
    """Container for all scan activies"""

    def __init__(self,cp):
        self.config = cp # read in ConfigParser object to get settings
        self.report = None

    def gather_targets(self):
        """Gather list of targets based on configured sources"""
        pass

    def run(self, targets="" ,options="-Pn"):
        #start a new nmap scan on localhost with some specific options

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

谁能帮我解决我可以将列表中的值一个一个地传递给 运行 方法以便 nmap 可以处理它的部分,现在它只是闲置

你传递参数的方式看起来不错。但是,您没有任何实际使用结果的代码。

尝试像这样更改 Scanner.run

class Scanner(object):
    ...
    def run(self, targets="" ,options="-Pn"):
        #start a new nmap scan on localhost with some specific options

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

        nmproc.run_background()
        while nmproc.is_running():
            print("Nmap Scan running: ETC: {0} DONE: {1}%".format(nmproc.etc,
                                                                  nmproc.progress))
            sleep(2)

        print("rc: {0} output: {1}".format(nmproc.rc, nmproc.summary)) 

这是直接取自 the docs