/*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===*/

Học Python Qua Ví Dụ #014 Bài Tập - Mail Merge Xử Lý File Trong Python

Yêu Cầu:
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

Nội dung file ListDevices.csv


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 file
	if i < 10:
		with open("zbx_export_hosts_1.xml","a") as f1:
			f1.write(pl)
	elif i < 32:
		with open("zbx_export_hosts_2.xml","a") as f2:
			f2.write(pl)
	else:
		with open("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"]

with open ("zbx_export_hosts_2.xml","r") as f:
	noidung = f.read()
	
	with open("ListDevices.csv","r") as fld:
		fld.readline() # bỏ dòng đầu tiên

		for 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 in range(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 file
			with open ("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"]

with open ("zbx_export_hosts_MOI.xml","a") as wf:
	for i in range(0,len(files_list),1):
		
        with open (files_list[i],"r") as rf:
			content = rf.read()
			wf.write(content)


Tham khảo file sau khi nối zbx_export_hosts_MOI.xml

Xong!

Học Python Qua Ví Dụ #013 Bài Tập - While, For - Convert MAC Trong Python

Yêu cầu:

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

2. Convert Windows MAC to Cisco MAC

Thực Hiện:

1. Convert Cisco to Windows MAC 
Code:
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ữ hoa
    print()
    new_mac = []  # khởi tạo list rỗng
    while len(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)

Kết quả:
C:\python>python Demo.py

00:62:EC:29:70:FE

C8:9C:1D:EA:0E:B6

1C:6A:7A:AF:57:6C

52:54:AB:A8:9A:EA

52:54:AB:BE:5B:7B

52:54:AB:71:E1:19

52:54:AB:C7:26:AA

52:54:AB:3A:8D:26

52:54:AB:FB:AF:12

00:01:00:FF:00:01

00:02:00:FF:00:01

64:64:9B:E8:08:C8

00:1C:C4:BF:82:6A

00:1B:78:73:56:34

C:\python>

2. Convert Windows MAC to Cisco MAC 

Code:
mac_add = "00:62:EC-29-70-FE"
mac_add = mac_add.replace(":","")
mac_add = mac_add.replace("-","").lower()
print()
new_mac = []
while len(mac_add) > 0:
	entry = mac_add [:4]
	mac_add = mac_add [4:]
	new_mac.append(entry) 

new_mac = ".".join(new_mac)
print(new_mac)

Kết quả:
C:\python>python Demo.py

0062.ec29.70fe

C:\python>
 
Xong!

CDP - Cisco Discovery Protocol & LLDP - Link Layer Discovery Protocol - LAB


Sơ đồ lab:


Yêu cầu:

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
R2                      Eth 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 R2Platform: 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
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2013 by Cisco Systems, Inc.
Compiled Sat 23-Nov-13 03:28 by prod_rel_team

advertisement version: 2
Duplex: half
Management address(es): 
  IP address: 192.168.12.2

Total cdp entries displayed : 1
>
R1#


Nhận xét: Láng giềng R2 có địa chỉ IP là: 192.168.12.2; hệ điều hành đang dùng là: I86BI_LINUX-ADVENTERPRISEK9-M


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

R2#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
R3               Eth 0/2           136              R B   Linux Uni Eth 0/3
R1               Eth 0/1           138              R B   Linux Uni Eth 0/0

Total cdp entries displayed : 2
R2#

Có 2 láng giềng kết nối với R2 là: R1 và R3

R2#show cdp entry *
-------------------------
Device ID: R3
Entry address(es): 
  IP address: 192.168.23.3
Platform: Linux Unix,  Capabilities: Router Source-Route-Bridge 
Interface: Ethernet0/2,  Port ID (outgoing port): Ethernet0/3
Holdtime : 175 sec

Version :
Cisco IOS Software, Linux Software (I86BI_LINUX-ADVENTERPRISEK9-M), Version 15.4(1)T, DEVELOPMENT TEST SOFTWARE
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2013 by Cisco Systems, Inc.
Compiled Sat 23-Nov-13 03:28 by prod_rel_team

advertisement version: 2
Duplex: half
Management address(es): 
  IP address: 192.168.23.3

-------------------------
Device ID: R1
Entry address(es): 
  IP address: 192.168.12.1
Platform: Linux Unix,  Capabilities: Router Source-Route-Bridge 
Interface: Ethernet0/1,  Port ID (outgoing port): Ethernet0/0
Holdtime : 174 sec

Version :
Cisco IOS Software, Linux Software (I86BI_LINUX-ADVENTERPRISEK9-M), Version 15.4(1)T, DEVELOPMENT TEST SOFTWARE
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2013 by Cisco Systems, Inc.
Compiled Sat 23-Nov-13 03:28 by prod_rel_team

advertisement version: 2
Duplex: half
Management address(es): 
  IP address: 192.168.12.1

R2# 

Với câu lệnh show cdp entry * chúng ta có thể biết được thông tin chi tiết của tất cả các láng giềng đang kết nối với R2. 

Nếu muốn xem chi tiết của một láng giềng nào đó chúng ta dùng lệnh show cdp entry <Device ID/name>
R2#show cdp entry R3  
-------------------------
Device ID: R3
Entry address(es): 
  IP address: 192.168.23.3
Platform: Linux Unix,  Capabilities: Router Source-Route-Bridge 
Interface: Ethernet0/2,  Port ID (outgoing port): Ethernet0/3
Holdtime : 165 sec

Version :
Cisco IOS Software, Linux Software (I86BI_LINUX-ADVENTERPRISEK9-M), Version 15.4(1)T, DEVELOPMENT TEST SOFTWARE
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2013 by Cisco Systems, Inc.
Compiled Sat 23-Nov-13 03:28 by prod_rel_team

advertisement version: 2
Duplex: half
Management address(es): 
  IP address: 192.168.23.3

R2#

               
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

R1:
int Et0/0
no cdp enable

Kiểm tra:
R2#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
R3               Eth 0/2           173              R B   Linux Uni Eth 0/3

Total cdp entries displayed : 1
R2#

Thông cdp chỉ còn một láng giềng là R3 mà thôi.
Notes: chúng ta phải chờ hết holdtime là 180s cdp mới cập thông tin đúng nhé.

4. Cấu hình lldp trên R1 và R2, dùng lệnh Show lldp neighbors để kiểm tra.

R1, R2:
conf t
lldp run

Kiểm tra:
R2#show lldp neighbors 
Capability codes:
    (R) Router, (B) Bridge, (T) Telephone, (C) DOCSIS Cable Device
    (W) WLAN Access Point, (P) Repeater, (S) Station, (O) Other

Device ID           Local Intf     Hold-time  Capability      Port ID
R1                  Et0/1          120        R               Et0/0

Total entries displayed: 1

R2#

R1#show lldp neighbors 
Capability codes:
    (R) Router, (B) Bridge, (T) Telephone, (C) DOCSIS Cable Device
    (W) WLAN Access Point, (P) Repeater, (S) Station, (O) Other

Device ID           Local Intf     Hold-time  Capability      Port ID
R2                  Et0/0          120        R               Et0/1

Total entries displayed: 1

R1#

Thông tin LLDP trên R1, R2 có có thông tin của nhau.

Với CDP và LLDP có thể giúp chúng ta build lại sơ đồ kết nối của thiết bi.

Xong!

Học Python Qua Ví Dụ #012 Bài Tập - If, For Loops (Continue & Break), While Loop / Điều Kiện IF, Vòng Lặp For (Continue & Break), Vòng Lặp While Trong Python

Yêu Cầu:
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:

2. Đọc file show_arp.txt và xử lý nếu:

  • 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:

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:

Thực Hiện:

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 = []
with open("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_vlan

for 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

with open("show_arp.txt") as f:
    show_arp = f.read()

print()
found1, found2 = (False, False) # gán found1 = false, và found2 = false
for line in show_arp.splitlines():
    if 'protocol' in line.lower(): # nếu dòng nào có chữ 'protocol' thì bỏ qua
        continue
    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 = True  
    elif 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 = True

    if 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 for
        print("Exiting...")
        break

print()

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

with open("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_id
for 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ứ 2
        break
    elif 'Port id: ' in line: 
        _, port_id = line.split('Port id: ') # tương tự system_name, gán port_id cho trường thứ 2
    
    if port_id and system_name: # nếu port_id và system_name KHÁC RỖNG thì DỪNG
        break

print()
print("System Name: {}".format(system_name))
print("Port ID: {}".format(port_id))
print()

Kết quả:
C:\python>python Demo.py

System Name: twb-sf-hpsw1
Port ID: 15


C:\python>

Xong!

Học Python Qua Ví Dụ #011 - While Loops, Range/ Vòng Lặp While, Range Trong Python

Yêu cầu:
1. Vòng Lặp While 
 2. Range - Cú pháp của range()

Thực hiện:

1. Vòng Lặp While 
Code:
i = 0
while 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 = 2
print(list(range(start, stop, step)))

Kết quả:
C:\python>python Demo.py
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6, 8, 10, 12]

C:\python>

Xong!

Reset Password Zabbix Admin Via MySQL

Thông tin Username và Password mặc định của zabbix là:

  • Username: Admin
  • Password: zabbix

Nếu như chúng ta quên password thì không thể đăng nhập vào được. Tuy nhiên chúng ta có thể reset password của zabbix thông qua MySQL từ linux console.


Thực hiện:

  • Kết nối với MySQL với username root:

[root@CentOS ~]# mysql -u root -p

Enter password: <Nhập vào password của root -> Enter >

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 44903

Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 


  • Xem thông tin các database hiện tại
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| zabbix_DB          |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> 

Tên của cơ sở dữ liệu hiện tại là:  zabbix_DB

  • Kết nối vào database xem các user đang tồn tại trên zabbix
MariaDB [(none)]> use zabbix_DB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [zabbix_DB]> 

MariaDB [zabbix_DB]> select alias from users;
+---------+
| alias      |
+---------+
| Admin   |
| guest      |
| monitor  |
+---------+
3 rows in set (0.00 sec)
MariaDB [zabbix_DB]> 

Admin là username chúng ta cần

  • Reset password và thoát khỏi MySql
MariaDB [zabbix_DB]> update zabbix_DB.users set passwd=md5('P@ssw0rd') where alias='Admin';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

MariaDB [zabbix_DB]>exit;
Bye
[root@CentOS ~]# 

Bây giờ chúng ta có thể đăng nhập vào zabbix với password mới là: P@ssw0rd

Xong!

Reset Password Zabbix Admin Via MySQL

Thông tin Username và Password mặc định của zabbix là:

  • Username: Admin
  • Password: zabbix

Nếu như chúng ta quên password thì không thể đăng nhập vào được. Tuy nhiên chúng ta có thể reset password của zabbix thông qua MySQL từ linux console.


Thực hiện:

  • Kết nối với MySQL với username root:

[root@CentOS ~]# mysql -u root -p

Enter password: <Nhập vào password của root -> Enter >

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 44903

Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 


  • Xem thông tin các database hiện tại
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| zabbix_DB          |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> 

Tên của cơ sở dữ liệu hiện tại là:  zabbix_DB

  • Kết nối vào database xem các user đang tồn tại trên zabbix
MariaDB [(none)]> use zabbix_DB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [zabbix_DB]> 

MariaDB [zabbix_DB]> select alias from users;
+---------+
| alias      |
+---------+
| Admin   |
| guest      |
| monitor  |
+---------+
3 rows in set (0.00 sec)
MariaDB [zabbix_DB]> 

Admin là username chúng ta cần

  • Reset password và thoát khỏi MySql
MariaDB [zabbix_DB]> update zabbix_DB.users set passwd=md5('P@ssw0rd') where alias='Admin';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

MariaDB [zabbix_DB]>exit;
Bye
[root@CentOS ~]# 

Bây giờ chúng ta có thể đăng nhập vào zabbix với password mới là: P@ssw0rd

Xong!

Học Python Qua Ví Dụ #010 - Condition, Loop For Continue & Break - Hàm Điều Kiện, Vòng Lặp For, Continue & Break Trong Python

Yêu cầu
1. Điều kiện IF
2. Lặp For Continue, For Break , For Lồng

Thực hiện:
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ệnh
elif condition 2:
    # lệnh 
    # hoặc khối lệnh
elif condition 3:
    # lệnh 
    # hoặc khối lệnh
else:
    # lệnh 
    # hoặc khối lệnh

Ví dụ:
a = 10
if a == 10:
    print("DUNG") # nếu đúng a bằng 10 thì in ra chữ DUNG
else:
    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 list
	if 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 ra
	print (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 list
	if 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 for
	else:
		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.

Xong!
/*header slide*/