The Expect library, originally written for use wth Tcl, is also available in other languages and today I wrote an example to introduce how it's used in Ruby. Please note this is a spike solution - not going to fail (if it does) in a pretty way.
The principle of expect is that you
spawn a process - passing back file handles - and you can then read and write exactly as if you were at the keyboard. Unlike
open3 which works on a line by line basis and can give buffering issues, you can read a prompt up to a specific character or pattern in expect, giving you much more flexibility. Beyond this example, you have timeouts, EOF handling, the ability to wait for alternatives, and much more.
When using expect, I recommend you to run the job by hand so that you have an example of the terminal session you're emulating:
# WomanWithCat:4c grahamellis$ ssh -l wellho www.wellho.net uptime
# wellho@www.wellho.net's password:
# 17:23:14 up 5 days, 2:08, 0 users, load average: 0.30, 0.49, 0.52
# WomanWithCat:4c grahamellis$
You'll require to load the
pty and
expect modules:
require 'pty'
require 'expect'
And here's the automation
uptimedata = []
PTY.spawn("ssh -l wellho www.wellho.net uptime") do |ssh_read,ssh_write,pid|
ssh_read.expect(/sword: /) { |msg| ssh_write.printf("#{ARGV[0]}\n") }
loop do
ssh_read.expect(/\n/) { |line| uptimedata.push line}
break if uptimedata[-1].nil?
p uptimedata[-1]
end
end
And the results:
WomanWithCat:4c grahamellis$ ruby exprub password-here
["\r\n"]
[" 19:22:11 up 5 days, 4:07, 0 users, load average: 0.15, 0.37, 0.45\r\n"]
WomanWithCat:4c grahamellis$
Complete example (which does a ping as well)
[here]. Although we may not cover this on our regular
public ruby course, I would be very happy to cover it in an extra session on the penultimate evening for interested delegates, and also to cover it on private courses.
You'll find more about Expect and its principles under Tcl
[here] and
[here].
(written 2016-05-18)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
T212 - Tcl/Tk - Expect Processes [287] Checking that all our servers are up and accessible - (2005-04-22)
[675] Adding PHP tags to an old cgi program - (2006-04-08)
[1173] Cheat Sheet / Check list for Expect maintainers - (2007-05-02)
[1785] What is running on your network? (tcl and expect) - (2008-09-04)
[2474] Using Tcl and Expect to automate repetitive jobs - (2009-10-24)
[2489] Parallel Pinging, using Python Threads or Expect spawn lists - (2009-11-02)
[3448] Checking all the systems on a subnet, using Expect and Tk - (2011-09-18)
T211 - Tcl/Tk - What is Expect? Why use it? [286] Automating regular manual procedures - (2005-04-21)
[435] Expect for Windows - (2005-09-04)
[1174] Installing Tcl and Expect on Solaris 10 - a checklist - (2007-05-02)
[1409] What is Expect? - (2007-10-26)
[1411] Buffering of inputs to expect, and match order - (2007-10-27)
[1469] Curley brackets v double quotes - Tcl, Tk, Expect - (2007-12-12)
[1531] Expecting a item from a list of possibles - (2008-02-04)
[1602] Automating processes through Expect - (2008-04-05)
[3009] Expect in Perl - a short explanation and a practical example - (2010-10-22)
[3286] Should we cover expect and/or Tk on our public Tcl courses? - (2011-05-11)
[3572] Adding Expect on top of Tcl - what is it and where can I get a training course to learn about it? - (2012-01-08)
[4405] Backup procedures - via backup server - (2015-01-24)
R106 - Input and Output in Ruby [1587] Some Ruby programming examples from our course - (2008-03-21)
[1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
[2290] Opening and reading files - the ruby fundamentals - (2009-07-16)
[2614] Neatly formatting results into a table - (2010-02-01)
[2621] Ruby collections and strings - some new examples - (2010-02-03)
[2893] Exclamation marks and question marks on ruby method names - (2010-07-28)
[2974] Formatting your output - options available in Ruby - (2010-09-29)
[3429] Searching through all the files in or below a directory - Ruby, Tcl, Perl - (2011-09-09)
[4499] Significant work - beyond helloworld in Ruby - (2015-05-27)
R113 - Ruby - Further Input and Output [4008] Reading and checking user inputs - first lessons - Ruby - (2013-02-17)
[4502] Reading and parsing a JSON object in Ruby - (2015-06-01)
[4553] RUby - loading, using, changing, storing JSON format data - (2015-10-23)
[4676] Running shell (operating system) commands from within Ruby - (2016-05-18)
Some other Articles
One line scripts - Awk, Perl and RubyRuby testing with RSpec - a new exampleProcessing data line by line - iterator in Ruby with yieldReading in XML in Ruby with xmlsimpleExpect with Ruby - a training example to get you startedSwindon, Chippenham and Melksham - day trips by train to Weymouth start for 2016Exceptions in Ruby - throwing, catching and usingAlternating valuses / flip-flop / toggle - example in RubySeparating detailed data code from the main application - Ruby example