- Hands-On Enterprise Automation with Python.
- Bassem Aly
- 202字
- 2021-06-18 19:22:33
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:

- LabVIEW Graphical Programming Cookbook
- 大學計算機基礎實驗教程
- Neo4j Essentials
- Internet of Things with Intel Galileo
- 精通Python設計模式(第2版)
- jQuery開發(fā)基礎教程
- 深入淺出Serverless:技術(shù)原理與應用實踐
- PHP+MySQL+Dreamweaver動態(tài)網(wǎng)站開發(fā)從入門到精通(第3版)
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(2)
- Qlik Sense? Cookbook
- 從零開始學UI:概念解析、實戰(zhàn)提高、突破規(guī)則
- 城市信息模型平臺頂層設計與實踐
- Java程序性能優(yōu)化實戰(zhàn)
- 開源網(wǎng)絡地圖可視化:基于Leaflet的在線地圖開發(fā)
- C語言進階:重點、難點與疑點解析