Skip to content
knipknap edited this page Jan 28, 2011 · 20 revisions

Automating Telnet and SSH

Exscript is a Python module and a template processor for automating network connections over protocols such as Telnet or SSH. We attempt to create the best possible set of tools for working with Telnet and SSH.

Introduction

Here’s how to use Exscript as a simple replacement for Perl’s Net::Telnet:

from Exscript.util.interact import read_login
from Exscript.protocols import SSH2

account = read_login()  # ask for username and password
conn = SSH2()
conn.connect('localhost')
conn.login(account)
conn.execute('ls -l')
conn.send('exit\r')
conn.close()

The same thing for Telnet? Just replace “SSH2” by Telnet, no other change is needed!

Too verbose? Here is a shorter version:

from Exscript.util.start import quickstart
def start(conn):
    conn.execute('ls -l')
quickstart('ssh://localhost', start)

Want to run the same script on two hosts in parallel? Try the following magic:

from Exscript.util.start import quickstart
def start(conn):
    conn.execute('ls -l')
quickstart(['ssh://localhost', 'telnet://anotherhost'], start, max_threads = 2)

More Information

Exscript can do a lot more, with support for logging, reporting, templates, emulators, config file handling, and many more tools for sysadmins. The Python module even comes with an integrated Telnet server and SSH2 server, so you can easily test your Telnet or SSH script against an emulated device! Here are some starting points for more info:

Command Line Tool and Template Language

The Exscript Template Language

The Exscript template language is in some ways comparable to Expect, but has unique features that make it a lot easier to use and understand for non-developers.

{fail "not a Cisco router" if connection.guess_os() is not "ios"}

show ip interface brief {extract /^(\S+)\s/ as interfaces}
configure terminal
{loop interfaces as interface}
  interface $interface
   description This is an automatically configured interface description!
   cdp enable
   no shut
  exit
{end}
copy running-config startup-config

To get going quickly with templates, have a look at A Short Overview For The Impatient. You may also look at our Template Examples.

Clone this wiki locally