-
Notifications
You must be signed in to change notification settings - Fork 0
/
HP_Backup.py
80 lines (55 loc) · 2.19 KB
/
HP_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
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
import json
import netmiko
import paramiko
import datetime
from getpass import getpass
# cd /volumes/netscripts/Backup
#to get current time stamp
def get_input(prompt=''):
try:
line = raw_input(prompt)
except NameError:
line = input(prompt)
return line
def get_credentials():
"""Prompt for and return a username and password."""
username = get_input('Enter Username: ')
password = None
while not password:
password = getpass()
password_verify = getpass('Retype your password: ')
if password != password_verify:
print('Passwords do not match. Try again.')
password = None
return username, password
#username, password = get_credentials()
device_type = 'hp_comware'
now = datetime.datetime.now()
today = str(now.year)+ '-' + str(now.month)+ '-' + str(now.day)
with open('DevicesBackup.txt') as f: #opening devices from device IP list
devices = f.read().splitlines()
for device in devices:
try:
#connection = netmiko.ConnectHandler(**device)
connection = netmiko.ConnectHandler(ip=device, device_type=device_type,username='', password='', global_delay_factor= 0)
#getting prompt for hostname
prompt= connection.find_prompt()
print(prompt)
#hostname = prompt[:-1]
file = 'file location'
with open(file,'w') as backup:
#connection.sendCommand('screen-length disable')
q = connection.sendCommand_expect('display current-configuration')
connection.send_command('copy running-config startup-config',expect_string= r"#")
print('Saving configuration')
q = connection.sendCommand_expect('display current-configuration')
backup.write(connection.send_command('save force', delay_factor=2))
backup.write('\n\n'+ q +'\n\n')
print('backup of '+ hostname + ' completed successfully')
#closing the connection
connection.disconnect()
except (netmiko.ssh_exception.NetMikoTimeoutException,
netmiko.ssh_exception.NetMikoAuthenticationException,
paramiko.ssh_exception.SSHException,paramiko.ssh_exception.SSHException, OSError, Exception) as e:
failed = 'Failed ', device, e, '\n'
backup.write(device + '\n')