Skip to main content

Introduction

[cannon][1] is a wrapper around [exscript][2] to connect with remote server or network devices with ssh.

Example Usage - Cisco IOS

This script will login, run a few show commands. If you want an interactive session, set interact=True when calling Shell()

import sys

from cannon import Shell, Account
from loguru import logger

log_stderr_id = logger.add(sink=sys.stderr)

@logger.catch(default=True, onerror=lambda _: sys.exit(1))
def main():
    sess = Shell(
        host='route-views.routeviews.org',
        # route-views doesn't need password
        account= Account(name='rviews', password=''),
        debug=0,
        json_logfile='/tmp/cmd_log.json',
        )

    sess.execute('term len 0')

    sess.execute('show clock')

    sess.execute('show version')
    version_text = sess.response

    # template is a TextFSM template
    values = sess.execute('show ip int brief',
        template="""Value INTF (\S+)\nValue IPADDR (\S+)\nValue STATUS (up|down|administratively down)\nValue PROTO (up|down)\n\nStart\n  ^${INTF}\s+${IPADDR}\s+\w+\s+\w+\s+${STATUS}\s+${PROTO} -> Record""")
    print("VALUES "+str(values))
    sess.close()

Example Usage - Linux

from getpass import getpass
import sys

from cannon.main import Shell, Account

log_stderr_id = logger.add(sink=sys.stderr)

@logger.catch(default=True, onerror=lambda _: sys.exit(1))
def main():
    account = Account("mpenning", getpass("Login password: "))
    conn = Shell(host="127.0.0.1", port=22, account=account, driver="generic", debug=0)
    assert conn is not None
    example_tfsm_template = """Value UNAME_LINE (.+)

Start
  ^${UNAME_LINE}
"""
    print(conn.execute("sudo uname -a", debug=0, template=example_tfsm_template, timeout=2))
    print(conn.execute("whoami", debug=0, template=None, timeout=2))
    #print("FOO2", conn.response)
    conn.close(force=True)

if __name__=="__main__":
    main()

Example test suite setup

  • git clone git@github.com:knipknap/Exscript

  • cd into Exscript/tests/Exscript/protocols and chmod 600 id_rsa

  • exscript spawns a local tests ssh daemon, pytest Exscript/tests/Exscript/protocols/SSH2Test.py

  • Connect with ssh -i id_rsa -p 1236 user@localhost

  • one command is supported: ls

    [1]: https://pypi.python.org/pypi/cannon # cannon on pypi [2]: https://pypi.python.org/pypi/exscript # Exscript on pypi

Release files for cannon 0.0.80

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cannon 0.0.80
File Size Uploaded
cannon-0.0.80.tar.gz 25.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cannon 0.0.80
File Interpreter ABI Platform
cannon-0.0.80-py3-none-any.whl Python 3 none any Details

Total release size: 57.1 kB

Release files / cannon-0.0.80.tar.gz

Download URL cannon-0.0.80.tar.gz
Size 25.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a9edf76a259b86f3583daacc120db17b9bc268a5dacf2ec99ff2c6eaeb0658a3
BLAKE2b-256 checksum
How to use checksums
062be236809c880f4b642df9518675b2488c8ff0defb12e07b7bff4a35f12bc9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.7.0 Linux/5.10.0-9-amd64

Release files / cannon-0.0.80-py3-none-any.whl

Download URL cannon-0.0.80-py3-none-any.whl
Size 31.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8ae84a4ecbbb118e9c0b7e41d38f08a68ab7b8fc15e5f2aefc4811377509a595
BLAKE2b-256 checksum
How to use checksums
08b61490651e4aef3d754dd180da857ad8ba2bd1af5ad7bbf430f64c6bb83177
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.7.0 Linux/5.10.0-9-amd64

Release history Release notifications | RSS feed

This release

0.0.80 This release

2 release files

0.0.79

2 release files

0.0.78

2 release files

0.0.77

2 release files

0.0.76

2 release files

0.0.75

2 release files

0.0.74

2 release files

0.0.73

2 release files

0.0.72

2 release files

0.0.71

2 release files

0.0.70

2 release files

0.0.69

2 release files

0.0.68

2 release files

0.0.67

2 release files

0.0.66

2 release files

0.0.65

2 release files

0.0.64

2 release files

0.0.63

2 release files

0.0.60

2 release files

0.0.59

2 release files

0.0.58

2 release files

0.0.57

2 release files

0.0.56

2 release files

0.0.38

2 release files

0.0.37

2 release files

0.0.36

2 release files

0.0.35

2 release files

0.0.24

2 release files

0.0.20

2 release files

0.0.19

2 release files

0.0.18

2 release files

0.0.17

2 release files

0.0.16

2 release files

0.0.15

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page