【发布时间】:2019-11-08 14:57:38
【问题描述】:
像大多数人一样,我正在尝试学习 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)
【问题讨论】:
标签: python python-3.x csv for-loop meraki-api