官术网_书友最值得收藏!

Configuring devices using netmiko

Netmiko can be used to configure remote devices over SSH. It does that by accessing config mode using the .config method and then applies the configuration given in list format. The list itself can be provided inside the Python script or read from the file, then converted to a list using the readlines() method:

from netmiko import ConnectHandler

SW2 = {
'device_type': 'cisco_ios',
'ip': '10.10.88.112',
'username': 'admin',
'password': 'access123',
'secret': 'access123',
}

core_sw_config = ["int range gig0/1 - 2","switchport trunk encapsulation dot1q",
"switchport mode trunk","switchport trunk allowed vlan 1,2"]


print "########## Connecting to Device {0} ############".format(SW2['ip'])
net_connect = ConnectHandler(**SW2)
net_connect.enable()

print "***** Sending Configuration to Device *****"
net_connect.send_config_set(core_sw_config)

In the previous script, we did the same thing that we did before to connect to SW2 and enter enable mode, but this time we leveraged another netmiko method called send_config_set(), which takes the configuration in list format and accesses device configuration mode and starts to apply it. We have a simple configuration that modifies the gig0/1 and gig0/2 and applies trunk configuration on them. You can check if the command executed successfully by running show run command on the device; you should get output similar to the following:

主站蜘蛛池模板: 兴国县| 莱芜市| 建阳市| 盐城市| 鄯善县| 阿拉尔市| 界首市| 茂名市| 泉州市| 黄冈市| 临湘市| 阆中市| 砚山县| 英吉沙县| 刚察县| 合山市| 郴州市| 舞钢市| 蓬莱市| 铁岭市| 华池县| 思南县| 凤山县| 额济纳旗| 太湖县| 新河县| 肇庆市| 阿拉尔市| 东兴市| 平邑县| 阜新| 麻城市| 岳普湖县| 剑河县| 丹凤县| 新沂市| 三河市| 灌阳县| 黎平县| 峨眉山市| 太保市|