05-05-2021 12:32 PM
I am creating a script for mass l7 firewall changes and I am running into an error. My code is below. I have made sure that my layer7rules.json file is properly formatted in json.
import requests
import json
import os
from dotenv import load_dotenv
load_dotenv()
api_key = os.environ.get("API_Key")
with open("layer7rules.json") as f:
payload = json.load(f)
with open("testnetids.txt") as file:
array = file.readlines()
for line in array:
line = line.rstrip("\n")
url = "https://api.meraki.com/api/v0/networks/%s/l7FirewallRules" % line
headers = {
'Content-Type': 'application/json',
'X-Cisco-Meraki-API-Key': api_key
}
response = requests.request("PUT", url, headers=headers, data=payload)
print(response.text)
Solved! Go to Solution.
05-11-2021 06:52 AM
Thank you for your suggestion. I was able to figure out the issue and the code is working. I had to add data=json.dumps(payload) because json.load() strips the double quotations required for json when converting the file to a python dictionary.
05-05-2021 12:46 PM
@Austin_Campbell : I think the below code from your script should be in double quotes. What error message you are getting ?
headers = {
'Content-Type': 'application/json',
'X-Cisco-Meraki-API-Key': api_key
}
05-05-2021 12:54 PM
I am getting the error "Bad request: There was a problem in the JSON you submitted". Tried with double quotes same result. Here is my json file.
{
"rules": [
{
"policy": "deny",
"type": "blacklistedCountries",
"value": [
"CN",
"CU",
"HK",
"ID",
"IR",
"LY",
"LT",
"MY",
"MX",
"NE",
"KP",
"PK",
"RU",
"SC",
"KR",
"SY",
"TW",
"TH",
"TR",
"UA",
"AE",
"YE"
]
},
{
"policy": "deny",
"type": "ipRange",
"value": "80.82.77.139/32"
},
{
"policy": "deny",
"type": "ipRange",
"value": "149.202.120.37/32"
},
{
"policy": "deny",
"type": "ipRange",
"value": "208.91.197.27/32"
},
{
"policy": "deny",
"type": "ipRange",
"value": "217.174.152.68/32"
},
{
"policy": "deny",
"type": "ipRange",
"value": "193.56.28.125/32"
},
{
"policy": "deny",
"type": "ipRange",
"value": "92.242.140.21/32"
},
{
"policy": "deny",
"type": "ipRange",
"value": "185.165.190.34/32"
},
{
"policy": "deny",
"type": "ipRange",
"value": "223.71.167.163/32"
}
]
}
05-05-2021 12:58 PM
@Austin_Campbell : Your Jason seems correct, Check and validate below your script.
05-05-2021 01:00 PM
Thanks for the link. I tested and it does pass as valid json.
05-05-2021 01:01 PM
and also Replace the base URL with the actual shard you are on. Google script does not seem to follow the redirect. E.g. https://n248.meraki.com/api.... instead of https://api.meraki.com ... More info about that issue here:
05-05-2021 01:07 PM
No luck with this change either. I believe this to be a specific problem with the python script and the way I am loading the json file into the api call.
05-05-2021 01:16 PM
Can you use Post method ?
05-05-2021 01:18 PM
No.
05-05-2021 12:59 PM
@Austin_Campbell : Change the URL as well fron v0 to v1 as well.
url = "https://api.meraki.com/api/v0/networks
05-05-2021 01:02 PM
I have tried this as well no luck with v0 or v1 same error.
05-05-2021 04:23 PM
Try setting up one entry exactly how you would like in the dashboard, then 'get' those settings and look at the json return. In fact, you should be able to use that json for your future put calls.
05-06-2021 07:46 AM
The json file that I have was produced from a postman meraki api "get" of our standard l7 rules. I am able to use a json.dump of the exact json I have above and it works successfully for a "put".
05-11-2021 03:31 AM
Hi,
Much easier is it like this :
We have Class B and C vessels..... each Class has a different set of rules...
Just read the json file and push it into the network
Best regards
Ed
05-11-2021 06:52 AM
Thank you for your suggestion. I was able to figure out the issue and the code is working. I had to add data=json.dumps(payload) because json.load() strips the double quotations required for json when converting the file to a python dictionary.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide