# Telnet question

**URL:** <https://www.ruby-forum.com/t/telnet-question/69940>\
**Category:** Ruby\
**Created:** [February 18, 2006, 2:44am UTC](https://www.ruby-forum.com/t/telnet-question/69940 "2006-02-18T02:44:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 18, 2006, 2:44am UTC](https://www.ruby-forum.com/t/telnet-question/69940/1 "2006-02-18T02:44:39Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 18, 2006, 5:06am UTC](https://www.ruby-forum.com/t/telnet-question/69940/2 "2006-02-18T05:06:39Z")

</div>

Look at ‘expect’, it’s the standard tool to handle the dialog  
interaction.

[http://rubyforge.org/projects/rexpect/](http://rubyforge.org/projects/rexpect/)

See page 676 in ‘Programming Ruby’

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 18, 2006, 5:12am UTC](https://www.ruby-forum.com/t/telnet-question/69940/3 "2006-02-18T05:12:41Z")

</div>

On 2/17/06, Sammyo [removed\_email\_address@domain.invalid](mailto: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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 22, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/telnet-question/69940/4 "2006-02-22T19:54:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 22, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/telnet-question/69940/5 "2006-02-22T19:54:09Z")

</div>

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
