Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从循环返回所有数据_Python_Loops_Return - Fatal编程技术网

Python 从循环返回所有数据

Python 从循环返回所有数据,python,loops,return,Python,Loops,Return,我有登录到设备的代码。我可以从循环中的设备打印通知。但我只能返回,不能打印列表中最后一台设备的数据。如何从循环中的所有设备返回所有数据 From flask import Flask, jsonify, request 导入netmiko 从netmiko.ssh\u自动检测导入SSHDetect 从netmiko.ssh_异常导入NetMikoTimeoutException 导入时间 '应用程序=烧瓶名称 @app.route'/firewall',methods=['GET','POST

我有登录到设备的代码。我可以从循环中的设备打印通知。但我只能返回,不能打印列表中最后一台设备的数据。如何从循环中的所有设备返回所有数据

From flask import Flask, jsonify, request
导入netmiko 从netmiko.ssh\u自动检测导入SSHDetect 从netmiko.ssh_异常导入NetMikoTimeoutException 导入时间

'应用程序=烧瓶名称

@app.route'/firewall',methods=['GET','POST','DELETE']

def防火墙:

# Authentication
headers = request.headers
auth = headers.get("xxxxx")
if auth == 'xxxx':

    data = request.get_json(force=True)
    fw_a = data["DeviceAddressList"]
    src_a = data['SourceAddressList']
    src_p = data['SourcePortList']
    dst_a = data['DestinationAddressList']
    dst_p = data['DestinationPortList']
    policy = data["PolicyAllow"]
    p_col = data['Protocol']
    p_show = data['show']
    p_push = data['push']
    config = data['config']

    # Juniper Normalize the data for command line interface
    juniper_command = '"({})"'.format('|'.join(src_a + src_p + dst_a + dst_p))

    username = "xxxx"
    password = "Pxxxx"


    try:
        ip_list = fw_a
        for ip in ip_list:
            #print(ip)
            device = {"device_type": "autodetect", "username": username, "host": ip, "password": password}
            guesser = SSHDetect(**device)
            best_match = guesser.autodetect()
            print(best_match)
            if "None" in str(best_match):
                continue
            #else:
            if "true" in str(p_show) and "juniper_junos" in str(best_match):
                device["device_type"] = best_match
                connection = netmiko.ConnectHandler(**device,)
                connection.find_prompt(delay_factor=2)
                time.sleep(1)
                connection.enable()
                resp = connection.send_command(
                    'show configuration | display xml | match ' + str(juniper_command), delay_factor=2)
                print(ip + '\n' + best_match + resp)


            if "true" in str(p_push) and "juniper_junos" in str(best_match):
                device["device_type"] = best_match
                connection = netmiko.ConnectHandler(**device)
                connection.find_prompt(delay_factor=2)
                time.sleep(1)
                connection.enable()
                push_resp = connection.send_command(config, delay_factor=2)
                connection.disconnect()
                print(push_resp)

        return ip + '\n' + best_match + resp

    except NetMikoTimeoutException:
        return "This Network Device is not reachable"
else:
返回jsonify{message:ERROR:Unauthorized},401

大宗报价


代码示例:在ip上循环,获取要为每个ip返回的值并将其推送到dict中。将dict返回给函数“firewall”的调用方

def firewall():
  result = dict()
  for ip in ip_list:
    push_resp = dummy_get_push_resp()
    result[ip] = push_resp
  return result 

注意,我正在尝试将resp命令返回发送到设备。同样,打印工作正常,但返回的ip+'\n+最佳匹配+resp代码仅返回循环中最后一个设备的数据。创建一个dict,将ip作为键,resp作为值。从“防火墙”函数返回此命令你好,balderman我不确定我是否在跟踪你,你能给我一个基本示例吗?我正在添加一个示例。