1

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

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()

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

如果我尝试执行不同的命令:
("show configuration | display set | match destination ")
-- 我将输出写入文件或屏幕。

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 | display set第二张图片显示了我得到的错误,但如果我将命令更改为:show configuration | display set | match description我获取要求的信息。我是否想在模块中添加一些内容以便 exscript/python 避免超时?

4

3 回答 3

3

默认情况下,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()无论如何该方法应该为您执行此操作。

于 2015-12-02T23:15:35.597 回答
0

对于使用 python 处理 Junos 设备,我建议您使用 PyEZ - https://github.com/Juniper/py-junos-eznc

from jnpr.junos import Device
from lxml import etree

dev = Device('hostname', user='username', password='Password123')
dev.open()

cnf = dev.rpc.get_config()    # similar to 'show configuration | no-more' on cli
print (etree.tounicode(cnf))

dev.close()
于 2016-06-15T09:01:03.753 回答
0

我使用带有 JSON 的 PyEZ 使用此脚本来使用多个 IP 地址。

from jnpr.junos import Device
from lxml import etree
import json


config_file = open('config.json')
config = json.load(config_file)
config_file.close()


for host in config['ip']:

    dev = Device(host=host, user=config['username'], 
    password=config['password'], port=22)
    dev.open()
    data = dev.rpc.get_config(options={'format':'set'})
    file_name = dev.facts['fqdn']
    print(etree.tostring(data))
    dev.close()

    f = open(file_name + '.txt', 'w')
    f.write(etree.tostring(data))
    f.close()

JSON 文件如下所示:

   {
  "username": "user",
  "password": "password",
  "ip": [
             "10.255.6.100",
             "10.255.6.101",
             "10.255.6.102",
             "10.255.6.103",
             "10.255.6.104"
           ]
}
于 2018-02-01T22:43:49.897 回答