#!/usr/bin/env ruby
require 'net/telnet'
tn = Net::Telnet.new("Host" => "10.0.0.138",
"Timeout" => 5,
"Prompt" => /[$%#>] \z/n
)
tn.login("wybo", "my_password") { |c| print c }
tn.cmd("help") { |c| print c }
tn.close
But it just times out:
$ tn
/usr/local/lib/ruby/1.8/net/telnet.rb:551:in `waitfor': timed out while
waiting for more data (Timeout::Error)
from /usr/local/lib/ruby/1.8/net/telnet.rb:719:in `login'
from ./tn:9
$
What can be wrong with my script?
--
Wybo
#I'm trying to run a telnet session with my adsl modem with
#
##!/usr/bin/env ruby
#
#require 'net/telnet'
#
#tn = Net::Telnet.new("Host" => "10.0.0.138",
# "Timeout" => 5,
# "Prompt" => /[$%#>] \z/n
#)
#tn.login("wybo", "my_password") { |c| print c }
#tn.cmd("help") { |c| print c }
#tn.close
#
#But it just times out:
#
#$ tn
#/usr/local/lib/ruby/1.8/net/telnet.rb:551:in `waitfor': timed
#out while
#waiting for more data (Timeout::Error)
# from /usr/local/lib/ruby/1.8/net/telnet.rb:719:in `login'
# from ./tn:9
#$
#
#What can be wrong with my script?
can you give us the screenshot if you telnet to 10.0.0.138 manually?
thanks ang kind regards -botp
#
#--
#Wybo
#
#Subject: Re: net/telnet session times out
#
#.. here it comes...
I think the telnet#login method is waiting for the text "login" and
therefore cannot catch the "Username" text prompt.
Try using #waitfor...
Eg,
--------------------------------
>cat ttest.rb
# test using waitfor
time_start = Time::new
require 'net/telnet'
host = "10.1.1.1"
tn = Net::Telnet::new( \
"Host" => host, # default: "localhost"
"Port" => 23, # default: 23
"Binmode" => false, # default: false
"Output_log" => "output_log", # default: nil (no output)
"Dump_log" => "dump_log", # default: nil (no output)
"Prompt" => /[$%#>]/, # default: /[$%#>] \z/n
"Telnetmode" => true, # default: true
"Timeout" => 5, # default: 10
"Waittime" => 0 # default: 0
)
tn.waitfor(/ogin:/) {|c| print c}
# ^ in your case,
#------------^ replace this w "tn.waitfor(/username/) {|c| print c}"
#
tn.puts "test"
tn.waitfor(/assword/) {|c| print c}
tn.puts "test's password"
tn.cmd("uname -an") { |c| print c }
tn.cmd("ls -la") { |c| print c }
tn.cmd("exit") {|c| print c}
tn.close
puts
puts "#{Time.now - time_start} seconds"
>ttest.rb
Red Hat Linux release 6.2 (Zoot)
Kernel 2.2.16-3 on an i586
login: test
Password:
uname -an
Last login: Tue Jul 5 22:33:55 from test-pc
You have new mail.
unknown terminal "network"
[]$ uname -an
Linux test.test.testdomain 2.2.16-3 #1 Mon Jun 19 18:10:14 EDT 2000 i586
unknown
[]$ ls -la
total 17
drwx------ 3 botp botp 1024 Jun 6 16:29 .
drwxr-xr-x 13 root root 1024 Mar 8 23:10 ..
-rw------- 1 botp botp 10285 Jul 5 22:35 .bash_history
-rw-r--r-- 1 botp botp 24 Jul 30 2001 .bash_logout
-rw-r--r-- 1 botp botp 230 Jul 30 2001 .bash_profile
-rw-r--r-- 1 botp botp 124 Jul 30 2001 .bashrc
drwxr-xr-x 2 root root 1024 May 20 02:22 scripts
[]$ exit
logout
'network': unknown terminal type.
0.797 seconds
>
------------------------
Is that ok?
kind regards -botp