2.3.3 Sử dụng một số hàm trong thư viện netmiko
- Để gửi 1 câu lệnh đến Router/Switch qua SSH ở Privileged EXEC mode , ta dùng hàm send_commnad(), VD:
output = net_connect.send_command('sh ip int bri')
print(output)
- Để vào Privileged EXEC mode , ta dùng hàm enable() , VD :
net_connect.enable()
- Để gửi 1 câu lệnh đến Router/Switch ở config mode ta dùng hàm send_config_set(), VD:
net_connect.enable()
addIPInterface = ['int vlan 10','ip add 192.168.186.133 255.255.255.0','no shut']
net_connect.send_config_set(addIPInterface)
Một số lệnh khác thường dùng :
net_conn.save_config() - lưu cấu hình
net_connt.find_prompt() - trả về tên host R/Sw
net_conn.disconnect() - Đóng kết nối
2.3.4 File cấu hình vè kết quả thực thi
File lab_netmiko.py như sau :
import netmiko SW1 = { 'device_type':'cisco_ios', 'ip':'192.168.168.154', 'username':'admin', 'password':'123', 'secret':'321' } net_conn = netmiko.ConnectHandler(**SW1) net_conn.enable() for i in range(10,41,10): taoVlan = 'vlan ' + str(i) ipVlan = ['int vlan '+ str(i),'ip add 192.168.'+str(i)+'.1 255.255.255.0','no shutdown'] output1 = net_conn.send_config_set(taoVlan) output2 = net_conn.send_config_set(ipVlan) print(output1) print(output2) print(net_conn.send_command('show ip interface brief'))
Thực hiện chạy file lab_netmiko.py:
Ta click vào nút play màu xanh hoặc Shift + F10.Lưu ý file ta cần chạy là lab_netmiko.
Kết quả :
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 10
SW1(config-vlan)#end
SW1#
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#int vlan 10
SW1(config-if)#ip add 192.168.10.1 255.255.255.0
SW1(config-if)#no shutdown
SW1(config-if)#end
SW1#
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 20
SW1(config-vlan)#end
SW1#
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#int vlan 20
SW1(config-if)#ip add 192.168.20.1 255.255.255.0
SW1(config-if)#no shutdown
SW1(config-if)#end
SW1#
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 30
SW1(config-vlan)#end
SW1#
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#int vlan 30
SW1(config-if)#ip add 192.168.30.1 255.255.255.0
SW1(config-if)#no shutdown
SW1(config-if)#end
SW1#
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 40
SW1(config-vlan)#end
SW1#
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#int vlan 40
SW1(config-if)#ip add 192.168.40.1 255.255.255.0
SW1(config-if)#no shutdown
SW1(config-if)#end
SW1#
Interface IP-Address OK? Method Status Protocol
Ethernet0/0 192.168.168.154 YES DHCP up up
Ethernet0/1 unassigned YES unset up up
Ethernet0/2 unassigned YES unset up up
Ethernet0/3 unassigned YES unset up up
Ethernet1/0 unassigned YES unset up up
Ethernet1/1 unassigned YES unset up up
Ethernet1/2 unassigned YES unset up up
Ethernet1/3 unassigned YES unset up up
Ethernet2/0 unassigned YES unset up up
Ethernet2/1 unassigned YES unset up up
Ethernet2/2 unassigned YES unset up up
Ethernet2/3 unassigned YES unset up up
Ethernet3/0 unassigned YES unset up up
Ethernet3/1 unassigned YES unset up up
Ethernet3/2 unassigned YES unset up up
Ethernet3/3 unassigned YES unset up up
Vlan1 unassigned YES unset administratively down down
Vlan10 192.168.10.1 YES manual down down
Vlan20 192.168.20.1 YES manual down down
Vlan30 192.168.30.1 YES manual down down
Vlan40 192.168.40.1 YES manual down down
- Để gửi 1 câu lệnh đến Router/Switch qua SSH ở Privileged EXEC mode , ta dùng hàm send_commnad(), VD:
output = net_connect.send_command('sh ip int bri')
print(output)
- Để vào Privileged EXEC mode , ta dùng hàm enable() , VD :
net_connect.enable()
- Để gửi 1 câu lệnh đến Router/Switch ở config mode ta dùng hàm send_config_set(), VD:
net_connect.enable()
addIPInterface = ['int vlan 10','ip add 192.168.186.133 255.255.255.0','no shut']
net_connect.send_config_set(addIPInterface)
Một số lệnh khác thường dùng :
net_conn.save_config() - lưu cấu hình
net_connt.find_prompt() - trả về tên host R/Sw
net_conn.disconnect() - Đóng kết nối
2.3.4 File cấu hình vè kết quả thực thi
File lab_netmiko.py như sau :
import netmiko SW1 = { 'device_type':'cisco_ios', 'ip':'192.168.168.154', 'username':'admin', 'password':'123', 'secret':'321' } net_conn = netmiko.ConnectHandler(**SW1) net_conn.enable() for i in range(10,41,10): taoVlan = 'vlan ' + str(i) ipVlan = ['int vlan '+ str(i),'ip add 192.168.'+str(i)+'.1 255.255.255.0','no shutdown'] output1 = net_conn.send_config_set(taoVlan) output2 = net_conn.send_config_set(ipVlan) print(output1) print(output2) print(net_conn.send_command('show ip interface brief'))
Thực hiện chạy file lab_netmiko.py:
Ta click vào nút play màu xanh hoặc Shift + F10.Lưu ý file ta cần chạy là lab_netmiko.
Kết quả :
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 10
SW1(config-vlan)#end
SW1#
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#int vlan 10
SW1(config-if)#ip add 192.168.10.1 255.255.255.0
SW1(config-if)#no shutdown
SW1(config-if)#end
SW1#
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 20
SW1(config-vlan)#end
SW1#
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#int vlan 20
SW1(config-if)#ip add 192.168.20.1 255.255.255.0
SW1(config-if)#no shutdown
SW1(config-if)#end
SW1#
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 30
SW1(config-vlan)#end
SW1#
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#int vlan 30
SW1(config-if)#ip add 192.168.30.1 255.255.255.0
SW1(config-if)#no shutdown
SW1(config-if)#end
SW1#
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#vlan 40
SW1(config-vlan)#end
SW1#
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#int vlan 40
SW1(config-if)#ip add 192.168.40.1 255.255.255.0
SW1(config-if)#no shutdown
SW1(config-if)#end
SW1#
Interface IP-Address OK? Method Status Protocol
Ethernet0/0 192.168.168.154 YES DHCP up up
Ethernet0/1 unassigned YES unset up up
Ethernet0/2 unassigned YES unset up up
Ethernet0/3 unassigned YES unset up up
Ethernet1/0 unassigned YES unset up up
Ethernet1/1 unassigned YES unset up up
Ethernet1/2 unassigned YES unset up up
Ethernet1/3 unassigned YES unset up up
Ethernet2/0 unassigned YES unset up up
Ethernet2/1 unassigned YES unset up up
Ethernet2/2 unassigned YES unset up up
Ethernet2/3 unassigned YES unset up up
Ethernet3/0 unassigned YES unset up up
Ethernet3/1 unassigned YES unset up up
Ethernet3/2 unassigned YES unset up up
Ethernet3/3 unassigned YES unset up up
Vlan1 unassigned YES unset administratively down down
Vlan10 192.168.10.1 YES manual down down
Vlan20 192.168.20.1 YES manual down down
Vlan30 192.168.30.1 YES manual down down
Vlan40 192.168.40.1 YES manual down down
- Ta đã thực hiện thành công tạo nhiều vlan và cấu hình địa chỉ ip cho các vlan trên Switch thông qua sử dụng thư viện netmiko trong python.