/*auto readmore*/
/*auto readmore*/
/* an hien script*/
// an hien password
/*an hien ma chuong trinh cong tru */
/*Scrollbox thanh cuon*/
/***Nhung CODE***/
/*
dòng xanh dòng trắng
*/
/*
https://cdnjs.com/libraries/prism lay thu vien, can vao ten file ma goi
1. copy link vao vi du:prism-python.min.js
2. ten ngon nua la python
*/
/*===
New posts
===*/
/*header slider*/
/*=== bai viet lien quan===*/
/*===tabcode===*/
Trộn và nối các trường tương ứng trong file ListDevices.csv vào file
zbx_export_hosts.xml, cụ thể là mỗi một dòng của file ListDevices.csv điền vào
các biến ở các dòng 12, 13, 14, 17, 22 và 27 để tạo file mới có tên
zbx_export_hosts_MOI.xml
Nội dung file zbx_export_hosts.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Nhận xét: Chúng ta thấy trong file zbx_export_hosts.xml từ dòng 11 cho
đến dòng 32 đó là cấu tạo để định nghĩa một host trong file này và cho phép
lặp lại. Nên chúng tôi đưa ra đề xuất giải quyết bài toán này là:
1. Tách file ban đầu ra làm 3 file nhỏ:
- Từ dòng đầu tiên đến dòng thứ 10 là file: zbx_export_hosts_1.xml
- Từ dòng thứ 11 đến dòng thứ 32 là file: zbx_export_hosts_2.xml
- Từ dòng thứ 33 đến hết file là file: zbx_export_hosts_3.xml
2. Thực hiện nối các biến tương ứng theo yêu cầu đề bài và lưu lại với tên
file mới là zbx_export_hosts_2EDIT.xml
3. Thực hiện nối 3: zbx_export_hosts_1.xml, zbx_export_hosts_2EDIT.xml,
zbx_export_hosts_3.xml thành file zbx_export_hosts_MOI.xml
Thực Hiện:
1. Tách file ban đầu ra làm 3 file nhỏ:
- Từ dòng đầu tiên đến dòng thứ 10 là file: zbx_export_hosts_1.xml
- Từ dòng thứ 11 đến dòng thứ 32 là file: zbx_export_hosts_2.xml
- Từ dòng thứ 33 đến hết file là file: zbx_export_hosts_3.xml
Code:
f =open("zbx_export_hosts.xml","r")
i =0
cont1 =[]for pl in f:# đọc cho đến hết fileif i <10:withopen("zbx_export_hosts_1.xml","a")as f1:
f1.write(pl)elif i <32:withopen("zbx_export_hosts_2.xml","a")as f2:
f2.write(pl)else:withopen("zbx_export_hosts_3.xml","a")as f3:
f3.write(pl)
i +=1
f.close
2. Thực hiện nối các biến tương ứng theo yêu cầu đề bài và lưu lại với
tên file mới là zbx_export_hosts_2EDIT.xml
Code:
'''
định nghĩa list key_word có nội_dung/giá trị là các key word cần thay thế
Vì trong file ListDevices.csv có cột đầu tiên là No. số thứ tự không dùng trong việc thay thế
Nên thêm key No. để biến i trong vòng lặp for dễ hiểu hơn
Key No. sẽ không dùng
'''
key_word =["No.","host12","name13","des14","Temp17","Group22","ip_addr27"]withopen("zbx_export_hosts_2.xml","r")as f:
noidung = f.read()withopen("ListDevices.csv","r")as fld:
fld.readline()# bỏ dòng đầu tiênfor line_dev in fld.readlines():
fiedl_dev = line_dev.split(",")# tách mỗi dòng thành 1 list
noidung_moi = noidung
for i inrange(0,len(key_word),1):# thay thế tất cả các key vào nội dung
noidung_moi = noidung_moi.replace(key_word[i], fiedl_dev[i])# ghi file sau khi thay thế xong, give vào cuối filewithopen("zbx_export_hosts_2EDIT.xml","a")as wf:
wf.write(noidung_moi)
3. Thực hiện nối 3: zbx_export_hosts_1.xml, zbx_export_hosts_2EDIT.xml,
zbx_export_hosts_3.xml thành file zbx_export_hosts_MOI.xml
Code:
#định nghĩa list gồm tên của file cần nối vào file mới
files_list =["zbx_export_hosts_1.xml","zbx_export_hosts_2EDIT.xml","zbx_export_hosts_3.xml"]withopen("zbx_export_hosts_MOI.xml","a")as wf:for i inrange(0,len(files_list),1):withopen(files_list[i],"r")as rf:
content = rf.read()
wf.write(content)
1. Convert Cisco to Windows MAC - Có bảng arp yêu cầu lấy ra cột mac address
(định dạng cisco: xxxx.xxxx.xxxx) và chuyển sang định dạng MAC của Windows
(xx:xx:xx:xx:xx:xx)
Nội dung list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
arp_table =[('10.220.88.1','0062.ec29.70fe'),('10.220.88.20','c89c.1dea.0eb6'),('10.220.88.21','1c6a.7aaf.576c'),('10.220.88.28','5254.aba8.9aea'),('10.220.88.29','5254.abbe.5b7b'),('10.220.88.30','5254.ab71.e119'),('10.220.88.32','5254.abc7.26aa'),('10.220.88.33','5254.ab3a.8d26'),('10.220.88.35','5254.abfb.af12'),('10.220.88.37','0001.00ff.0001'),('10.220.88.38','0002.00ff.0001'),('10.220.88.39','6464.9be8.08c8'),('10.220.88.40','001c.c4bf.826a'),('10.220.88.41','001b.7873.5634')]for ip_addr, mac_addr in arp_table:
mac_addr = mac_addr.split(".")
mac_addr ="".join(mac_addr)
mac_addr = mac_addr.upper()# chuyển sang chữ hoaprint()
new_mac =[]# khởi tạo list rỗngwhilelen(mac_addr)>0:# khi độ dài của MAC address lớn hơn 0
entry = mac_addr[:2]# lấy 2 ký tự đầu tiên
mac_addr = mac_addr[2:]# MAC address mới sẽ là chuỗi ký tự từ vị trí thứ 3 đến hết chuỗi (ở đây chiều dài của mac_addr đã giảm đi 2)
new_mac.append(entry)# cứ 2 ký tự nối là thành phần của list
new_mac =":".join(new_mac)print(new_mac)
1. Dùng các lệnh show cdp neighbors, show cdp neighbors detail trên R1 để xem thông tin của router láng giềng
2. Dùng các lệnh show cdp neighbors, show cdp entry * trên R2 để xem thông tin các router láng giềng
3. Tắt cdp trên Et0/0 của R1, dùng lệnh show cdp neighbors trên R2
kiểm tra và nhận xét
4. Cấu hình lldp trên R1 và R2, dùng lệnh show lldp neighbors để
kiểm tra.
Chuẩn bị:
R1:
enable
conf t
hostname R1
interface Ethernet0/0
description ===Connect to R2 Et0/1===
ip address 192.168.12.1 255.255.255.0
no shutdown
exit
do wri
R2:
enable
conf t
hostname R2
interface Ethernet0/1
description ===Connect to R1 Et0/0===
ip address 192.168.12.2 255.255.255.0
no shutdown
exit
interface Ethernet0/2
description ===Connect to R3 Et0/3===
ip address 192.168.23.2 255.255.255.0
no shutdown
exit
do wri
R3:
enable
conf t
hostname R3
interface Ethernet0/3
description ===Connect to R2 Et0/2===
ip address 192.168.23.3 255.255.255.0
no shutdown
exit
do wri
Thực hiện:
1. Dùng các lệnh show cdp neighbors, show cdp neighbors detail trên R1 để xem thông tin của router láng giềng
R1:
R1>show cdp neighbors
Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
S - Switch,
H - Host, I - IGMP, r - Repeater, P - Phone,
D - Remote,
C - CVTA, M - Two-port Mac Relay
Device ID Local Intrfce
Holdtme Capability Platform Port ID
R2Eth 0/0 148
R B Linux
Uni Eth 0/1
Total cdp entries displayed : 1
R1>
Nhận xét:R1 đang dùng port Eth 0/0 nối với port Eth 0/1 của R2. Platform: vì là làm trên lab ảo nên chổ này chỉ hiển thị là Linux Uni, nếu
là thiết bị thật sẽ chổ này sẽ hiển thị đúng flatform của thiết bị
R1:
R1#show cdp neighbors detail
-------------------------
Device ID: R2
Entry address(es):
IP address: 192.168.12.2
Platform: Linux Unix, Capabilities: Router
Source-Route-Bridge
Interface: Ethernet0/0, Port ID (outgoing port): Ethernet0/1
Holdtime : 158 sec
Version :
Cisco IOS Software, Linux Software (I86BI_LINUX-ADVENTERPRISEK9-M), Version 15.4(1)T, DEVELOPMENT TEST SOFTWARE
1. Đọc file show_vlan.txt và xử lý để lấy ra các trường VLAN ID, VLAN
NAME
Nội dung file show_vlan.txt:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tìm thấy 10.220.88.1 thì int ra "Default gateway IP/Mac" với IP và
MAC tương ứng
Tìm thấy 10.220.88.30 thì in ra "Arista3 IP/Mac is" với IP và MAC tương ứng
Nội dung fileshow_arp.txt:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3. Đọc file show_lldp_neighbors_detail.txt và xử lý tìm ra nội dung của
System Name, và Port ID của thiết bị láng giềng.
Nội dung file show_lldp_neighbors_detail.txt:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Đọc file show_vlan.txt và xử lý để lấy ra các trường VLAN ID,
VLAN NAME
Code:
from __future__ import unicode_literals, print_function
from pprint import pprint
vlan_list =[]withopen("show_vlan.txt","r")as f:# Mở file lên đọc và tự động đóng file
show_vlan = f.read()# Gán nội dung của file vào biến show_vlanfor line in show_vlan.splitlines():# trả về một chuỗi tương ứng là một dòng trong biến show_vlan'''
Sẽ bỏ qua các dòng nếu có một trong các điều kiện của if
'''if'VLAN'in line or'-----'in line or line.startswith(' '):continue
fields = line.split()# tách ra thành list nhỏ, căn cứ vào dấu khoảng trắng
vlan_id = fields[0]# lấy trường đầu tiên
vlan_name = fields[1]# lấy trường thứ 2
vlan_list.append((vlan_id, vlan_name))print()
pprint(vlan_list)print()
Kết quả:
C:\python>python Demo.py
[('1', 'default'),
('400', 'blue400'),
('401', 'blue401'),
('402', 'blue402'),
('403', 'blue403')]
C:\python>
2. Đọc file show_arp.txt và xử lý để lấy ra các trường IP, MAC.
Code:
from __future__ import unicode_literals, print_function
withopen("show_arp.txt")as f:
show_arp = f.read()print()
found1, found2 =(False,False)# gán found1 = false, và found2 = falsefor line in show_arp.splitlines():if'protocol'in line.lower():# nếu dòng nào có chữ 'protocol' thì bỏ quacontinue
fields = line.split()# chuyển mỗi dòng đọc được thành list
ip_addr = fields[1]
mac_addr = fields[3]if ip_addr =='10.220.88.1':# nếu trường thứ 2 là '10.220.88.1'print("Default gateway IP/Mac is: {}/{}".format(ip_addr, mac_addr))# in địa chỉ ip và mac
found1 =Trueelif ip_addr =='10.220.88.30':# nếu trường thứ 2 là '10.220.88.30'print("Arista3 IP/Mac is: {}/{}".format(ip_addr, mac_addr))
found2 =Trueif found1 and found2:# nếu cả 2 đã tìm được thì DỪNG không cần thực hiện hết vòng forprint("Exiting...")breakprint()
Kết quả:
C:\python>python Demo.py
Default gateway IP/Mac is: 10.220.88.1/0062.ec29.70fe
Arista3 IP/Mac is: 10.220.88.30/5254.ab71.e119
Exiting...
C:\python>
3. Đọc file show_lldp_neighbors_detail.txt và xử lý tìm ra nội dung của System Name, và Port ID của thiết bị láng giềng.
Code:
from __future__ import unicode_literals, print_function
withopen("show_lldp_neighbors_detail.txt")as f:
show_lldp = f.read()
system_name, port_id =(None,None)# khởi tạo biến ban đầu là rỗng cho system_name và port_idfor line in show_lldp.splitlines():if'System Name: 'in line:# nếu tìm thấy thoát khỏi if
_, system_name = line.split('System Name: ')# gán biến _ cho trường đầu tiên, gán system_name là trường thứ 2breakelif'Port id: 'in line:
_, port_id = line.split('Port id: ')# tương tự system_name, gán port_id cho trường thứ 2if port_id and system_name:# nếu port_id và system_name KHÁC RỖNG thì DỪNGbreakprint()print("System Name: {}".format(system_name))print("Port ID: {}".format(port_id))print()
i =0while i <=5:# khi nhỏ hơn hoặc bằng 5 thì thực hiện (i = 6 sẽ dừng) print("Hello world!")
i +=1# i = i + 1, tăng i lên một đơn vịelse:print("Done!")
Kết quả:
C:\python>python Demo.py
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Done!
C:\python>
2. Range - Cú pháp của range()
Code:
'''
- với range nếu chỉ có một tham số thì python ngầm hiểu đó là: stop
- range(stop)
- Kết quả trả về là dãy số từ 0 -> stop - 1
'''print(list(range(10)))'''
- với rang nếu với 2 tham số thì python hiểu là: start, stop
- range(start, stop)
- python ngầm hiểu bước nhảy sẽ là 1
'''print(list(range(1,10)))'''
- rang có thể sử dụng tham số truyền vào
- start: bắt đầu là số 2
- step: bước nhảy là 2, hoặc tăng start lên 2 đơn vị
- stop: tăng cho đến 14 - 1 thì dừng (trong ví dụ này kết quả in ra sẽ không có 14)
'''
start =2
stop =14
step =2print(list(range(start, stop, step)))
1. Cấu trúc lệnh if: tạm hiểu theo tiếng Việt là: NẾU đùng THÌ
làm....
if condition 1:# lệnh # hoặc khối lệnhelif condition 2:# lệnh # hoặc khối lệnhelif condition 3:# lệnh # hoặc khối lệnhelse:# lệnh # hoặc khối lệnh
Ví dụ:
a =10if a ==10:print("DUNG")# nếu đúng a bằng 10 thì in ra chữ DUNGelse:print("SAI")# hoặc ngược lại - nếu sai (a khác 10) thì in ra chữ SAI
Kết quả:
C:\python>python Demo.py
DUNG
C:\python>
2. Lặp For Continue, For Break, For Lồng
Lặp For và Continue:
Code:
ip_list =["192.168.1.1","192.168.1.2","192.168.1.3","192.168.1.4"]for ip in ip_list:# đọc cho đến hết listif ip =="192.168.1.3":# nếu trong list có giá trị là "192.168.1.3" thìcontinue# nhảy qua index kế tiếp mà không in giá trị ở điều kiện if raprint(ip)
Kết quả:
C:\python>python Demo.py
192.168.1.1
192.168.1.2
192.168.1.4
C:\python>
Chúng ta thấy giá "192.168.1.3" trong điều kiện if không được in ra. Với continue thì nó vẫn tiếp vòng for.
Lặp For và Break
Code:
ip_list =["192.168.1.1","192.168.1.2","192.168.1.3","192.168.1.4"]for ip in ip_list:# đọc cho đến hết listif ip =="192.168.1.3":# nếu trong list có giá trị là "192.168.1.3" thìbreak# dừng và thoát ra khỏi forelse:print(ip)# nếu ip KHÔNG phải là "192.168.1.3" thì print ra.
Kết quả:
C:\python>python Demo.py
192.168.1.1
192.168.1.2
C:\python>
Nó hành xử là: nếu trong list có giá trị của điều kiện if thì break sẽ thoát ra khỏi for luôn, mà không thực hiện khối lệnh bên dưới.
Lăp For Lồng:
Code:
ip_list =["192.168.1.1","192.168.1.2","192.168.1.3","192.168.1.4"]
ip_net =["8.8.8.8","4.4.4.4"]for ip in ip_list:for ipnet in ip_net:print(ip)print(ipnet)
Kết quả:
C:\python>python Demo.py
192.168.1.1
8.8.8.8
192.168.1.1
4.4.4.4
192.168.1.2
8.8.8.8
192.168.1.2
4.4.4.4
192.168.1.3
8.8.8.8
192.168.1.3
4.4.4.4
192.168.1.4
8.8.8.8
192.168.1.4
4.4.4.4
C:\python>
Kết quả in ra là phần tử thứ nhất của ip_list với từng phần tử của ip_net, và
tương tự với các phần tử còn lại của ip_list.