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

Our first Paramiko program

Our first program will use the same general structure of the Pexpect program as far as looping over a list of devices and commands, with the exception of using Paramiko instead of Pexpect. This will give us a good compare and contrast of the differences between the two programs.

You can download the the code from the book GitHub repository, https://github.com/PacktPublishing/Mastering-Python-Networking. I will list out the notable differences here:

 devices = {'iosv-1': {'ip': '172.16.1.20'}, 'iosv-2': {'ip': '172.16.1.21'}}

We no longer need to match the device prompt using Paramiko, therefore, the device dictionary can be simplified:

   commands = ['show version', 'show run']

There is no send line equivalent in Paramiko, so we are manually including the newline break in each of the commands:

  def clear_buffer(connection):
if connection.recv_ready():
return connection.recv(max_buffer)

We are including a new method to clear the buffer for sending commands such as terminal length 0 or enable because we do not need to the output for those commands. We simply want to clear the buffer and get to the execution prompt. This function will later on be used in the loop such as in line 25:

      output = clear_buffer(new_connection)

The rest of the program should be pretty self explanatory, as we have seen the usage in this chapter. The last thing I would like to point out is that since this is an interactive program, we placed some buffer and wait for the command to be finished on the remote device before retrieving the output:

time.sleep(2)

After we clear the buffer, during the time in-between the execution of commands, we will wait two seconds. This will ensure us to give the device adequate time in case it is busy.

主站蜘蛛池模板: 喜德县| 修水县| 嘉兴市| 定结县| 壤塘县| 肥城市| 宿松县| 伊宁市| 连平县| 临安市| 北川| 福贡县| 方城县| 偏关县| 城口县| 锡林浩特市| 怀宁县| 拉萨市| 淳安县| 通城县| 公主岭市| 云龙县| 托克托县| 新泰市| 定安县| 吐鲁番市| 弋阳县| 鲁甸县| 望都县| 九江市| 宁阳县| 都匀市| 永寿县| 西和县| 昭苏县| 安多县| 呼图壁县| 岢岚县| 四川省| 宜昌市| 海丰县|