Skip to main content

An SSH automation tool based on Exscript

Project description

Introduction

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

Example Usage - Cisco IOS

from cannon import Shell, Account

sess = Shell(
    host='route-views.routeviews.org',
    # 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...
username = getuser()
acct01 = Account(username, getpass("Password for %s" % username), 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.

Source Distribution

cannon-0.0.38.tar.gz (20.4 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.38-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for cannon-0.0.38.tar.gz
Algorithm Hash digest
SHA256 b0c1718ed5b7c802bd9d0906b1c9bd344eb6186435b6dd4fbbbfc8cc46bd3ee0
MD5 f4f549b88125fa0329335e6e067d30be
BLAKE2b-256 43a25ebab904dda7988a338efffb572b0781639222c5483a7071aa7e21ca6e4d

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for cannon-0.0.38-py3-none-any.whl
Algorithm Hash digest
SHA256 0515c4045951af4a8e98f73b063a10c066ca041259d2f9e7230c2ee911239e7e
MD5 b7cf9aca249fd1208032b138b9a85896
BLAKE2b-256 d24ed645b314e4cde3c4c50a774cb8cf123b8c3fe0f25e825690dc1fe21a9216

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