0
votes

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

Blockquote

1
Note i'm trying to return the resp command send to device. Again print works perfect but the return ip + '\n + best_match + resp code only returns data from last device in the loop. - kbirds30
Create a dict that will have the ip as key and the resp as value. Return this dict from the function 'firewall' - balderman
Hello balderman i'm not sure i'm following you can you please give me a basic example. - kbirds30
I am adding an example. - balderman

1 Answers

1
votes

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