Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

cannon

Project description

Introduction

cannon is a wrapper around pexpect to connect with remote server or network devices with ssh.

Example Usage - Cisco IOS

from cannon import Shell, Account

sess = Shell(
    host='route-views.oregon-ix.net',
    # route-views doesn't need password
    credentials=(
        Account(user='rviews', passwd=''),
    ),

    log_screen=True,
    log_file="~/mylog.txt",
    debug=False,
    )

sess.execute('term len 0')

# relax_prompt reduces prompt matching to a minimum... relax_prompt is
#     useful if the prompt may change while running a series of commands.
sess.execute('show clock', relax_prompt=True)

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, getuser
import sys
import re

from cannon import Account, Shell

"""Test for auth fallback, public-key authorization and linux sudo"""

print("Test logging into linux with ssh...")
# Test password fallback and ssh_key authorization...
acct01 = Account(getuser(), passwd, ssh_key='~/.ssh/id_rsa')

conn = Shell('localhost', credentials=(acct01,),
    mode="linux", debug=False, log_screen=True,
    strip_colors=True)

conn.execute('uname -a', timeout=5)

########### sudo ##########################################################
#
index = conn.execute('sudo uname -a', prompts=(r"assword.+?:",))
print("INDEX", index)
conn.execute(passwd)

conn.execute('sudo ls /tmp')
conn.execute('ls')

# FIXME, interact() is broken...
#conn.interact()

conn.execute('whoami', command_timeout=5)
print("WHOAMI RESULT QUOTED '{}'".format(conn.response))
conn.execute('uptime', command_timeout=5)
print("UPTIME '{}'".format(conn.response))
conn.close()

Project details


Download files

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

Files for cannon, version 0.0.37
Filename, size File type Python version Upload date Hashes
Filename, size cannon-0.0.37-py3-none-any.whl (16.6 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size cannon-0.0.37.tar.gz (28.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page