0

I am creating a l7 firewall mass update python script using the meraki api and I have run into an issue. I need the script to iterate through for each line item in the .txt file. Each line item in the .txt file is a network id. Currently the way I have my code set up it only runs once with the network id that is at the top of the list. How can I remedy this?

import requests
import json
import os
from dotenv import load_dotenv

load_dotenv()
api_key = os.environ.get("API_Key")

with open('testnetids.txt') as file:
    for line in file:
        line = line.rstrip("\n")

url = "https://api.meraki.com/api/v0/networks/%s/l7FirewallRules" % line

with open("layer7rules.json") as f:
    payload = json.load(f)

headers = {
    'X-Cisco-Meraki-API-Key': api_key,
    'Content-Type': 'application/json',
}

response = requests.put(url, data=json.dumps(payload), headers=headers)

print(response.text)

1 Answers1

0

in order to do some processing for each line in the file, you need to move the actual processing inside the while loop:

import requests
import json
import os
from dotenv import load_dotenv

load_dotenv()
api_key = os.environ.get("API_Key")

with open('testnetids.txt') as file:
    for line in file:
        line = line.rstrip("\n")

        url = "https://api.meraki.com/api/v0/networks/%s/l7FirewallRules" % line

        with open("layer7rules.json") as f:
            payload = json.load(f)

            headers = {
                'X-Cisco-Meraki-API-Key': api_key,
                'Content-Type': 'application/json',
            }

            response = requests.put(url, data=json.dumps(payload), headers=headers)

            print(response.text)