Package: paramiko-expect
Artwork courtesy ofOpen Clip Art
LibraryIntroductionParamiko 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 StartTo install paramiko-expect, simply run the following at your prompt:# from pypipipinstallparamiko-expect# from sourcepipinstallgit+https://github.com/fgimian/paramiko-expect.gitSo let’s check out how it works in general (please seeparamiko_expect-demo.pyfor the complete code):# Connect to the hostclient.connect(hostname=hostname,username=username,password=password)# Create a client interaction class which will interact with the hostinteract=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 matchedinteract.send('~/paramiko_expect-demo-helper.py')interact.expect([prompt,'Please enter your name: '])ifinteract.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: '])iffound_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 commandprint'-'*79print'Cleaned Command Output'print'-'*79print'uname -a output:'printcmd_output_unameprint'ls -l / output:'printcmd_output_lsImportant: Before running this script, be sure to placeparamiko_expect-demo-helper.pyin~.The print statements at the bottom of the script provide the following
output:-------------------------------------------------------------------------------CleanedCommandOutput-------------------------------------------------------------------------------uname-aoutput:Linuxfotsies-ubuntu-testlab3.2.0-23-generic#36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linuxls-l/output:total77drwxr-xr-x2rootroot4096May122:21bindrwxr-xr-x4rootroot1024May122:22bootdrwxr-xr-x15rootroot4300Jun1215:00devdrwxr-xr-x90rootroot4096Jun1216:45etcdrwxr-xr-x4rootroot4096May123:37homelrwxrwxrwx1rootroot33May122:18initrd.img->/boot/initrd.img-3.2.0-23-genericdrwxr-xr-x18rootroot4096May122:21libdrwxr-xr-x2rootroot4096May122:17lib64drwx------2rootroot16384May122:17lost+founddrwxr-xr-x4rootroot4096May122:18mediadrwxr-xr-x2rootroot4096Apr1919:32mntdrwxr-xr-x2rootroot4096May122:17optdr-xr-xr-x84rootroot0Jun1215:00procdrwx------3rootroot4096May3023:32rootdrwxr-xr-x15rootroot560Jun1217:02rundrwxr-xr-x2rootroot4096Jun420:59sbindrwxr-xr-x2rootroot4096Mar604:54selinuxdrwxr-xr-x2rootroot4096May122:17srvdrwxr-xr-x13rootroot0Jun1215:00sysdrwxrwxrwt2rootroot4096Jun1216:17tmpdrwxr-xr-x10rootroot4096May122:17usrdrwxr-xr-x12rootroot4096Jun1213:16varlrwxrwxrwx1rootroot29May122:18vmlinuz->boot/vmlinuz-3.2.0-23-genericFor interacting with tail-like scripts, we can use the tail function (please seeparamiko_expect-tail-demo.pyfor the complete code):# Connect to the hostclient.connect(hostname=hostname,username=username,password=password)# Create a client interaction class which will interact with the hostinteract=SSHClientInteraction(client,timeout=10,display=False)interact.expect(prompt)# Send the tail commandinteract.send('tail -f /var/log/auth.log')# Now let the class tail the file for usinteract.tail(line_prefix=hostname+': ')The true power of the tail function will become more apparent when you
check out theMulti-SSHlibrary. Ever thought about tailing a log on multiple servers? Well
dream no more my friend, it’s here!TestsNot full coverage yet, and assumes you have docker setup:pipinstall-rrequirements-test.txtdockerrun-d-p2222:22-v`pwd`/examples:/examples-v`pwd`/test/id_rsa.pub:/root/.ssh/authorized_keysdocker.io/panubo/sshdpytest-s--covparamiko_expect--cov-reportterm-missingContributionsIsrael Fruchter (@fruch) - Tests / CI / Uploads to PypiKiseok Kim (@kiseok7) - Vagrent imageLicenseParamiko Expect is released under theMITlicense. Please see theLICENSEfile for more details.