-
Notifications
You must be signed in to change notification settings - Fork 0
/
Metro_Broadband_Device_v2.py
211 lines (205 loc) · 9.29 KB
/
Metro_Broadband_Device_v2.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
211
import copy
import csv
import json
# from rancidcmd import RancidCmd
import os
import sys
import time
from pprint import pprint
import requests
from requests.packages import urllib3
import re
import datetime
import database_ipv6
import API_device
import filter_zone
def device_not_added():
NAT = '0'
ipv6 = '0'
ccu = '0'
ipv4 = '0'
device_area = { "FTN": ["HNI", "BGG", "BNH", "CBG", "LSN", "LCI", "PTO", "TNN", "TQG", "VPC", "YBI", "HBH", "SLA", "DBN", "QNH", "HNM", "NDH", "NAN", "NBH", "TBH", "THA", "HTH", "HDG", "HPG", "HYN"],
"FTS": ["BDH", "DNG", "DLK", "GLI", "HUE", "NTG", "KTM", "PYN", "QTI", "QNM", "QBH", "QNI", "KHA", "HCM", "BDG", "BTN", "DNI", "LDG", "NTN", "TNH", "VTU", "BPC", "CTO", "AGG", "BLU", "BTE", "CMU", "DTP", "HGG", "KGG", "TGG", "LAN", "VLG", "STG", "TVH"]
}
device_zone = { "Zone_1":"HNI",
"Zone_2":["BGG","BNH","CBG","LSN","LCI","PTO","TNN","TQG","VPC","YBI","HBH","SLA","DBN","QNH"],
"Zone_3":["HNM","NDH","NAN","NBH","TBH","THA","HTH","HDG","HPG","HYN"],
"Zone_4":["BDH","DNG","DLK","GLI","HUE","NTG","KTM","PYN","QTI","QBH","QNI","KHA","QNM"],
"Zone_5":"HCM",
"Zone_6":["BDG","BTN","DNI","LDG","NTN","TNH","VTU","BPC","BTN"],
"Zone_7":["CTO","AGG","BLU","BTE","CMU","DTP","HGG","KGG","TGG","LAN","VLG","STG","TVH"]
}
filter_zone = {"Zone_1_":"1","Zone_2_":"2","Zone_3_":"3","Zone_4_":"4","Zone_5_":"5","Zone_6_":"6","Zone_7_":"7"}
_row_1 = '+ {0:-^20}+{1:-^20}+{2:-^6}+{3:-^6}+{4:-^6}+{5:-^6}+{6:-^15}+{7:-^30}+{8:-^6}+{9:-^6}+{10:-^20}+\n'.format('','', '','','','','','','','','')
_row_2 = '|{0:^20} |{1:^20}|{2:^6}|{3:^6}|{4:^6}|{5:^6}|{6:^15}|{7:^30}|{8:^6}|{9:^6}|{10:^20}|\n'.format('hostname', 'ip_device', 'NAT','IPV6','Area','Zone','Province','Log time','CCU','IPV4','NOTE')
_row_3 = '+ {0:-^20}+{1:-^20}+{2:-^6}+{3:-^6}+{4:-^6}+{5:-^6}+{6:-^15}+{7:-^30}+{8:-^6}+{9:-^6}+{10:-^20}+\n'.format('','', '','','','','','','','','')
list_ip = []
data = API.getdataAPI()
ip_databse = database.getipdevice()
for ip in ip_databse:
list_ip.append(ip)
reg_ip = re.findall(r"\d+\.\d+\.\d+\.\d+",str(list_ip))
reg_name = r"[A-Z]+\-[A-Z]+\-\d+"
with open ("/home/dev/Manhvc/device_database/list_review.txt","w+") as review_file:
review_file.writelines(_row_1)
review_file.writelines(_row_2)
review_file.writelines(_row_3)
for i in data["data"]:
list_device = []
function = []
name_not_added = ""
ip_not_added = ""
zone_not_added = ""
area_not_added = ""
province_not_added = ""
nat_not_added = ""
ipv6_not_added = ""
ccu_not_added = ""
ipv4_not_added = ""
note = ""
function.append(i["function"])
if function[0] =="Metro BroadBand BRAS":
if i["ip"] not in reg_ip:
ip_not_added += i["ip"]
update_time = str(datetime.datetime.now())
name_not_added += i["name"]
nat_not_added += NAT
ipv6_not_added += ipv6
if name_not_added[0:3] in device_area['FTS']:
area_not_added = area_not_added + 'FTS'
elif name_not_added[0:3] in device_area['FTN']:
area_not_added = area_not_added + 'FTN'
if name_not_added[0:3] in device_zone['Zone_1']:
zone_not_added= zone_not_added + filter_zone['Zone_1_']
elif name_not_added[0:3] in device_zone['Zone_2']:
zone_not_added= zone_not_added + filter_zone['Zone_2_']
elif name_not_added[0:3] in device_zone['Zone_3']:
zone_not_added= zone_not_added + filter_zone['Zone_3_']
elif name_not_added[0:3] in device_zone['Zone_4']:
zone_not_added= zone_not_added + filter_zone['Zone_4_']
elif name_not_added[0:3] in device_zone['Zone_5']:
zone_not_added= zone_not_added + filter_zone['Zone_5_']
elif name_not_added[0:3] in device_zone['Zone_6']:
zone_not_added= zone_not_added +filter_zone['Zone_6_']
elif name_not_added[0:3] in device_zone['Zone_7']:
zone_not_added= zone_not_added +filter_zone['Zone_7_']
if (len(i["name"]) > 12):
province = re.findall(reg_name,str(i["name"]),re.MULTILINE)
province_not_added += province[0]
else:
province_not_added += i["name"][0:-3]
ccu_not_added += ccu
ipv4_not_added += ipv4
note += 'not added'
_row_4 = '|{0:^20} |{1:^20}|{2:^6}|{3:^6}|{4:^6}|{5:^6}|{6:^15}|{7:^30}|{8:^6}|{9:^6}|{10:^20}|\n'.format(name_not_added, ip_not_added, nat_not_added,ipv6_not_added,area_not_added,zone_not_added,province_not_added,update_time,ccu_not_added,ipv4_not_added,note)
review_file.writelines(_row_4)
def device_recalled():
# _device_area = { "FTN": ["HNI", "BGG", "BNH", "CBG", "LSN", "LCI", "PTO", "TNN", "TQG", "VPC", "YBI", "HBH", "SLA", "DBN", "QNH", "HNM", "NDH", "NAN", "NBH", "TBH", "THA", "HTH", "HDG", "HPG", "HYN"],
# "FTS": ["BDH", "DNG", "DLK", "GLI", "HUE", "NTG", "KTM", "PYN", "QTI", "QNM", "QBH", "QNI", "KHA", "HCM", "BDG", "BTN", "DNI", "LDG", "NTN", "TNH", "VTU", "BPC", "CTO", "AGG", "BLU", "BTE", "CMU", "DTP", "HGG", "KGG", "TGG", "LAN", "VLG", "STG", "TVH"]
# }
# _device_zone = { "Zone_1":"HNI",
# "Zone_2":["BGG","BNH","CBG","LSN","LCI","PTO","TNN","TQG","VPC","YBI","HBH","SLA","DBN","QNH"],
# "Zone_3":["HNM","NDH","NAN","NBH","TBH","THA","HTH","HDG","HPG","HYN"],
# "Zone_4":["BDH","DNG","DLK","GLI","HUE","NTG","KTM","PYN","QTI","QBH","QNI","KHA","QNM"],
# "Zone_5":"HCM",
# "Zone_6":["BDG","BTN","DNI","LDG","NTN","TNH","VTU","BPC","BTN"],
# "Zone_7":["CTO","AGG","BLU","BTE","CMU","DTP","HGG","KGG","TGG","LAN","VLG","STG","TVH"]
# }
# _filter_zone = {"Zone_1_":"1","Zone_2_":"2","Zone_3_":"3","Zone_4_":"4","Zone_5_":"5","Zone_6_":"6","Zone_7_":"7"}
list_ip = []
data = API.getdataAPI()
data_databse = database.getdata()
for i in data["data"]:
function = []
function.append(i["function"])
if function[0] == "Metro BroadBand BRAS":
list_ip.append(i["ip"])
with open ("/home/dev/Manhvc/device_database/list_review.txt","a+") as file:
try:
for data in data_databse:
name_recalled = ""
ip_recalled = ""
zone_recalled = ""
area_recalled = ""
province_recalled = ""
nat_recalled = ""
ipv6_recalled = ""
ccu_recalled = ""
ipv4_recalled = ""
_note = ""
#regex = r"\(u\'([A-Z]{3}\-[A-Z]+-\d+-\d+)\'.\s+u\'(\d+.\d+.\d+.\d+)\'.\s+(\d+)\,\s+(\d+)\,\s+u\'([A-Z]+)\'\,\s+(\d+)\,\s+u\'([A-Z]+-[A-Z]+\-\d+)\'\,\s+(.*\)\,)\s+(\d+)\,\s+(\d+)\)"
regex = r"\(u\'([A-Z]+\-[A-z]+\-(\d+|[A-Za-z]+)\-\d+(.*))\'\,\s+u\'(\d+.\d+.\d+.\d+)\'\,\s+(\d+)\,\s+(\d+)\,\s+u\'([A-Z]+)\'\,\s+(\d+)\,\s+u\'([A-Z]+\-[A-Z]+\-(\d+|[A-Za-z]+))\'\,\s+(.*\)\,)\s+(\d+)\,\s+(\d+)\)"
#regex = r"\(u.*\)"
matches = re.findall(regex,str(data),re.MULTILINE)
if matches[0][3] not in list_ip:
ip_recalled += matches[0][3]
update_time = str(datetime.datetime.now())
name_recalled += matches[0][0]
nat_recalled += matches[0][4]
ipv6_recalled += matches[0][5]
area_recalled += matches[0][6]
zone_recalled += matches[0][7]
province_recalled += matches[0][8]
ccu_recalled += matches[0][11]
ipv4_recalled += matches[0][12]
_note += 'recalled'
_row_5 = '|{0:^20} |{1:^20}|{2:^6}|{3:^6}|{4:^6}|{5:^6}|{6:^15}|{7:^30}|{8:^6}|{9:^6}|{10:^20}|\n'.format(name_recalled, ip_recalled, nat_recalled,ipv6_recalled,area_recalled,zone_recalled,province_recalled,update_time,ccu_recalled,ipv4_recalled,_note)
file.writelines(_row_5)
except Exception:
print "No device has been recalled"
def wrong_name():
ip_API = []
compare_API = []
data = API.getdataAPI()
data_databse = database.getdata()
for i in data["data"]:
function = []
function.append(i["function"])
if function[0] == "Metro BroadBand BRAS":
list_API = []
ip_API.append(i["ip"])
list_API.append(i["ip"])
list_API.append(i["name"])
compare_API.append(list_API)
with open ("/home/dev/Manhvc/device_database/list_review.txt","a+") as file:
try:
for data in data_databse:
name_wrong = ""
ip_wrong = ""
zone_wrong = ""
area_wrong = ""
province_wrong = ""
nat_wrong = ""
ipv6_wrong = ""
ccu_wrong = ""
ipv4_wrong = ""
compare_db = []
reg = r"\(u\'([A-Z]+\-[A-z]+\-(\d+|[A-Za-z]+)\-\d+(.*))\'\,\s+u\'(\d+.\d+.\d+.\d+)\'\,\s+(\d+)\,\s+(\d+)\,\s+u\'([A-Z]+)\'\,\s+(\d+)\,\s+u\'([A-Z]+\-[A-Z]+\-(\d+|[A-Za-z]+))\'\,\s+(.*\)\,)\s+(\d+)\,\s+(\d+)\)"
match = re.findall(reg,str(data),re.MULTILINE)
if match[0][3] in ip_API:
compare_db.append(match[0][3])
compare_db.append(match[0][0])
if compare_db not in compare_API:
ip_wrong += match[0][3]
update_time = str(datetime.datetime.now())
name_wrong += match[0][0]
nat_wrong += match[0][4]
ipv6_wrong += match[0][5]
area_wrong += match[0][6]
zone_wrong += match[0][7]
province_wrong += match[0][8]
ccu_wrong += match[0][11]
ipv4_wrong += match[0][12]
note_ = "wrong_name"
_row_6 = '|{0:^20} |{1:^20}|{2:^6}|{3:^6}|{4:^6}|{5:^6}|{6:^15}|{7:^30}|{8:^6}|{9:^6}|{10:^20}|\n'.format(name_wrong, ip_wrong, nat_wrong,ipv6_wrong,area_wrong,zone_wrong,province_wrong,update_time,ccu_wrong,ipv4_wrong,note_)
_row_7 = '+ {0:-^20}+{1:-^20}+{2:-^6}+{3:-^6}+{4:-^6}+{5:-^6}+{6:-^15}+{7:-^30}+{8:-^6}+{9:-^6}+{10:-^20}+\n'.format('','', '','','','','','','','','')
file.writelines(_row_6)
except Exception:
print "No device is wrong name"
file.writelines(_row_7)
if __name__ == '__main__':
API = API_device.API_device()
database = database_ipv6.database_ipv6()
device_not_added()
device_recalled()
wrong_name()