kbirds30
kbirds30

Reputation: 49

return all data from a loop

I have code that logs into devices. I can print the inform from the devices in the loop just fine. But i can only return "not print" the data from the last device in the list. How can i return all data from all devices on the loop ?

from flask import Flask, jsonify, request
import netmiko
from netmiko.ssh_autodetect import SSHDetect
from netmiko.ssh_exception import NetMikoTimeoutException
import time

app = Flask(__name__)

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

def firewall():

    # 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:
  return jsonify({"message": "ERROR: Unauthorized"}), 401

Upvotes: 0

Views: 211

Answers (1)

balderman
balderman

Reputation: 23825

Code example: Loop over the ips, get the value you want to return for each ip and push it into a dict. Return the dict to the caller of the function 'firewall'

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

Upvotes: 1

Related Questions