-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetmiko_connect_and_run.py
210 lines (200 loc) · 6.83 KB
/
netmiko_connect_and_run.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# from netmiko import Netmiko
# connection = Netmiko(host='192.168.56.2',port='22', username='ubuntunode01',password='123',device_type='linux')
from netmiko import ConnectHandler
# creating a dictionary for the device to connect to
# cisco_device = {
# 'device_type': 'linux', #device type from https://github.com/ktbyers/netmiko/blob/master/netmiko/ssh_dispatcher.py
# 'host': '192.168.56.2',
# 'username': 'ubuntunode01',
# 'password': '123',
# 'port': 22,
# 'secret': '123', # optional, default 22
# 'verbose': True # optional, default False
# }
# connection=ConnectHandler(**cisco_device)
# prompt=connection.find_prompt()
# print(prompt)
# if '$' in prompt:
# connection.enable()
# prompt=connection.find_prompt()
# print(prompt)
# print(connection.check_config_mode())
# if not connection.check_config_mode():
# connection.check_config_mode()
# print(connection.check_config_mode())
# connection.send_command('username u4 secret cisco')
# output=connection.send_command('cat /etc/shadow')
# print(output)
# print('Closing connection')
# connection.disconnect()
# from netmiko import ConnectHandler
# cisco_device = {
# 'device_type': 'cisco_ios',
# 'host': '192.168.122.10',
# 'username': 'u1',
# 'password': 'cisco',
# 'port': 22, # optional, default 22
# 'secret': 'cisco', # this is the enable password
# 'verbose': True # optional, default False
# }
# connection = ConnectHandler(**cisco_device)
# print('Entering the enable mode...')
# connection.enable()
#
# # this method receives a list of commands to send to the device
# # in enters automatically into global config mode and exists automatically at the end
# # commands = ['int loopback 0', 'ip address 1.1.1.1 255.255.255.255', 'exit', 'username netmiko secret cisco']
# cmd = 'ip ssh version 2;access-list 1 permit any;ip domain-name network-automation.io'
# # cmd= '''ip ssh version 2
# # access-list 1 permit any
# # ip domain-name network-automation.io
# # '''
# # output = connection.send_config_set(cmd.split('\n'))
# output = connection.send_config_set(cmd.split(';'))
# print(connection.find_prompt())
# connection.send_command('write memory')
# print(output)
# print('CLosing Connection')
# connection.disconnect()
# cisco_device = {
# 'device_type': 'cisco_ios',
# 'host': '192.168.122.10',
# 'username': 'u1',
# 'password': 'cisco',
# 'port': 22, # optional, default 22
# 'secret': 'cisco', # this is the enable password
# 'verbose': True # optional, default False
# }
# connection = ConnectHandler(**cisco_device)
# print('Entering the enable mode...')
# connection.enable()
#
# print('Sendin command command from file')
# output=connection.send_config_from_file('ospf.txt')
# print(output)
# print('CLosing Connection')
# connection.disconnect()
# with open('devices.txt') as f:
# devices=f.read().splitlines()
#
# device_list=list()
# for ip in devices:
# cisco_device = {
# 'device_type': 'cisco_ios',
# 'host': ip,
# 'username': 'u1',
# 'password': 'cisco',
# 'port': 22, # optional, default 22
# 'secret': 'cisco', # this is the enable password
# 'verbose': True # optional, default False
# }
# device_list.append(cisco_device)
# # print(device_list)
#
# print(device_list)
# # exit(1)
# for device in device_list:
# connection = ConnectHandler(**device)
#
# print('Entering the enable mode ...')
# connection.enable()
#
# # prompting the user for a config file
# file = input(f'Enter a configuration file (use a valid path) for {device["host"]}:')
#
# print(f'Running commands from file: {file} on device: {device["host"]}')
# output = connection.send_config_from_file(file)
# print(output)
#
# print(f'Closing connection to {cisco_device["host"]}')
# connection.disconnect()
#
# print('#' * 30)
# import time
# start= time.time()
# with open('devices.txt') as f:
# devices=f.read().splitlines()
#
# for ip in devices:
# cisco_device = {
# 'device_type': 'cisco_ios',
# 'host': ip,
# 'username': 'u1',
# 'password': 'cisco',
# 'port': 22, # optional, default 22
# 'secret': 'cisco', # this is the enable password
# 'verbose': True # optional, default False
# }
# connection = ConnectHandler(**cisco_device)
# print('Entering the enable mode...')
# connection.enable()
# output = connection.send_command('show run')
# # print(output)
# prompt = connection.find_prompt()
# # print(prompt)
# hostname = prompt[0:-1]
# # print(hostname)
# from datetime import datetime
#
# now = datetime.now()
# year = now.year
# month = now.month
# day = now.day
#
#
# filename = f'{hostname}_{year}-{month}-{day}-backup.txt'
# with open(filename, 'w') as backup:
# backup.write(output)
# print(f'Backup of {hostname} is succesfull')
# print('*'*30)
# print('CLosing Connection')
# connection.disconnect()
#
# end=time.time()
# print(f'Total time {end-start}')
import time
import threading
start = time.time()
def backup(device):
connection = ConnectHandler(**device)
print('Entering the enable mode...')
connection.enable()
output = connection.send_command('show run')
# print(output)
prompt = connection.find_prompt()
# print(prompt)
hostname = prompt[0:-1]
# print(hostname)
from datetime import datetime
now = datetime.now()
year = now.year
month = now.month
day = now.day
filename = f'{hostname}_{year}-{month}-{day}-backup.txt'
with open(filename, 'w') as backup:
backup.write(output)
print(f'Backup of {hostname} is succesfull')
print('*' * 30)
print('CLosing Connection')
connection.disconnect()
with open('devices.txt') as f:
devices=f.read().splitlines()
threads = list()
for ip in devices:
device = {
'device_type': 'cisco_ios',
'host': ip,
'username': 'u1',
'password': 'cisco',
'port': 22, # optional, default 22
'secret': 'cisco', # this is the enable password
'verbose': True # optional, default False
}
th = threading.Thread(target=backup, args=(device,))
threads.append(th)
for th in threads:
th.start()
for th in threads:
th.join()
end=time.time()
print(f'Total time {end-start}')