Python Exscript - JunOS

标签 python cisco juniper-network-connect exscript

我正在尝试运行一个脚本来显示所有配置并将它们写入瞻博网络和思科路由器的文件中。 到目前为止,CISCO 脚本正常工作,但问题在于 Juniper 路由器。

for ii in JUNIPER:
    print ii
    cmd2 = 'show configuration | display set'
    conn.connect(ii)
    conn.login(account1)
    conn.execute(cmd2)
    print conn.response
    #filerouter = open(ii, "w")
    #filerouter.write(conn.response)
    #filerouter.close()

获取要查询的设备列表后,我运行它,但它卡住了,好像缓冲区有限制...... -

如果我尝试执行不同的命令:
("显示配置|显示设置|匹配目的地")
-- 我将输出写在文件或屏幕上。

C:\Python27>python.exe C:\User\suserrr\Downloads\shrun.py
'clear' is not recognized as an internal or external command,
operable program or batch file.
Generating configs for ROUTER:  R1.test.site
Generating connect for ROUTER:  R2.test.site
==============
===========
routername
Traceback (most recent call last):
  File "C:\Users\userrr\Downloads\shrun.py", line 40, in <module>
    conn.execute(cmd2)
  File "C:\Python27\lib\site-packages\exscript-2.1.440-py2.7.egg\Exscript\protocols\Protocol.py", line 900, in execute
    return self.expect_prompt()
  File "C:\Python27\lib\site-packages\exscript-2.1.440-py2.7.egg\Exscript\protocols\Protocol.py", line 999, in expect_prompt
    result = self.expect(self.get_prompt())
  File "C:\Python27\lib\site-packages\exscript-2.1.440-py2.7.egg\Exscript\protocols\Protocol.py", line 980, in expect
    result = self._expect(prompt)
  File "C:\Python27\lib\site-packages\exscript-2.1.440-py2.7.egg\Exscript\protocols\Protocol.py", line 956, in _expect
    result = self._domatch(to_regexs(prompt), True)
  File "C:\Python27\lib\site-packages\exscript-2.1.440-py2.7.egg\Exscript\protocols\SSH2.py", line 329, in _domatch
    if not self._fill_buffer():
  File "C:\Python27\lib\site-packages\exscript-2.1.440-py2.7.egg\Exscript\protocols\SSH2.py", line 303, in _fill_buffer
    raise TimeoutException(error)
Exscript.protocols.Exception.TimeoutException: Timeout while waiting for response from device

========== ==== 问题 - 如何让脚本运行并提供命令的输出: show configuration |显示设置 第二张图片显示了我得到的错误,但如果我将命令更改为: show configuration |显示组|匹配描述 我得到了所请求的信息。我是否缺少在模块中添加一些内容以便 exscript/python 避免超时?

最佳答案

默认情况下,JunOS 对任何命令返回的冗长输出进行分页。可能发生的情况是,您正在连接的 Juniper 设备正在对 show configuration | 的输出进行分页。 display set 命令,Exscript 超时,因为设备正在等待用户输入以继续对命令输出进行分页,而不是返回 Exscript 识别的提示。

我会进行以下修改:

for ii in JUNIPER:
    print ii
    cmd2 = 'show configuration | display set | no-more'
    conn.connect(ii)
    conn.login(account1)
    conn.execute(cmd2)
    print conn.response

这将禁用该特定命令的输出分页,并且应立即返回到提示符并允许 Exscript 将输出返回给您。为了更好地衡量,我还在命令中添加了回车符,即:

cmd2 = 'show configuration | display set | no-more\r'

但是执行上述操作的有用性是值得商榷的,就好像我没记错 execute() 方法应该为你做这件事一样。

关于Python Exscript - JunOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34042841/

相关文章:

python - pretty-print Python xml.dom.Element 对象

python - 无法从网页获取所有图像

python - 预计 setecho 不工作

python - 使用 Juniper 执行的 Paramiko 回显命令

javascript - Juniper SSL VPN 中的 Angular Web 应用程序路由

python - 如何在 Scikit-Learn 中绘制超过 10 倍交叉验证的 PR 曲线

python - 测量纬度/经度坐标和 utm 坐标之间的距离

python - SSL.SSLError : tlsv1 alert protocol version

php - 使用 SNMP WALK 获取一个端口上的 MAC 地址列表