views:

42

answers:

1

Hi all ,

I am using Net/TELNET to connect to remote host trying to run su command in order to get root privilage. This is how I am doing it

require 'net/telnet'

localhost = Net::Telnet::new("Host" => "192.147.217.27",
                             "Timeout" => 50,
                             "Prompt" => /[$%#>] \z/n)
localhost.login("dvsdkrp", "dvsdkvrp") { |c| print c }
localhost.cmd("cd /home/dvsdkrp/workdir/smruti") { |c| print c }
localhost.cmd("su") { |c| print c }
localhost.puts("passwd"){ |c| print c }

I am able to login and able to do smruti directory but when I use su command it takes me to the password prompt but then I get this error, even after I changed the Timeout parameter to 150

Password: C:/Ruby/lib/ruby/1.8/net/telnet.rb:552:in `waitfor': timed out
while waiting for more data (Timeout::Error)
        from C:/Ruby/lib/ruby/1.8/net/telnet.rb:679:in `cmd'
        from tel.rb:7

What should I do?

A: 

The prompt you specify when you create the Telnet object is the one its going to look for before it runs its next command. If you are timing out then I think its because its not seeing the prompt it expects. If you use the Match option when you send the 'su' command, you can specify a prompt specifically for this command.

su_prompt = "Password: "
localhost.cmd("String" => "su", "Match" => /#{su_prompt}/) { |c| print c }
localhost.cmd("passwd")

This new prompt is only used for the current command. The originally specified prompt is still in affect for the rest of the session.

D-D-Doug
But D in which line i shoud provide password
AMIT
after the 'Match' line is executed, the telnet session will wait for input after it sees the temporary prompt. At this point you can send it your password. (3rd line in the code sample)
D-D-Doug
undefined local variable or method `su' for main:Object (NameError)
AMIT
D am getting this error now when trying to execute Match line
AMIT
Thanks D Thanks you made it work...
AMIT
You genius ,but could you please Explain what was the problem before i still did n't get what was the problem before
AMIT