-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_script.py
More file actions
40 lines (31 loc) · 1.13 KB
/
backup_script.py
File metadata and controls
40 lines (31 loc) · 1.13 KB
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
Pythmport csv
import os
from netmiko import ConnectHandler
csv_file = "/home/jonsnow/devices.csv"
backup_folder = "/home/jonsnow/backups"
os.makedirs(backup_folder, exist_ok=True)
username = os.getenv("SSH_USER")
with open(csv_file, "r") as file:
reader = csv.DictReader(file)
for row in reader:
device_name = row["name"]
device_ip = row["ip"]
device = {
"device_type": "cisco_ios",
"host": device_ip,
"username": username,
"use_keys": True,
"key_file": "/home/jonsnow/.ssh/id_rsa",
"passphrase": None,
"disabled_algorithms": {
"pubkeys": ["rsa-sha2-512", "rsa-sha2-256"]
}
}
print(f"Connecting to {device_name} ({device_ip})...")
net_connect = ConnectHandler(**device)
output = net_connect.send_command("show running-config")
filename = os.path.join(backup_folder, f"{device_name}_backup.txt")
with open(filename, "w") as backup_file:
backup_file.write(output)
print(f"Backup saved to {filename}")
net_connect.disconnect()