首页
学习
活动
专区
圈层
工具
发布

JunOS
EN

Stack Overflow用户
提问于 2015-12-02 12:31:26
回答 3查看 1.4K关注 0票数 1

我试图运行一个脚本来显示所有的配置,并将它们写入juniper和CISCO路由器的文件中。到目前为止,CISCO脚本可以正常工作,但是问题是与juniper路由器有关。

代码语言:javascript
复制
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 ")

--我把输出写在文件或屏幕上。

代码语言:javascript
复制
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避免超时?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-12-02 23:15:35

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

我要作以下修改:

代码语言:javascript
复制
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将输出返回给您。为了更好地衡量,我还添加了一辆马车返回我的命令,即:

代码语言:javascript
复制
cmd2 = 'show configuration | display set | no-more\r'

但是,做上面的工作是否有用还有待商榷,似乎我正确地记得execute()方法应该会为您做这件事。

票数 3
EN

Stack Overflow用户

发布于 2016-06-15 09:01:03

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

代码语言:javascript
复制
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()
票数 0
EN

Stack Overflow用户

发布于 2018-02-01 22:43:49

我使用这个脚本使用PyEZ和JSON来使用多个IP地址。

代码语言:javascript
复制
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文件如下所示:

代码语言:javascript
复制
   {
  "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"
           ]
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34042841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档