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

···

--
Posted via http://www.ruby-forum.com/\.

...

       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

···

--
Posted via http://www.ruby-forum.com/.

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

Cheers

···

On 29 June 2012 21:10, Tim L. <lists@ruby-forum.com> wrote:

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

--
Posted via http://www.ruby-forum.com/\.

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

···

On Fri, Jun 29, 2012 at 8:10 PM, ZipiZap zap <zipizap123@gmail.com> wrote:

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

https://github.com/net-ssh/net-ssh

Cheers

On 29 June 2012 21:10, Tim L. <lists@ruby-forum.com> wrote:

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

--
Posted via http://www.ruby-forum.com/\.