1

I am trying fabric to fetch few inputs from cisco Nexus switch and output displaying disconnecting with error

error:

  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.

code:

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

I used Pass option still it is failing:

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

If we run one command i used get output without issues but if i use two run commands second command is terminating with below error

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

I have tried exception as suggested but i am getting below is code and error:

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.

If Please help

Fabric version :

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

Python 2.7

please help

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
asteroid4u
  • 79
  • 2
  • 9
  • What happens when you ssh into server and type failing command manually? After the command, you can also type `echo $?` to see the exit code, that would be helpful if you can provide that too. – hurturk Apr 03 '17 at 18:19
  • This is network device so cant use echo $? command and I done manually ssh to switch and execute above command and it is working fine, meaning able to see full out. – asteroid4u Apr 03 '17 at 18:26
  • Okay, what about the output of the manual typing? Is it showing only single interface or more? I think you are already happy with output of the command, you probably mean rest is not executed? – hurturk Apr 03 '17 at 21:21

1 Answers1

0

Updated answer:

As Cisco routers are not plain unix, Fabric won't work reliably on such systems. See this github issue for further details.

Old answer:

Fabric runs in fast-fail behavior as default, that is both stated in doc and this answer. I see your command is not critical and you use it mostly for inspection purposes, then you might ignore the exit code by wrapping it as follows:

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

That will skip the line even command ends with non-zero return. Finally, you would need following import statement to make with block work:

from fabric.api import settings

Update

To completely ignore any failure during the command without using any settings wrapper:

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

Update 2 (with exception class)

Taken from this 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.
Community
  • 1
  • 1
hurturk
  • 5,214
  • 24
  • 41
  • Still having same issue.from fabric.api import run from fabric.api import settings def shrun(): with settings(warn_only=True): run("show running-config",shell=False) def shint(): with settings(warn_only=True): run("show interface brief",shell=False) – asteroid4u Apr 04 '17 at 02:50
  • If i use only single run then it will work and if i use two runs second run is failing . – asteroid4u Apr 04 '17 at 03:26
  • Could you add try/except I have updated in answer, which is an overkill but at least should get it done. – hurturk Apr 04 '17 at 04:06
  • Still failing with pass option and added my script in question – asteroid4u Apr 06 '17 at 02:43
  • Is it possible for you to upgrade fabric to 1.3.4 at least and try `warn_only` or exception handling again? As well as this [answer](http://stackoverflow.com/a/27990242/1233686). – hurturk Apr 06 '17 at 03:19
  • I am using latest 1.13.1 – asteroid4u Apr 06 '17 at 03:39
  • Oh sorry just noticed you have higher, did setting an exception class work? Lasty, you might try running fab command with `-w` option, or downgrade. – hurturk Apr 06 '17 at 03:54
  • with option -w is also same error. Could you please guide the exception class? – asteroid4u Apr 06 '17 at 19:23
  • I just added as update 2, it is weird there is nothing left to ignore the failure. Trying a smaller major version of fabric maybe? – hurturk Apr 06 '17 at 19:44
  • if you have added `.. import *` you should remove it and make explicit imports instead, as stated in reddit thread [here](https://www.reddit.com/r/learnpython/comments/2zj3gl/fabric_it_will_not_read_my_defined_hosts_no_hosts/). – hurturk Apr 07 '17 at 03:10
  • still same issue i imported settings, run and other than this anything i should have to import and i have update code in my question – asteroid4u Apr 07 '17 at 03:40
  • `with settings(abort_exception = FabricException):` this look excessive anyway, I didn't have that. Are you sure your last code runs under a function like `def sh():` as in your earlier attempt? – hurturk Apr 07 '17 at 03:49
  • def sh code slightly i changed edited quetion and i am sure that is running and 1st run successful and second run failed. I am wonder any command you put at first it will work with exception pass second command fails – asteroid4u Apr 07 '17 at 04:19
  • Your last example is not under `def sh():` or `def conf():`, you should edit again. I have added `def sh():` to **update 2** in my answer. – hurturk Apr 07 '17 at 04:30
  • sorry i just able to run but still failing second command and any other way to fix or its bug kind? – asteroid4u Apr 07 '17 at 04:40
  • Well it could be a bug with windows if nothing makes it continue, I would try running this from a linux machine (copy/paste code there) to prove then would open a github ticket if it works. – hurturk Apr 07 '17 at 04:50
  • Then it might be still fabric/paramiko bug, I think this goes to github. You can refer stackoverflow question link there, so they can see what you tried already. – hurturk Apr 07 '17 at 04:59
  • hi hurturk, where do we need to log bug? do you have link please – asteroid4u Apr 07 '17 at 05:01