Expect with Ruby - a training example to get you started
Archive - Originally posted on "The Horse's Mouth" - 2016-05-18 20:03:54 - Graham EllisThe 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].