Ruby pty and expect

I’m trying to write a program that will log into a list of ip’s and run
a command. I running into an issue with expect. When I get logged in I
want to run the command, but some machines the password doesn’t work.
When this happens it returns the password prompt again, I want to kill
the process when this happens and move to the next ip.
Her is the code I have but doesn’t work correctly. Any help will be
appreciated.

PTY.spawn(“ssh -o StrictHostKeyChecking=no user@#{address}”) do
|reader, writer, pid|
reader.expect(“Password:”)
writer.puts(“my_password”)
reader.expect(/$|Password/) do |a, b|
if a.match("$")
writer.puts(“ifconfig”)
reader.expect("$")
else
Process.kill(9, pid)
end
end
end

 reader.expect(/$|Password/) do |a, b|

You do remember that the $ character is special in regular
expressions… Try escaping it.

Aaron out.

That was it. thanks a million

PTY.spawn(“ssh -o StrictHostKeyChecking=no user@#{address}”) do |reader,
writer, pid|
reader.expect(/Password/)
writer.puts(“my_password”)
reader.expect(/$|Password/) do |a, b|
if a.match(/$/)
writer.puts(“ifconfig”)
reader.expect(/$/)
writer.puts(“exit”)
reader.expect(/$/)
else
Process.kill(9, pid)
end

        end

Also if any time later you need more from ruby+ssh, have a look at gem
net-ssh

Cheers

I agree with the regex comment and also I have had an easier time /
better
luck using net-ssh for these type of actions.

Use ssh -oNumberOfPasswordPrompts=1