Scrapli Channel Log

A quick post to showcase a feature that should have been in scrapli from the beginning… but wasn’t… the “channel log”! If you’ve used scrapli you probably have seen that there is pretty robust logging going on. Logs are very helpful for troubleshooting and are maybe even nice to have just for historical reasons so you know what happened when. Logs are not very helpful, however, if you just want to see what is happening from the perspective of a user – what would this look like if this was just happening in a terminal. Clearly not having a feature to easily get this output was a pretty big miss in scrapli, but, now it exists!

The channel_log argument of the base device class accepts a string, a bool, or a BytesIO object. If you pass a True, scrapli will automatically log to a file called “scrapli_channel.log” in your current directory… a nice option if you’re feeling lazy and just need the log output.

If you’re dealing with multiple devices, or just want to have the channel log sent somewhere else or with a custom name, you can pass a string path to a file to log to.

Lastly, and perhaps interestingly to some folks, you can pass a BytesIO object – scrapli will simply log into this object. This is really handy if you want to have a picture of what happened in the channel but don’t want to or aren’t able to write out the log file for some reason. For me, this is a really valuable for capturing channel output and dumping it into a final result object that gets sent off to a message queue or written out to S3 or something like that.

Here is a simple example of how to get going with the channel log option:

from scrapli import Scrapli

device = {
    "host": "c3560",
    "port": 22,
    "ssh_config_file": True,
    "channel_log": True,
    "platform": "cisco_iosxe"
}

conn = Scrapli(**device)
conn.open()
conn.send_command(command="show version")
conn.close()

And we can see our channel log output looks exactly like if you were connecting to the device “normally”:

Warning: Permanently added 'c3560,172.31.254.1' (RSA) to the list of known hosts.

C3560CX#
C3560CX#terminal length 0
C3560CX#terminal width 512
C3560CX#show version
Cisco IOS Software, C3560CX Software (C3560CX-UNIVERSALK9-M), Version 15.2(4)E7, RELEASE SOFTWARE (fc2)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2018 by Cisco Systems, Inc.
Compiled Tue 18-Sep-18 13:20 by prod_rel_team

ROM: Bootstrap program is C3560CX boot loader
BOOTLDR: C3560CX Boot Loader (C3560CX-HBOOT-M) Version 15.2(4r)E5, RELEASE SOFTWARE (fc4)

C3560CX uptime is 2 days, 6 hours, 49 minutes
System returned to ROM by power-on
System restarted at 03:48:27 PST Sat Feb 13 2021
System image file is "flash:c3560cx-universalk9-mz.152-4.E7.bin"
Last reload reason: power-on



This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.
Importers, exporters, distributors and users are responsible for
compliance with U.S. and local country laws. By using this product you
agree to comply with applicable laws and regulations. If you are unable
to comply with U.S. and local laws, return this product immediately.

A summary of U.S. laws governing Cisco cryptographic products may be found at:
http://www.cisco.com/wwl/export/crypto/tool/stqrg.html

If you require further assistance please contact us by sending email to
export@cisco.com.

License Level: ipservices
License Type: Permanent Right-To-Use
Next reload license Level: ipservices

cisco WS-C3560CX-8PC-S (APM86XXX) processor (revision A0) with 524288K bytes of memory.
Processor board ID FOC1911Y0NH
Last reset from power-on
3 Virtual Ethernet interfaces
12 Gigabit Ethernet interfaces
The password-recovery mechanism is enabled.

512K bytes of flash-simulated non-volatile configuration memory.
Base ethernet MAC Address       : C8:00:84:B2:E9:80
Motherboard assembly number     : 73-16471-04
Power supply part number        : 341-0675-01
Motherboard serial number       : FOC190608U7
Power supply serial number      : DCB190430Z0
Model revision number           : A0
Motherboard revision number     : A0
Model number                    : WS-C3560CX-8PC-S
System serial number            : FOC1911Y0NH
Top Assembly Part Number        : 68-5359-01
Top Assembly Revision Number    : A0
Version ID                      : V01
CLEI Code Number                : CMM1400DRA
Hardware Board Revision Number  : 0x02


Switch Ports Model                     SW Version            SW Image                 
------ ----- -----                     ----------            ----------               
*    1 12    WS-C3560CX-8PC-S          15.2(4)E7             C3560CX-UNIVERSALK9-M    


Configuration register is 0xF

C3560CX#
C3560CX#

Hopefully this will come in handy for folks!