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.
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.
@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
}
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"
}
]
}
@Austin_Campbell : Your Jason seems correct, Check and validate below your script.
Thanks for the link. I tested and it does pass as valid json.
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:
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.
Can you use Post method ?
No.
@Austin_Campbell : Change the URL as well fron v0 to v1 as well.
url = "https://api.meraki.com/api/v0/networks
I have tried this as well no luck with v0 or v1 same error.
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.
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".
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
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.