Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Expect with Ruby - a training example to get you started

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)


Back to
Swindon, Chippenham and Melksham - day trips by train to Weymouth start for 2016
Previous and next
or
Horse's mouth home
Forward to
Reading in XML in Ruby with xmlsimple
Some other Articles
One line scripts - Awk, Perl and Ruby
Ruby testing with RSpec - a new example
Processing data line by line - iterator in Ruby with yield
Reading in XML in Ruby with xmlsimple
Expect with Ruby - a training example to get you started
Swindon, Chippenham and Melksham - day trips by train to Weymouth start for 2016
Exceptions in Ruby - throwing, catching and using
Alternating valuses / flip-flop / toggle - example in Ruby
Separating detailed data code from the main application - Ruby example
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4678_Exp ... arted.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb