Skip to main content
https://img.shields.io/pypi/l/paramiko-expect.svg https://coveralls.io/repos/github/fgimian/paramiko-expect/badge.svg?branch=master https://img.shields.io/travis/fgimian/paramiko-expect.svg https://img.shields.io/pypi/v/paramiko-expect.svg https://img.shields.io/pypi/pyversions/paramiko-expect.svg Paramiko Expect Logo

Artwork courtesy of Open Clip Art Library

Introduction

Paramiko Expect provides an expect-like extension for the Paramiko SSH library which allows scripts to fully interact with hosts via a true SSH connection.

The class is constructed with an SSH Client object (this will likely be extended to support a transport in future for more flexibility).

Quick Start

To install paramiko-expect, simply run the following at your prompt:

pip install git+https://github.com/fgimian/paramiko-expect.git

So let’s check out how it works in general (please see paramiko_expect-demo.py for the complete code):

# Connect to the host
client.connect(hostname=hostname, username=username, password=password)

# Create a client interaction class which will interact with the host
interact = SSHClientInteraction(client, timeout=10, display=True)
interact.expect(prompt)

# Run the first command and capture the cleaned output, if you want the output
# without cleaning, simply grab current_output instead.
interact.send('uname -a')
interact.expect(prompt)
cmd_output_uname = interact.current_output_clean

# Now let's do the same for the ls command but also set a timeout for this
# specific expect (overriding the default timeout)
interact.send('ls -l /')
interact.expect(prompt, timeout=5)
cmd_output_ls = interact.current_output_clean

# To expect multiple expressions, just use a list.  You can also selectively
# take action based on what was matched.

# Method 1: You may use the last_match property to find out what was matched
interact.send('~/paramiko_expect-demo-helper.py')
interact.expect([prompt, 'Please enter your name: '])
if interact.last_match == 'Please enter your name: ':
    interact.send('Fotis Gimian')
    interact.expect(prompt)

# Method 2: You may use the matched index to determine the last match (like pexpect)
interact.send('~/paramiko_expect-demo-helper.py')
found_index = interact.expect([prompt, 'Please enter your name: '])
if found_index == 1:
    interact.send('Fotis Gimian')
    interact.expect(prompt)

# Send the exit command and expect EOF (a closed session)
interact.send('exit')
interact.expect()

# Print the output of each command
print '-'*79
print 'Cleaned Command Output'
print '-'*79
print 'uname -a output:'
print cmd_output_uname
print 'ls -l / output:'
print cmd_output_ls

Important: Before running this script, be sure to place paramiko_expect-demo-helper.py in ~.

The print statements at the bottom of the script provide the following output:

-------------------------------------------------------------------------------
Cleaned Command Output
-------------------------------------------------------------------------------
uname -a output:
Linux fotsies-ubuntu-testlab 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

ls -l / output:
total 77
drwxr-xr-x  2 root root  4096 May  1 22:21 bin
drwxr-xr-x  4 root root  1024 May  1 22:22 boot
drwxr-xr-x 15 root root  4300 Jun 12 15:00 dev
drwxr-xr-x 90 root root  4096 Jun 12 16:45 etc
drwxr-xr-x  4 root root  4096 May  1 23:37 home
lrwxrwxrwx  1 root root    33 May  1 22:18 initrd.img -> /boot/initrd.img-3.2.0-23-generic
drwxr-xr-x 18 root root  4096 May  1 22:21 lib
drwxr-xr-x  2 root root  4096 May  1 22:17 lib64
drwx------  2 root root 16384 May  1 22:17 lost+found
drwxr-xr-x  4 root root  4096 May  1 22:18 media
drwxr-xr-x  2 root root  4096 Apr 19 19:32 mnt
drwxr-xr-x  2 root root  4096 May  1 22:17 opt
dr-xr-xr-x 84 root root     0 Jun 12 15:00 proc
drwx------  3 root root  4096 May 30 23:32 root
drwxr-xr-x 15 root root   560 Jun 12 17:02 run
drwxr-xr-x  2 root root  4096 Jun  4 20:59 sbin
drwxr-xr-x  2 root root  4096 Mar  6 04:54 selinux
drwxr-xr-x  2 root root  4096 May  1 22:17 srv
drwxr-xr-x 13 root root     0 Jun 12 15:00 sys
drwxrwxrwt  2 root root  4096 Jun 12 16:17 tmp
drwxr-xr-x 10 root root  4096 May  1 22:17 usr
drwxr-xr-x 12 root root  4096 Jun 12 13:16 var
lrwxrwxrwx  1 root root    29 May  1 22:18 vmlinuz -> boot/vmlinuz-3.2.0-23-generic

