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

Putting things together for Paramiko

We are almost at the end of the chapter. In this last section, let's make the Paramiko program more reusable. There is one downside for our existing script; we need to open up the script every time we want to add or delete a host; or whenever we need to change the commands we have to execute on the remote host. Besides having a higher chance for mistakes, how about when you need to let your co-workers or NOC use the script? They might not feel comfortable working in Python, Paramiko, or Linux.

By making both the hosts and commands files that we read in as a parameter for the script, we can eliminate some of these concerns. The users (and a future you) can simply modify these text files when you need to make host or command changes.

The file is named chapter2_4.py.

We break the commands into a commands.txt file. Up to this point, we have been using show commands; in this example, we will make configuration changes for the logging buffer size:

$ cat commands.txt
config t
logging buffered 30000
end
copy run start

The devices information is written into a devices.json file. We choose JSON format for the devices information because JSON data types can be easily translated into Python dictionary data types:

$ cat devices.json
{
"iosv-1": {"ip": "172.16.1.20"},
"iosv-2": {"ip": "172.16.1.21"}
}

In the script, we made the following changes:

  with open('devices.json', 'r') as f:
devices = json.load(f)

with open('commands.txt', 'r') as f:
commands = [line for line in f.readlines()]

Here is an abbreviated output from the script execution:

$ python3 chapter2_4.py
Username: cisco
Password:
b'terminal length 0rniosv-2#config trnEnter configuration commands, one per line. End with CNTL/Z.rniosv-2(config)#'
b'logging buffered 30000rniosv-2(config)#'
...

Do a quick check to make sure the change has taken place in both running-config and startup-config:

iosv-1#sh run | i logging
logging buffered 30000
iosv-1#sh start | i logging
logging buffered 30000
iosv-2#sh run | i logging
logging buffered 30000
iosv-2#sh start | i logging
logging buffered 30000

主站蜘蛛池模板: 高雄市| 仲巴县| 新巴尔虎左旗| 昭苏县| 东莞市| 和田县| 于都县| 塔城市| 如东县| 博白县| 定襄县| 柘城县| 达日县| 友谊县| 涿鹿县| 潍坊市| 菏泽市| 冕宁县| 岗巴县| 长宁县| 富阳市| 赤壁市| 江油市| 犍为县| 绿春县| 澄江县| 甘洛县| 梧州市| 孟州市| 许昌县| 盘山县| 若羌县| 呈贡县| 东至县| 拉萨市| 油尖旺区| 遵义市| 陆河县| 洮南市| 永和县| 南阳市|