How to catch and rescue a login error through telnet

P

Prasad Pednekar

Hi,
I was trying out the following snippet of code and just wanted to
know why the rescue doesn't work when the username or password are not
valid. The code works fine when both are valid, but I wanted to catch
invalid login error and take action, but so far can't do so.

As a beginner in Ruby could you show me a way out ...

require 'net/telnet'

uname = "prasad"
pwd = "prasad123"

begin
tn = Net::Telnet.new({"Host" => "localhost"}) { |str| print
str }

tn.login(uname,pwd) { |str| print str }

rescue
print "The username or password is incorrect. \nPlease try again
...\n\n"

print "\nEnter user name : "
uname = STDIN

print "\nEnter password : "
pwd = STDIN

retry

ensure
tn.cmd("date") { |str| print str }
tn.close()

end
 
M

Mikel Lindsaar

I was trying out the following snippet of code and just wanted to
know why the rescue doesn't work when the username or password are not
valid. The code works fine when both are valid, but I wanted to catch
invalid login error and take action, but so far can't do so.

It's because not all exceptions are created equal :)

Some (most) are inherited from StandardError, which you will catch
with a simple rescue statement, others come from other parent classes.

Here is a short write up on it for you, it talks about Net::pOP3, but
you are probably running into the same thing:

http://lindsaar.net/2007/12/9/rbuf_filltimeout-error

Regards

Mikel
 
P

Prasad Pednekar

Hi Mikel,
I have used TimeoutError with the rescue statement, which works
fine when I hardcore the credentials, but the problem is when I want to
login again by asking the user to enter his credentials, it just jumps
out.

Trying 172.20.25.154...
Connected to 172.20.25.154.
Red Hat Enterprise Linux Server release 5 (Tikanga)
Kernel 2.6.18-PIANO-RHEL5 on an i686
login: prasad
Password:
Login incorrect

login: The username or password is incorrect.
Please try again ...


Enter user name :
Enter password :

User name is => #<IO:0xb7f6cf7c>
Password for #<IO:0xb7f6cf7c> is => #<IO:0xb7f6cf7c>
Trying 172.20.25.154...
Connected to 172.20.25.154.
Red Hat Enterprise Linux Server release 5 (Tikanga)
Kernel 2.6.18-PIANO-RHEL5 on an i686
/usr/lib/ruby/1.8/net/telnet.rb:634:in `puts': undefined method `+' for
#<IO:0xb7f6cf7c> (NoMethodError)
from /usr/lib/ruby/1.8/net/telnet.rb:676:in `cmd'
from /usr/lib/ruby/1.8/net/telnet.rb:722:in `login'
from try.rb:23



Regards,
Prasad.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top