[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to catch and rescue a login error through telnet

Prasad Pednekar

1/15/2008 12:15:00 PM

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
--
Posted via http://www.ruby-....

3 Answers

Mikel Lindsaar

1/15/2008 12:31:00 PM

0

On Jan 15, 2008 11:14 PM, Prasad Pednekar <prasadm29@gmail.com> 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:

http://lindsaar.net/2007/12/9/rbuf_filltim...

Regards

Mikel

Prasad Pednekar

1/15/2008 1:00:00 PM

0

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 Lindsaar wrote:
> On Jan 15, 2008 11:14 PM, Prasad Pednekar <prasadm29@gmail.com> 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:
>
> http://lindsaar.net/2007/12/9/rbuf_filltim...
>
> Regards
>
> Mikel

--
Posted via http://www.ruby-....

Prasad Pednekar

1/16/2008 5:43:00 AM

0


Just curious to know if there exists any error if the login fails ???
--
Posted via http://www.ruby-....