cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
5123
Views
20
Helpful
9
Replies

Suppress Genie output when using in a script

Mike Assel
Level 4
Level 4

I'm playing around with using Genie in a python script. Whenever I do anything with Genie, like device.connect(), it prints the output to the console. How do I suppress that?  Thanks, Mike

1 Accepted Solution

Accepted Solutions

After further research, there are two other methods:

1) When you initially connect to a device, you can pass the argument dev.connect(log_stdout=False) to disable logging; this disable will be in-effect for the entire session until you re-enable it.

2) The device instance, after connection, provide a dev.log_user() method, which can be enabled/disabled passing True/False.

View solution in original post

9 Replies 9

Jeremy Schulman
Level 1
Level 1

Given a device instance, you can change the log settings via the connectionmgr instance. 

 

For example:

import logging

# dev is a Device instance from a testbed

dev.connectionmgr.log.setLevel(logging.ERROR)

# now execute a command and you will not see the output on the console
resp = dev.execute('show version')

# you can print the response
print(resp)

 

After further research, there are two other methods:

1) When you initially connect to a device, you can pass the argument dev.connect(log_stdout=False) to disable logging; this disable will be in-effect for the entire session until you re-enable it.

2) The device instance, after connection, provide a dev.log_user() method, which can be enabled/disabled passing True/False.

Thanks Jeremy! That did the trick. However when I use dev.connect(log_stdout=False) then the output is not logged to a file. A few follow up questions:

  1. How did you find dev.connect(log_stdout=False) ???? I did a lot of googling and NOTHING comes up (other than your blog post :)
  2. Assuming I can disable logging to the console but enable logging to a file, how would I specify the path?

Hi @Mike Assel,

 

re: connect(log_stdout=False) I learned via a response on Twitter from one of the DevNet genie developers.  After I learned this, I did a scan of the sourcecode and I could not find any reference to this Device() argument either.  Seem like an opportunity for the DevNet team to add more documentation.

 

re: logging - even though the console logging is disabled, there is still a logfile.  You can find it using the Device.logfile attribute, for example:

 

In [11]: dev.logfile
Out[11]: '/tmp/atlrs21-cli-20191021T024126194.log'

I have not experimented with changing the logfile value.

 

Hope this helps!

My bad. I had checked and thought I did not see a log file, but I checked again and indeed it was there. Thanks for the help.

@Jeremy Schulman 

I know this is a blast from the past but would you be willing to take a walk down memory lane and walk me through how you found this?  I've spent hours trying it myself.  I think my hold up is I can't even find the connect method that the object of class genie.libs.conf.device.nxos.device.Device can run to inspect the code.  I looked into all the inherited classes but didn't find a connect method.  Using the inspect module it looks like it my be written in C and maybe thats why I can't see the code.  How did you find out the exact syntax to use?

I'm using this code in ipython to test

from genie import testbed
tb = testbed.load(".\testbed.yaml")
core1 = tb.devices.Core1
core1.connect()

nevermind, I completely missed the post where you got that straight from a dev

stumbled across the documentation for the connect method.  It uses another cisco framework call Unicon.  On their docs it lists out the options you can use for pyats

https://developer.cisco.com/docs/unicon/

Here's a direct link to the Unicon device connection arguments.

https://pubhub.devnetcloud.com/media/unicon/docs/user_guide/connection.html#python-apis