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
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.
Mikel L. wrote:
On Jan 15, 2008 11:14 PM, Prasad P. [email protected] wrote:
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:
Redirecting…
Regards
Mikel
On Jan 15, 2008 11:14 PM, Prasad P. [email protected] wrote:
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:
Regards
Mikel
Just curious to know if there exists any error if the login fails ???