Forum: Ruby Telnet question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
(Guest)
on 2006-02-18 03:44
(Received via mailing list)
I'll ask the questions first, and then show the code and examples
below:

waitfor is timing the script out. Is it because it isn't recognizing
that password input is complete? or is it because the device doesn't
issue a command prompt after the password is input? (The user has to
hit the Enter key to get the command prompt)?

I've interjected comments on the behavior of the script with <ROLFE-...
below.

I'm running the following script to log on to a peice of hardware:

   require 'net/telnet'

   tn = Net::Telnet.new('Host'       => '192.168.47.150')     { |str|
print str }
   tn.login("myusername", "mypassword")  { |str| print str }

This is how script displays in FreeRIDE:

    >ruby c:/Documents and Settings/rdlugyhegwer/sampler.rb
    Trying 192.168.47.150...
    Connected to 192.168.47.150.
    WELCOME TO SYMMETRICOM NETWORK INTERFACE!
    USER NAME: myusername

<ROLFE-It pauses here for a little while, and then continues with:>

    PASSWORD: mypassword
    NETWORK INTERFACE 192-8001      (c) 1998 - 2006 SYMMETRICOM
    ALL RIGHTS RESERVED
    LOGIN SUCCESSFUL!

    c:/ruby/lib/ruby/1.8/net/telnet.rb:551:in `waitfor': timed out
while waiting for more data (Timeout::Error)
	from c:/ruby/lib/ruby/1.8/net/telnet.rb:678:in `cmd'
	from c:/ruby/lib/ruby/1.8/net/telnet.rb:725:in `login'
	from c:/Documents and Settings/rdlugyhegwer/sampler.rb:4
    >exit


If I log in using a terminal program, it looks like this:

    WELCOME TO SYMMETRICOM NETWORK INTERFACE!
    USER NAME: myusername
    PASSWORD: *****
    NETWORK INTERFACE 192-8001      (c) 1998 - 2006 SYMMETRICOM
    ALL RIGHTS RESERVED
    LOGIN SUCCESSFUL!

<ROLFE - the user must hit the ENTER key on this blank line to get the
command prompt>
    >quit

    GOODBYE.

Any suggestions? I've read a lot of the documentation online and tried
variations on the telnet.rb sourcecode. Your help would be very much
appreciated...
Sammyo (Guest)
on 2006-02-18 06:06
(Received via mailing list)
Look at 'expect', it's the standard tool to handle the dialog
interaction.

http://rubyforge.org/projects/rexpect/

See page 676 in 'Programming Ruby'
William R. (Guest)
on 2006-02-18 06:12
(Received via mailing list)
On 2/17/06, Sammyo <removed_email_address@domain.invalid> wrote:
>
> Look at 'expect', it's the standard tool to handle the dialog
> interaction.
>
>

Net::Telnet has a similiar command: waitfor

It may be that you have to use write and waitfor to accomplish logins on
the
system you're using. I was doing some work with some cisco devices a
while
back and I had to kludge my way past a few things with those commands.
(Guest)
on 2006-02-22 20:54
(Received via mailing list)
After even more tinkering with waitfor and other methods, I ultimately
chose the following BFI approach (which worked!):

require 'net/telnet'
require 'time'

t = Time.now
this_time = t.strftime("%y_%m_%d_%H_%M_%S")


tn = Net::Telnet.new( 'Host' => '192.168.47.150',
                      "Output_log" => "./logs/" + this_time + ".txt")
  { |str| print str }
sleep 1
tn.puts("operator")  { |str| print str }
sleep 1
tn.puts('janus') { |str| print str }

#Skip line with no prompt that appears after logging in
tn.puts ''

#Time zone offset
tn.puts("F1")
sleep 1
(Guest)
on 2006-02-22 20:54
(Received via mailing list)
As a relative newbie, I was frustrated by the lack of good
documentation/comments for RExpect. It seems from other postings to
this list that RExpect doesn't play well with Windows. I decided to use
a different approach explained in a separate reply to this thread.

require 'net/telnet'
require 'time'

t = Time.now
this_time = t.strftime("%y_%m_%d_%H_%M_%S")


tn = Net::Telnet.new( 'Host' => '192.168.47.150',
                      "Output_log" => "./logs/" + this_time + ".txt")
  { |str| print str }
sleep 1
tn.puts("operator")  { |str| print str }
sleep 1
tn.puts('janus') { |str| print str }

#Skip line with no prompt that appears after logging in
tn.puts ''

#Time zone offset
tn.puts("F1")
sleep 1
This topic is locked and can not be replied to.