-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathcisco_git.py
More file actions
56 lines (42 loc) · 1.54 KB
/
Copy pathcisco_git.py
File metadata and controls
56 lines (42 loc) · 1.54 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import tempfile
import subprocess
from netmiko import ConnectHandler
# Cisco IOS-XE connection details
device = {
"device_type": "cisco_ios",
"host": "devnetsandboxiosxe.cisco.com",
"username": "admin",
"password": "C1sco12345",
}
# Git repository details
git_repo_url = "https://github.com/aiwithr/ios_git"
commit_message = "Automatic config update"
# ------ Connect to device and get device config ------
# Connect to IOS-XE device
net_connect = ConnectHandler(**device)
# Run show command on device
device_config = net_connect.send_command("show run")
# Disconnect from Device
net_connect.disconnect()
# ------ Clone git repo in temporary directory, replace files with new config file and push changes back to git repo ------
# Create temporary directory
temporary_folder = tempfile.TemporaryDirectory()
# Clone Git Repo
subprocess.call(
f"cd {temporary_folder.name} && git clone {git_repo_url} . && rd /s /q *.*", shell=True
)
# Write all config to file
with open(f"{temporary_folder.name}/{device['host']}_config.txt", "w") as outfile:
outfile.write(device_config)
# git init
# git add -A
# git commit -m '{commit_message}'
# git remote add origin {git_repo_url}
# git push origin main --force
# Git commit all changes
subprocess.call(
f"cd {temporary_folder.name} && git init && git add -A && git commit -m '{commit_message}' git remote add origin {git_repo_url} && git push origin main --force",
shell=True,
)
# Delete temporary directory
temporary_folder.cleanup()