-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiosbackup.py
114 lines (92 loc) · 3.35 KB
/
iosbackup.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'''
This code is based on netmiko to take back up (show running-config) from cisco ios devices every 30 days
Accepts input (Device names/ip) as text file.
Output will be txt file for every device with time stamp
Netmiko can be found on GitHub at https://github.com/ktbyers/netmiko
'''
import argparse
import getpass
import time
from netmiko import ConnectHandler
class IosBackup:
def __init__(self, username, password, input_file):
'''
class initialization
'''
self.username = username
self.password = password
self.input_file = input_file
self.ip = list()
def reader(self):
'''
Reads from the text file to return a list
'''
with open(self.input_file) as lines:
for line in lines:
self.ip.append(line)
return self.ip
def writer(self,ip,output,date):
'''
Write the show run outputs to a text file
'''
file_name = '{}-{}.txt'.format(ip,date)
with open(
file_name,
'w') as the_file:
for line in output:
the_file.write(line+'\n')
return True
def login(self,device):
'''
Logs in to the ios device; change device_type to 'cisco_nxos' if device is nexus. Refer netmiko git link above
:return:
'''
self.session = ConnectHandler(device_type='cisco_ios',
ip=device,
username=self.username,
password=self.password)
return True
def logout(self):
'''
logs out of device
'''
self.session.disconnect()
return True
def run(self):
'''
Runs show runn command and returns output as list
'''
output = self.session.send_command('show runn')
output = output.split('\n')
return output
def arguments():
parser = argparse.ArgumentParser()
parser.add_argument('-u', "--username", help="Login Username",
action="store")
parser.add_argument('-p', "--password", help="Login password",
action="store")
parser.add_argument('-f', "--in_file", help="Input txt file with device list",
action="store")
args = parser.parse_args()
if not args.username:
args.username = raw_input("Login Username: ")
if not args.password:
args.password = getpass.getpass("Login Password: ")
if not args.in_file:
args.in_file = raw_input("txt file with device names: ")
return (args.username,args.password,args.in_file)
# main
if __name__ == "__main__":
(username, password, in_file) = arguments()
backup = IosBackup(username, password, in_file)
routers = backup.reader()
while True:
calender = time.strftime("%Y-%m-%d") # Year Month Date
for router in routers:
router = router.strip()
backup.login(router)
show_run = backup.run()
backup.logout()
backup.writer(router,show_run,calender)
print 'Recent Back up: {}'.format(calender)
time.sleep(2600000) # Approx a month 60X60X24X30 = 2600000