-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull_multiple_cisco.py
72 lines (57 loc) · 2.15 KB
/
pull_multiple_cisco.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
#!/usr/bin/python3
# Automated pulling of all cisco configs
# Created Feb 2019
# Tim Bowers, PCMS Group
## Connect to Primary firewall and get copy of the config
## Connect to Secondary firewall and get copy of the config
## Return state of copies, possibly return line highlighting for changes
import getpass
import time
import pexpect
import datetime
from time import sleep
import sys
import subprocess
import difflib
## We can't use pxssh because it tries to change the prompt which doesn't work
now = datetime.datetime.now()
date = now.strftime("%Y-%b-%d")
## For each ip, hostname pair - pull the configs
print ("Enter ssh password")
password = getpass.getpass()
print ("Enter enable password")
enable_password = getpass.getpass()
print ("Enter destination IP")
f = open("sites.txt", "r")
for line in f:
host1 = line.split(",")[0]
hostname = line.split(",")[1]
hostname1 = hostname.rstrip()
print ('Running config pull on %s named %s copying to %s' % (destination,host1,hostname1))
child1 = pexpect.spawn('/usr/bin/ssh networks@%s' % (host1))
try:
child1.expect('assword:')
child1.sendline(password)
child1.expect('>')
print ("Logged in successfully")
child1.sendline('en')
child1.expect('assword:')
child1.sendline(enable_password)
child1.expect('#')
child1.sendline("copy start tftp://%s/%s-%s.txt" %(destination,hostname1,date))
child1.expect(']?')
child1.sendline()
child1.expect(']?')
child1.sendline()
sleep(1)
child1.expect('#')
child1.sendline('exit')
print ("Copy finished, terminating in 5 seconds")
sleep(5)
child1.sendline('exit')
child1.close()
except pexpect.TIMEOUT :
print('Timed out connecting to %s - %s' % (host1,hostname1))
except pexpect.EOF :
print('EOF Recieved, connecting to %s - %s' % (host1,hostname1))
print ("Finished copying files")