Is there an Expect equivalent gem for Ruby?

Ruby comes with the PTY module for setting up pseudoterminals to drive interactive command line applications. With it comes an expect method that allows you to interact with an application kinda like Expect. For learning how to use expect, I found “What to expect from the Ruby expect library?” helpful. As far as gems go, … Read more

How can I make an expect script prompt for a password?

Use expect’s stty command like this: # grab the password stty -echo send_user — “Password for $user@$host: ” expect_user -re “(.*)\n” send_user “\n” stty echo set pass $expect_out(1,string) #… later send — “$pass\r” Note that it’s important to call stty -echo before calling send_user — I’m not sure exactly why: I think it’s a timing … Read more

Using conditional statements inside ‘expect’

Have to recomment the Exploring Expect book for all expect programmers — invaluable. I’ve rewritten your code: (untested) proc login {user pass} { expect “login:” send “$user\r” expect “password:” send “$pass\r” } set username spongebob set passwords {squarepants rhombuspants} set index 0 spawn telnet 192.168.40.100 login $username [lindex $passwords $index] expect { “login incorrect” { … Read more

Use Expect in a Bash script to provide a password to an SSH command

Mixing Bash and Expect is not a good way to achieve the desired effect. I’d try to use only Expect: #!/usr/bin/expect eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com # Use the correct prompt set prompt “:|#|\\\$” interact -o -nobuffer -re $prompt return send “my_password\r” interact -o -nobuffer -re $prompt return send “my_command1\r” interact -o -nobuffer -re … Read more