-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patho365.py
133 lines (112 loc) · 3.87 KB
/
o365.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
#! /usr/bin/python
'''
This script parses Microsoft's o365 IP webpage and updates a specific
object-group in a Cisco ASA with the new IP addreses. It can also
remove IPs, but that functionality is commented out.
'''
import re
import sys
import paramiko
import requests
import os
import datetime
import time
import netaddr
import creds
################################
####### Custom Variables #######
################################
### Set MS url
url = "http://technet.microsoft.com/en-us/library/hh373144.aspx"
### Set device IPs
devices = ["192.168.25.254"]
### Set o365 object-group
object_group = "MS-OFFICE365-SUBNETS"
### Set device username and password - or remove user and pass variables and change the "account" variable to "read_login()" to ask for password at script run - no automation.
username = creds.username
password = creds.password
enable_pw = creds.enable_pw
date = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M")
#### Functions ####
def main():
if len(sys.argv) > 1 and sys.argv[1].lower() == "--fresh":
try:
os.remove('new-ip-list.txt')
os.remove('old-ip-list.txt')
print("\n------ Old files removed, building fresh config ------")
except:
pass
print("\n" + "#" * 95)
print("Microsoft URL: {}".format(url))
print("User Account: {}".format(username))
print("Devices: {}".format(", ".join(devices)))
print("Object Group: {}".format(object_group))
print("#" * 95)
page = requests.get(url)
content = page.content
ip_list = re.findall('(?:[0-9]{1,3}\.){3}[0-9]{1,3}\/[0-3][0-9]|(?:[0-9]{1,3}\.){3}[0-9]{1,3}', content)
if "new-ip-list.txt" in os.listdir("./"):
os.rename("new-ip-list.txt", "old-ip-list.txt")
else:
open("old-ip-list.txt", "w+").close()
with open("new-ip-list.txt", "w+") as out_file:
for ip in ip_list:
out_file.writelines(ip + "\n")
configure(devices)
def enable(conn, enable_pw):
conn.send("enable\r")
time.sleep(1)
conn.send(enable_pw + "\r")
time.sleep(1)
def compare():
commands = ["object-group network " + object_group,]
old = [line.rstrip() for line in open("old-ip-list.txt", "r").readlines()]
new = [line.rstrip() for line in open("new-ip-list.txt", "r").readlines()]
for ip in new:
if ip in old:
pass
if ip not in old:
ip = netaddr.IPNetwork(ip)
commands.append(" network-object {} {}".format(str(ip.ip), str(ip.netmask)))
for ip in old:
if ip not in new and "/" in ip:
ip = netaddr.IPNetwork(ip)
print("------ {} appears to have been removed from MS IP list ------".format(str(ip.cidr)))
with open("removed-ip." + date + ".txt", "a") as out_file:
out_file.writelines(ip + "\n")
# commands.append(" no network-object " + ip)
return commands
def configure(devices):
commands = compare()
if len(commands) > 1:
for device in devices:
try:
output = []
conn_pre = paramiko.SSHClient()
conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn_pre.connect(device, username=username, password=password)
conn = conn_pre.invoke_shell()
time.sleep(1)
if ">" in conn.recv(100000):
enable(conn, enable_pw)
time.sleep(0.5)
print("\n====== Configuring {} ======".format(device))
conn.send("conf t\r")
time.sleep(0.5)
for command in commands:
# print conn.recv(100000)
conn.send(command + "\r")
time.sleep(0.25)
output.append(conn.recv(100000))
with open(device + "_" + date + ".out", "a") as out_file:
out_file.writelines(output)
print("\n====== Configuration applied successfully, output file '{}_{}.out' was created ======\n".format(device, date))
except Exception as e:
print("\n!!!!!! ------ There was an issue with {}, see file: '{}_{}.error.out' ------ !!!!!!\n".format(device, device, date))
out_file = open(device + "_" + date + ".error.out", "w")
out_file.writelines(e)
print("\n")
else:
print("\n!!!!!! ------ No changes necessary ------ !!!!!!\n")
if __name__ == "__main__":
main()