-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnetboxinfo.py
More file actions
400 lines (376 loc) · 15.7 KB
/
Copy pathnetboxinfo.py
File metadata and controls
400 lines (376 loc) · 15.7 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import pynetbox
import telebot
import re
import config
netbox = pynetbox.api(url=config.URL_NB, token=config.TOKEN_NB)
bot = telebot.TeleBot(config.TOKEN_TELE)
regex_ip = re.compile(config.REGEX_IP)
def search_prefix(ipaddr,prefix):
"""Lấy các thông tin của prefix
:param prefix: Là địa chỉ prefix truyền vào để lấy thông tin của prefix đó
:response: Trả về result là tổng hợp thông tin của prefix như:
Site, Status, Tenant, vlan, Description
"""
try:
single_prefix = netbox.ipam.prefixes.get(prefix='{}' .format(ipaddr), mask_length='{}' .format(prefix))
ip_prefix = ''
status_prefix = ''
descr_prefix = ''
if single_prefix.prefix:
ip_prefix = single_prefix.prefix
if single_prefix.status:
status_prefix = single_prefix.status
try:
site_prefix = single_prefix.site.name
except:
site_prefix ='null'
try:
tenant_prefix = single_prefix.tenant.name
except:
tenant_prefix = 'null'
try:
vlan_prefix = single_prefix.vlan.name
except:
vlan_prefix ='null'
if single_prefix.description:
descr_prefix = single_prefix.description
result = "Prefix" + " : " + '`'+ ip_prefix +'`'+ "\n"
result = result + "Site" + " : " + '`'+ site_prefix + '`' + "\n"
result = result + "Status" + " : " + '`'+ str(status_prefix) +'`'+ "\n"
result = result + "Tenant" + " : " + '`'+ tenant_prefix +'`'+ "\n"
result = result + "vlan" + " : " + '`' + vlan_prefix + '`' + "\n"
result = result + "Description" + " : " + '`'+ descr_prefix +'`'+ "\n"
except Exception as ex:
result = str(ex)
return result
def list_prefix():
"""Lấy tất cả các prefix
:param : Không cần truyền vào các thông số,
chỉ cần gọi hàm sẽ trả về kết quả.
:response: Trả về prefix_all bao gồm tổng số prefix và
liệt kê tất cả các prefix đó
"""
try:
prefix = netbox.ipam.prefixes.all()
prefix_count = netbox.ipam.prefixes.count()
prefix_all = 'Tổng số Prefix : '
prefix_all = prefix_all + str(prefix_count) + '\n\n' + str(prefix)
except Exception as ex:
prefix_all = str(ex)
return prefix_all
def search_ip_address(ipaddr, prefix):
"""Lấy thông tin của địa chỉ IP
:param : Truyền vào địa chỉ IP kèm netmask
:response: Trả về info_ipaddr là tổng hợp thông tin của ip address bao gồm:
Interface, Description, Tenant, Created, Last update
"""
try:
single_ipaddr = netbox.ipam.ip_addresses.get(address='{}' .format(ipaddr), mask_length='{}' .format(prefix))
interface_ip = ''
descr_ip = ''
create_ip = ''
last_update_ip = ''
tenant_ip = ''
try:
site_ip = single_ipaddr.interface.device.site
except:
site_ip = single_ipaddr.interface.device.site
try:
parent_ip = single_ipaddr.interface.device
except:
parent_ip = 'null'
if single_ipaddr.interface:
interface_ip = single_ipaddr.interface
if single_ipaddr.description:
descr_ip = single_ipaddr.description
if single_ipaddr.created:
create_ip = single_ipaddr.created
if single_ipaddr.last_updated:
last_update_ip = single_ipaddr.last_updated
if single_ipaddr.tenant:
tenant_ip = single_ipaddr.tenant
info_ipaddr = "Device" + " : "
info_ipaddr = info_ipaddr + '`' + str(parent_ip) + '`' + "\n"
info_ipaddr = info_ipaddr + "Interface" + " : "
info_ipaddr = info_ipaddr + '`' + str(interface_ip) + '`' + "\n"
info_ipaddr = info_ipaddr + "Description" + " : "
info_ipaddr = info_ipaddr + '`' + str(descr_ip) + '`' + "\n"
info_ipaddr = info_ipaddr + "Tenant" + " : "
info_ipaddr = info_ipaddr + '`' + str(tenant_ip) + '`' + "\n"
info_ipaddr = info_ipaddr + "Site" + " : "
info_ipaddr = info_ipaddr + '`' + str(site_ip) + '`' + "\n"
info_ipaddr = info_ipaddr + "Created" + " : "
info_ipaddr = info_ipaddr + '`' + str(create_ip) + '`' + "\n"
info_ipaddr = info_ipaddr + "Last updated" + " : "
info_ipaddr = info_ipaddr + '`' + str(last_update_ip) + '`' + "\n"
except Exception as ex:
info_ipaddr = str(ex)
return info_ipaddr
def list_ip_address():
"""Lấy tất cả các IP hiện có
:param : Không cần truyền vào các thông số,
chỉ cần gọi hàm sẽ trả về kết quả.
:response: Trả về ipaddr_all bao gồm tổng số prefix và
liệt kê tất cả các prefix đó
"""
try:
ipaddr = netbox.ipam.ip_addresses.all()
ipaddr_count = netbox.ipam.ip_addresses.count()
ipaddr_all = 'Tổng số IP addresses : '
ipaddr_all = ipaddr_all + str(ipaddr_count) + '\n\n' + str(ipaddr)
except Exception as ex:
ipaddr_all = str(ex)
return ipaddr_all
def search_device(device):
"""Lấy thông tin của device
:param : Truyền vào tên của thiết bị
:response: Trả về info_device là tổng hợp các thông tin của device như :
Tenant, Role, Site, Type, Rack, Ipaddress,
U, Platform, Serial, Assetag
"""
try:
single_device = netbox.dcim.devices.get(name='{}' .format(device))
name_device = ''
tenant_device = ''
role_device = ''
type_device = ''
site_device = ''
rack_device = ''
ipddr_device = ''
asetag_device = ''
serial_device = ''
u_device = ''
platform_device = ''
create_device = ''
update_device = ''
if single_device.name:
name_device = single_device.name
if single_device.tenant:
tenant_device = single_device.tenant
if single_device.device_role:
role_device = single_device.device_role
if single_device.device_type:
type_device = single_device.device_type
if single_device.site:
site_device = single_device.site
if single_device.rack:
rack_device = single_device.rack
if single_device.primary_ip:
ipddr_device = single_device.primary_ip
if single_device.asset_tag:
asetag_device = single_device.asset_tag
if single_device.serial:
serial_device = single_device.serial
if single_device.position:
u_device = single_device.position
if single_device.platform:
platform_device = single_device.platform
if single_device.created:
create_device = single_device.created
if single_device.last_updated:
update_device = single_device.last_updated
info_device = "Name" + " : "
info_device = info_device + '`' + str(name_device) + '`' + "\n"
info_device = info_device + "Tenant" + " : "
info_device = info_device + '`' + str(tenant_device) + '`' + "\n"
info_device = info_device + "Role" + " : "
info_device = info_device + '`' + str(role_device) + '`' + "\n"
info_device = info_device + "Type" + " : "
info_device = info_device + '`' + str(type_device) + '`' + "\n"
info_device = info_device + "Site" + " : "
info_device = info_device + '`' + str(site_device) + '`' + "\n"
info_device = info_device + "Rack" + " : "
info_device = info_device + '`' + str(rack_device) + '`' + "\n"
info_device = info_device + "IPaddress" + " : "
info_device = info_device + '`' + str(ipddr_device) + '`' + "\n"
info_device = info_device + "U" + " : "
info_device = info_device + '`' + str(u_device) + '`' + "\n"
info_device = info_device + "Platform" + " : "
info_device = info_device + '`' + str(platform_device) + '`' + "\n"
info_device = info_device + "Serial" + " : "
info_device = info_device + '`' + str(serial_device) + '`' + "\n"
info_device = info_device + "Assetag" + " : "
info_device = info_device + '`' + str(asetag_device) + '`' + "\n"
info_device = info_device + "Created" + " : "
info_device = info_device + '`' + str(create_device) + '`' + "\n"
info_device = info_device + "Last Update" + " : "
info_device = info_device + '`' + str(update_device) + '`' + "\n"
except Exception as ex:
info_device = str(ex)
return info_device
def device_comment(device):
"""Lấy thông tin comment của device
:param device: Truyền vào tên của thiết bị
:response: Trả về kết quả là comment của thiết bị
"""
try:
single_device = netbox.dcim.devices.get(name='{}' .format(device))
comment = "no comment"
if single_device.comments:
comment = single_device.comments
device_comment = "Comments" + " : " + "\n" + str(comment)
except Exception as ex:
device_comment = str(ex)
return device_comment
def list_device():
"""Lấy tất cả các device
:param : Không cần truyền vào các thông số,
chỉ cần gọi hàm sẽ trả về kết quả.
:response: Trả về device_all bao gồm tổng số prefix và
liệt kê tất cả các prefix đó
"""
try:
devices = netbox.dcim.devices.all()
device_count = netbox.dcim.devices.count()
device_all = 'Tổng số device : '
device_all = device_all + str(device_count) + '\n\n' + str(devices)
except Exception as ex:
device_all = str(ex)
return device_all
@bot.message_handler(commands=["start"])
def send_devices(message):
"""
Tạo lệnh start để hướng dẫn sử dụng
"""
bot.reply_to(message,
'Nhập vào /allprefix để xem' +
'tất cả các prefix' + '\n\n' +
'Nhập /prefix <ip/netmask> để xem' +
'thông tin của từng prefix. VD: /prefix 10.10.10.0/24' + '\n\n'+
'Nhập vào /allipaddr để xem tất cả các IP address' + '\n\n' +
'Nhập /ipaddr <ip/netmask> để xem' +
'thông tin của từng địa chỉ IP. VD: /ipaddr 10.10.10.10/24'+'\n\n'+
'Nhập vào /alldevice để xem tất cả các device' + '\n\n' +
'Nhập /device <tên thiết bị> để xem' +
'các thông tin của từng device. VD: /device SERVER-IDS')
def main():
@bot.message_handler(commands=["prefix"])
def send_prefix(message):
"""
Tạo lệnh để truyền vào prefix từ telegram
"""
IP = message.text[8:]
same_ip = regex_ip.match(IP)
if same_ip :
prefix = IP[-2:]
result = search_prefix(IP,prefix)
try:
bot.send_message(config.CHAT_ID,
str(result), parse_mode='Markdown')
except:
bot.send_message(config.CHAT_ID,
str(result))
else:
bot.send_message(config.CHAT_ID,
'Nhập sai rồi !!',parse_mode='Markdown')
@bot.message_handler(commands=["ipaddr"])
def send_ip(message):
"""
Tạo lệnh để truyền vào ip address từ telegram
"""
IP = message.text[8:]
same_ip = regex_ip.match(IP)
if same_ip :
prefix = IP[-2:]
result = search_ip_address(IP, prefix)
try:
bot.send_message(config.CHAT_ID,
str(result), parse_mode='Markdown')
except:
bot.send_message(config.CHAT_ID,
str(result))
else:
bot.send_message(config.CHAT_ID,
'Nhập sai rồi !!', parse_mode='Markdown')
@bot.message_handler(commands=["device"])
def send_device(message):
"""
Tạo lệnh để truyền vào device từ telegram
"""
DEVICE = message.text[8:]
info_device = search_device(DEVICE)
comment = device_comment(DEVICE)
try:
bot.send_message(config.CHAT_ID,
str(info_device), parse_mode='Markdown')
except:
bot.send_message(config.CHAT_ID,str(info_device))
try:
bot.send_message(config.CHAT_ID,
str(comment), parse_mode='Markdown')
except:
bot.send_message(config.CHAT_ID,str(comment))
@bot.message_handler(commands=["alldevice"])
def send_device_all(message):
"""
Tạo lệnh nhập /alldevice từ tele để lấy tất cả các device
Nếu số ký tự gửi về telegram lớn hơn 4096,
sẽ chia ra gửi thành nhiều message
"""
device_all = list_device()
if len(device_all) > 4096:
for x in range(0, len(device_all), 4096):
try:
bot.send_message(config.CHAT_ID,
device_all[x:x+4096],
parse_mode='Markdown')
except:
bot.send_message(config.CHAT_ID,
device_all[x:x+4096])
else:
try:
bot.send_message(config.CHAT_ID,
device_all, parse_mode='Markdown')
except:
bot.send_message(config.CHAT_ID,
device_all)
@bot.message_handler(commands=["allprefix"])
def send_prefix_all(message):
"""
Tạo lệnh nhập /allprefix từ tele để lấy tất cả các prefix
Nếu số ký tự gửi về tele lớn hơn 4096,
sẽ chia ra gửi thành nhiều message
"""
prefix_list = list_prefix()
prefix_all = str(prefix_list)
if len(prefix_all) > 4096:
for x in range(0, len(prefix_all), 4096):
try:
bot.send_message(config.CHAT_ID,
prefix_all[x:x+4096],
parse_mode='Markdown')
except:
bot.send_message(config.CHAT_ID,
prefix_all[x:x+4096])
else:
try:
bot.send_message(config.CHAT_ID,
prefix_all, parse_mode='Markdown')
except:
bot.send_message(config.CHAT_ID,
prefix_all)
@bot.message_handler(commands=["allipaddr"])
def send_ip_all(message):
"""
Tạo lệnh nhập /allipaddr từ tele để lấy tất cả các ip address
Nếu số ký tự gửi về tele lớn hơn 4096,
sẽ chia ra gửi thành nhiều message
"""
ipaddr_list = list_ip_address()
ipaddr_all = str(ipaddr_list)
if len(ipaddr_all) > 4096:
for x in range(0, len(ipaddr_all), 4096):
try:
bot.send_message(config.CHAT_ID,
ipaddr_all[x:x+4096], parse_mode='Markdown')
except:
bot.send_message(config.CHAT_ID,
ipaddr_all[x:x+4096])
else:
try:
bot.send_message(config.CHAT_ID,
ipaddr_all, parse_mode='Markdown')
except:
bot.send_message(config.CHAT_ID,
ipaddr_all)
bot.polling()