Telnet and 'waitfor'

Hi,

by reading the man i understood that ‘waitfor’ method could be used like
this :

waitfor(String => ‘Name’, Timeout => 30)

But, with that code… :

telnet_session = Net::Telnet.new(‘Host’ => @apc_ip)

@apc_ip is the ip of the host chosen

telnet_session.waitfor(String => ‘User Name’, Timeout => 30)

…I have a timeout. Is this a syntax problem ?

On 9/7/06, David C. [email protected] wrote:

telnet_session = Net::Telnet.new(‘Host’ => @apc_ip)

@apc_ip is the ip of the host chosen

telnet_session.waitfor(String => ‘User Name’, Timeout => 30)

…I have a timeout. Is this a syntax problem ?

Seems like you have to quote your key names, i.e. ‘String’, “Timeout”.

Jan S. wrote:

Seems like you have to quote your key names, i.e. ‘String’, “Timeout”.

Yes that’s right, i made a mistake by writting it on the forum. I’d try
with waitfor(buffer), and my aString buffer is never recognized.

On 9/7/06, David C. [email protected] wrote:

telnet_session = Net::Telnet.new(‘Host’ => @apc_ip)

@apc_ip is the ip of the host chosen

telnet_session.waitfor(String => ‘User Name’, Timeout => 30)

…I have a timeout. Is this a syntax problem ?

Just curious, but are you trying to connect to an APC Power
Distribution Unit? Your choice of variable names – @apc_ip – just
made me think of the APC PDUs we have here at work.

Your telnet code looks correct except for the quoting, but I think you
know that already.

Try setting ‘Output_log’ to a filename when you create your telnet
session. This will copy all telnet traffic to the given log file. It
might be that what you are receiving is not what you are expecting

telnet_session = Net::Telnet.new(‘Host’ => @apc_ip, ‘Output_log’ =>
‘my_log.txt’)

Blessings,
TwP

“D” == David C. [email protected] writes:

D> telnet_session.login(‘LoginPrompt’ => ‘User Name’,

telnet.rb use String#===, in your case, with LoginPrompt

moulon% ruby -e ‘p “User Name” === “User Name :”’
false
moulon%

moulon% ruby -e ‘p “User Name :” === “User Name :”’
true
moulon%

try to give it a regexp

D> ‘PasswordPrompt’ => ‘Password’,

Guy Decoux

David C. wrote:

I obtained in the log :
<<
Trying 3.249.13.24…
Connected to 3.249.13.24.

User Name :

So, ‘User Name’ is what I wait for. But it crashes.

Could it be that it is waiting for a new line character before matching?
Maybe using ‘Prompt’ instead of ‘String’ will work?

Also, it says that it converts the string to a regex anyhow, so why not
try setting Match and trying to get it to work that way. Maybe it’s
trying to make it match the whole line or something when you use
‘String’.

Just thoughts… I haven’t tried to use this yet, and I can’t right this
moment, unfortunately.

On 9/7/06, David C. [email protected] wrote:

Tim P. wrote:

Just curious, but are you trying to connect to an APC Power
Distribution Unit? Your choice of variable names – @apc_ip – just
made me think of the APC PDUs we have here at work.

Yes I work with APC PDU. :wink:

So, I wrote up some code to automate the rebooting / turning on /
turning off of various outlets in the PDU. This can be done by outlet
name (assiged through the web interface) or by outlet number.

Attached is a gem file that contains all the code. It also has a
command lien client that lets you do things like …

apc -a reboot outlet_name
apc -a turn_off outlet_name

where the “-a” specifies the action and “outlet_name” is the alias for
the outlet you want to control. The authentication credentials for
the outlet are stored in a file, so you only have to enter them once.
Also, all other outlets in the PDU are automatically discovered and
stored.

Try it out and let me know if it works for you. I should really
release this throug RubyForge, but I’m just too lazy to setup a
project right now :confused:

TwP

Tim P. wrote:

Just curious, but are you trying to connect to an APC Power
Distribution Unit? Your choice of variable names – @apc_ip – just
made me think of the APC PDUs we have here at work.

Yes I work with APC PDU. :wink:

Try setting ‘Output_log’ to a filename when you create your telnet
session. This will copy all telnet traffic to the given log file.

Ok, so i tried with function made to that : ‘login’. Here is my code :

###################Start of code

require ‘net/telnet’

class Host

def initialize(host_ip, apc_ip, slot_number)
@host_ip = host_ip
@apc_ip = apc_ip
@slot_number = slot_number
end

def reboot
telnet_session = Net::Telnet.new(‘Host’ => @apc_ip, ‘Output_log’ =>
‘my_log.txt’)
telnet_session.login(‘LoginPrompt’ => ‘User Name’,
‘PasswordPrompt’ => ‘Password’,
‘Name’ => ‘cortdav’,
‘Password’ => ‘cortdav’)

end

end

Dione = Host.new(“3.249.13.36”, “3.249.13.24”, 2)

Dione.reboot

###################End of code

I obtained in the log :
<<
Trying 3.249.13.24…
Connected to 3.249.13.24.

User Name :

So, ‘User Name’ is what I wait for. But it crashes.
It works if I write :

telnet_session.login('LoginPrompt'    => '',
                     'PasswordPrompt' => '',
                     'Name'           => 'cortdav',
                     'Password'       => 'cortdav')

But the day I’ll need to wait for an expression, I will have the same
problem, so I need to find the solution now. Reading and Reading the
docs, and I don’t find the problem…

Thanks everybody for your help.

One by one :

Guy, I didn’t understand what you mean, sorry.

William, if it is waiting for a new line character, there is a real
problem with waitfor. In the doc they don’t say that.

Tim, You’re right i can do it without Ruby, but my work isn’t to reboot
only so I want to do that with Ruby for learning how to do.

Paul, you’re code can’t work with me because you use :

session.login(“username”,“password”)

‘login’, by default wait for the prompt “login”. But my prompt is “User
Name”, So that doesn’t work.

For the regex i tried /^User Name$/ but that doesn’t work too, do you
have another idea ?

David C. wrote:

For the regex i tried /^User Name$/ but that doesn’t work too, do you
have another idea ?

Ok thanks everyone : /^User Name$/ couldn’t work because of the ‘:’.
/User Name/ work !!!

Thanks a lot all your helps was precious.

David C. wrote:

session. This will copy all telnet traffic to the given log file.

Ok, so i tried with function made to that : ‘login’. Here is my code :

Try something like this:


#! /usr/bin/ruby

require ‘net/telnet’

session = Net::Telnet.new(
“Host” => “hostname”,
“Timeout” => 10,
“Prompt” => /[$%#>] \z/n
)

session.login(“username”,“password”)

session.cmd(“ls -la”) { |c| print c }

session.close