-
Notifications
You must be signed in to change notification settings - Fork 2
/
sw_cfg_backup.py
41 lines (33 loc) · 963 Bytes
/
sw_cfg_backup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/home/py3/bin/python
# -*- coding: utf-8 -*-
import asyncio
import yaml
import sys
from NetDevices import DeviceHandler
import time
import os
FILEPATH = "/home/NetBackup/switches_cfg/" + str(time.strftime("%Y-%m-%d", time.localtime()))
if not os.path.exists(FILEPATH):
os.makedirs(FILEPATH)
async def get_config(device):
hostname = device.get("hostname")
conn = DeviceHandler(device)
conn.connect()
await conn.login()
r = await conn.get_config()
file_name = FILEPATH + r"/" + hostname
open(file_name, "w").write(r[1])
print("%s is saved" % file_name)
deviceinfos = {}
try:
yaml_cfg = sys.argv[1]
except IndexError:
print("please give yaml configure file")
sys.exit(1)
f = open(yaml_cfg)
deviceinfos = yaml.load(f.read())
loop = asyncio.get_event_loop()
tasks = []
for device in deviceinfos.get("devices"):
tasks.append(loop.create_task(get_config(device)))
loop.run_until_complete(asyncio.wait(tasks))