For interacting with tail-like scripts, we can use the tail function (please see paramiko_expect-tail-demo.py for the complete code):

# Connect to the host
client.connect(hostname=hostname, username=username, password=password)

# Create a client interaction class which will interact with the host
interact = SSHClientInteraction(client, timeout=10, display=False)
interact.expect(prompt)

# Send the tail command
interact.send('tail -f /var/log/auth.log')

# Now let the class tail the file for us
interact.tail(line_prefix=hostname+': ')

The true power of the tail function will become more apparent when you check out the Multi-SSH library. Ever thought about tailing a log on multiple servers? Well dream no more my friend, it’s here!

Tests

Not full coverage yet, and assumes you have docker setup:

pip install -r requirements-test.txt
docker run -d -p 2222:22 -v `pwd`/examples:/examples -v `pwd`/test/id_rsa.pub:/root/.ssh/authorized_keys  macropin/sshd
pytest -s --cov paramiko_expect --cov-report term-missing

Contributions

Israel Fruchter (@fruch) - Tests / CI / Uploads to Pypi Kiseok Kim (@kiseok7) - Vagrent image

License

Paramiko Expect is released under the MIT license. Please see the LICENSE file for more details.

Metadata

Release files for paramiko-expect 0.2.5

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

Source distribution (sdist)

Source distribution for paramiko-expect 0.2.5
File Size Uploaded
paramiko-expect-0.2.5.tar.gz 8.1 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for paramiko-expect 0.2.5
File Interpreter ABI Platform
paramiko_expect-0.2.5-py2.py3-none-any.whl Python 3, Python 2 none any Details
paramiko_expect-0.2.5-py2-none-any.whl Python 2 none any Details

Total release size: 29.4 kB

Release files / paramiko-expect-0.2.5.tar.gz

Download URL paramiko-expect-0.2.5.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5b2b4a023c1167a60e5ff5319a7273d7d8fa1ecd17be0d1fc14b1eb3d7954a20
BLAKE2b-256 checksum
How to use checksums
5798349d38e921411b39359a34c7f4f12c71f8398757d0f458181a5b33bad2cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / paramiko_expect-0.2.5-py2.py3-none-any.whl

Download URL paramiko_expect-0.2.5-py2.py3-none-any.whl
Size 10.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e6b4e9dba6717dd046f97cfd965abd83078ee19e67bba572ad5cb52b22d8eafd
BLAKE2b-256 checksum
How to use checksums
bc11bffeb64908d74886123f25f165ce13f502f499b4ebbf0b03d760a6e4eb40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / paramiko_expect-0.2.5-py2-none-any.whl

Download URL paramiko_expect-0.2.5-py2-none-any.whl
Size 10.6 kB
Tags Python 2
SHA-256 checksum
How to use checksums
2c3cfdf885c2ef90db34dbda1d03e10fdf9d3da5d80ffb06a131730c46faa386
BLAKE2b-256 checksum
How to use checksums
dbfd42b5d3809cdd0803db1c7ba33f10d4e78f315e4189c8255ea72a65705707
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.3.5

2 release files

0.3.4

2 release files

0.3.2

2 release files

0.3.0

2 release files

0.2.8

2 release files

0.2.6

2 release files

This release

0.2.5 This release

3 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