最像我尝试学习python来发展自己的职业等...作为我们正在考虑在工作中部署Cisco Meraki的一部分。由于我们将拥有大量的网络,因此能够通过API从csv文件中部署信息将非常有用。
我设法使以下代码在一定程度上正常工作。它会成功运行,但只会运行CSV的最后一行,而不是每一行,直到没有剩余行为止。
CSV大约包含130行子网,ip地址和网络ID
我是如此亲密,但我似乎无法弄清这最后一点。
# actionBatch-VlanUpdate.py
import requests
import csv
# Environment Variables
API_KEY = "xxxxxx"
org_id = xxxxx
url = f"https://api.meraki.com/api/v0/organizations/{org_id}/actionBatches"
with open('Subnets.csv', newline="\n") as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
net_id = row[3]
V5NET = row[4]
V5GW = row[5]
V10NET = row[6]
V10GW = row[7]
V11NET = row[8]
V11GW = row[9]
payload = {
"confirmed": True,
"synchronous": True,
"actions": [
{
"resource": f"/networks/{net_id}/vlans",
"operation": "create",
"body": {
"id": "5",
"name": "Data",
"applianceIp": f"{V5GW}",
"subnet": f"{V5NET}/24",
},
},
{
"resource": f"/networks/{net_id}/vlans",
"operation": "create",
"body": {
"id": 10,
"name": "Voice",
"applianceIp": f"{V10GW}",
"subnet": f"{V10NET}/24",
},
},
{
"resource": f"/networks/{net_id}/vlans",
"operation": "create",
"body": {
"id": 11,
"name": "Property",
"applianceIp": f"{V11GW}",
"subnet": f"{V11NET}/24",
},
},
],
}
headers = {
"X-Cisco-Meraki-API-Key": API_KEY,
"Content-Type": "application/json",
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)
基于反馈的编辑版本
# actionBatch-VlanUpdate.py
import requests
import csv
# Environment Variables
API_KEY = "XXXX"
org_id = xxxx
url = f"https://api.meraki.com/api/v0/organizations/{org_id}/actionBatches"
with open('Subnets.csv', newline="\n") as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
net_id = row[3]
V5NET = row[4]
V5GW = row[5]
V10NET = row[6]
V10GW = row[7]
V11NET = row[8]
V11GW = row[9]
payload = {
"confirmed": True,
"synchronous": True,
"actions": [
{
"resource": f"/networks/{net_id}/vlans",
"operation": "create",
"body": {
"id": "5",
"name": "Data",
"applianceIp": f"{V5GW}",
"subnet": f"{V5NET}/24",
},
},
{
"resource": f"/networks/{net_id}/vlans",
"operation": "create",
"body": {
"id": 10,
"name": "Voice",
"applianceIp": f"{V10GW}",
"subnet": f"{V10NET}/24",
},
},
{
"resource": f"/networks/{net_id}/vlans",
"operation": "create",
"body": {
"id": 11,
"name": "Property",
"applianceIp": f"{V11GW}",
"subnet": f"{V11NET}/24",
},
},
],
}
headers = {
"X-Cisco-Meraki-API-Key": API_KEY,
"Content-Type": "application/json",
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)
答案 0 :(得分:0)
在for循环中,您将覆盖先前每次迭代的结果,因此它将始终是文件中的最后一行。您可以使用.append()数组方法将每个循环的结果插入各种数组的末尾。
编辑-看起来您在for循环之前接受了数组的声明。如果您只想将请求调用130次,则只需更改缩进即可,使您的请求调用位于for循环内。
编辑2-您也希望在for循环中获得请求调用。
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
net_id = row[3]
V5NET = row[4]
V5GW = row[5]
V10NET = row[6]
V10GW = row[7]
V11NET = row[8]
V11GW = row[9]
payload = {
"confirmed": True,
"synchronous": True,
"actions": [
...
],
}
headers = {
"X-Cisco-Meraki-API-Key": API_KEY,
"Content-Type": "application/json",
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)