Skip to main content

An SSH automation tool based on Exscript

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cannon-0.0.67.tar.gz (25.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cannon-0.0.67-py3-none-any.whl (30.7 kB view details)

Uploaded Python 3

File details

Details for the file cannon-0.0.67.tar.gz.

File metadata

  • Download URL: cannon-0.0.67.tar.gz
  • Upload date:
  • Size: 25.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.7.0 Linux/5.10.0-9-amd64

File hashes

Hashes for cannon-0.0.67.tar.gz
Algorithm Hash digest
SHA256 3fcf5d7291e2a3dbcbb481b3cc15a5daaec801c6849a024f1f1a71c96e89a7c2
MD5 a4c1096e9b0c3a5181c1c823a694fe7d
BLAKE2b-256 a8f69a083d003c21d103e93441b40da4b0f7bd9b884cd9251d4205dbac653764

See more details on using hashes here.

File details

Details for the file cannon-0.0.67-py3-none-any.whl.

File metadata

  • Download URL: cannon-0.0.67-py3-none-any.whl
  • Upload date:
  • Size: 30.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.7.0 Linux/5.10.0-9-amd64

File hashes

Hashes for cannon-0.0.67-py3-none-any.whl
Algorithm Hash digest
SHA256 2fce14bb9102d2f91f72d50c32034a31204ffc4b750037c16536902831083f0a
MD5 21a625e6acdd5a8582c837af6db084ae
BLAKE2b-256 3ec86411e890e926098c7055e630c50e98f9300b8518b0d55d267ff858587f1d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page