Fabric python无法生成完整输出并在中间中止

时间:2017-04-03 18:10:45

标签: python fabric

我正在尝试使用cisco Nexus交换机获取少量输入并输出显示与错误断开连接

错误:

  c:\Python27>Scripts\fab.exe -f D:\Python-scripts\nexus-fabric.py sh  -H     10.10.10.10 -u admin
  [10.10.10.10] Executing task 'sh'
  [10.10.10.10] run: terminal length 0
  [10.10.10.10] Login password for 'admin':
  [10.10.10.10] out: gl_set_term_size: NULL arguments(s).
  [10.10.10.10] out:

  [10.10.10.10] run: show interface brief
  [10.10.10.10] out:
  [10.10.10.10] out: -------------------------------------------------------   ------------------------
  [10.10.10.10] out: Interface  Vsan   Admin  Admin   Status          SFP         Oper  Oper   Port
  [10.10.10.10] out:                   Mode   Trunk                            Mode  Speed  Channel
  [10.10.10.10] out:                          Mode                                 (Gbps)
 [10.10.10.10] out: -------------------------------------------------------------------------------
 [10.10.10.10] out: fc2/1      4094   auto   on      sfpAbsent        --     --           --
 [10.10.10.10] out:


 Fatal error: run() received nonzero return code -1 while executing!

 Requested: show interface brief
 Executed: show interface brief

 Aborting.
 Disconnecting from 10.10.10.10... done.

代码:

  from fabric.api import run
  def sh():
     run("terminal length 0",shell=False)
     run("show interface brief",shell=False

我使用Pass选项仍然失败:

from fabric.api import run
from fabric.api import settings
from fabric.api import warn_only
def sh():
    try:
       run("show running-config",shell=False)
       run("show interface brief",shell=False)
    except:
       pass

如果我们运行一个命令,我使用get输出没有问题,但如果我使用两个运行命令第二个命令终止以下错误

致命错误:run()在执行时收到非零返回码-1

我按照建议尝试了异常,但我得到的是代码和错误:

from fabric.api        import env,run
from fabric.operations import sudo

class FabricException(Exception):
   pass

env.abort_exception = FabricException
# ... set up the rest of the environment...

def sh():
    try:
       run("show interface brief",shell=False)
       run("show running-config",shell=False)
    except FabricException:
        pass  # This is expected, we can continue.

如果 请帮忙

Fabric版本:

     c:\Python27>Scripts\fab.exe --version
     Fabric 1.13.1
     Paramiko 2.1.2

Python 2.7

请帮助

1 个答案:

答案 0 :(得分:0)

更新回答:

由于Cisco路由器不是简单的unix,因此Fabric在这些系统上无法可靠地工作。有关详细信息,请参阅this github问题。

旧答案

Fabric默认以快速失败行为运行,docanswer中都说明了这一点。我看到你的命令并不重要,你主要用于检查目的,然后你可以通过如下包装来忽略退出代码:

with settings(warn_only=True):
    run("show interface brief",shell=False)

这将跳过行甚至命令以非零返回结束。最后,您需要使用以下import语句来使with块工作:

from fabric.api import settings

<强>更新

在不使用任何设置包装器的情况下完全忽略命令期间的任何失败:

try:
    run("show interface brief",shell=False)
except:
    pass

更新2(包含异常类)

取自answer

from fabric.api        import env
from fabric.operations import sudo

class FabricException(Exception):
    pass

env.abort_exception = FabricException
# ... set up the rest of the environment...

def sh():
    try:
        run("show interface brief",shell=False)
    except FabricException:
        pass  # This is expected, we can continue.
相关问题