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

Network Automation #003 - Netmiko Backup VLAN Configuration on Cisco IOS Switch

 Nên xem các bài dưới đây trước khi xem bài này:


Sơ đồ lab:


Yêu cầu:

Dùng thư viện netmiko để SSH vào và thực hiện backup cấu vlan trên switch


Chuẩn bị:

        Sw:

enable
conf t
hostname Sw1
ip domain name netmiko.lab

username admin privilege 15 password admin1234@sw1

line vty 0 4
login local
transport input ssh
crypto key generate rsa general-keys modulus 1024
ip ssh version 2

interface vlan 1
no shutdown
ip address 192.168.0.8 255.255.255.0
exit

vlan 10-20
do wri


Thực hiện:

from netmiko import ConnectHandler 
from datetime import datetime

bk_device = { 
	"host":"192.168.0.8",
	"username":"admin",
	"password":"admin1234@sw1",
	"device_type":"cisco_ios" 
	}

tftpserver = "192.168.0.48" 

def cisco_ios_sw(txt): # định nghĩa hàm backup VLAN cho switch cisco ios
	
	cmd = "copy vlan.dat tftp:"
	filename = txt + "vlan.dat"
	
	net_connect = ConnectHandler(**bk_device)

	output = net_connect.send_command_timing(cmd)
	if "Address or name of remote host []" in  output:
		output += net_connect.send_command_timing(tftpserver)
	if "Destination filename" in output:
		output += net_connect.send_command_timing(filename)

	print(output)
	print("-" * 80)	


print(f"Dang ket noi vao IP:'{bk_device['host']}' voi Username: '{bk_device['username']}'")	

now = datetime.now().strftime("%Y-%b-%d_%H%M%S")
filename = now + "_" + bk_device["device_type"] + "_" + bk_device["host"] 

cisco_ios_sw(filename)


  • Kết quả:

C:\python>python Demo.py

Dang ket noi vao IP:'192.168.0.8' voi Username: 'admin'

Address or name of remote host []? Destination filename [vlan.dat]? !!

2076 bytes copied in 0.025 secs (83040 bytes/sec)

--------------------------------------------------------------------------------

C:\python>


Xong!


1 comment:

/*header slide*/