# How to catch and rescue a login error through telnet

**URL:** <https://www.ruby-forum.com/t/how-to-catch-and-rescue-a-login-error-through-telnet/126102>\
**Category:** Ruby\
**Created:** [January 15, 2008, 1:15pm UTC](https://www.ruby-forum.com/t/how-to-catch-and-rescue-a-login-error-through-telnet/126102 "2008-01-15T13:15:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prasadm29](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/cc9497/32.png) [@prasadm29](https://www.ruby-forum.com/u/prasadm29)\
**Post date:** [January 15, 2008, 1:15pm UTC](https://www.ruby-forum.com/t/how-to-catch-and-rescue-a-login-error-through-telnet/126102/1 "2008-01-15T13:15:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![prasadm29](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/cc9497/32.png) [@prasadm29](https://www.ruby-forum.com/u/prasadm29)\
**Post date:** [January 15, 2008, 2:00pm UTC](https://www.ruby-forum.com/t/how-to-catch-and-rescue-a-login-error-through-telnet/126102/2 "2008-01-15T14:00:20Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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…](http://lindsaar.net/2007/12/9/rbuf_filltimeout-error)
> 
> Regards
> 
> Mikel

---

<div class="post-metadata">

**Author:** ![prasadm29](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/cc9497/32.png) [@prasadm29](https://www.ruby-forum.com/u/prasadm29)\
**Post date:** [January 15, 2008, 1:32pm UTC](https://www.ruby-forum.com/t/how-to-catch-and-rescue-a-login-error-through-telnet/126102/3 "2008-01-15T13:32:44Z")

</div>

On Jan 15, 2008 11:14 PM, Prasad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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:

> **[Mikel Lindsaar](https://lindsaar.net/2007/12/9/rbuf_filltimeout-error)**
>
> Internet Home for Mikel Lindsaar

Regards

Mikel

---

<div class="post-metadata">

**Author:** ![prasadm29](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/cc9497/32.png) [@prasadm29](https://www.ruby-forum.com/u/prasadm29)\
**Post date:** [January 16, 2008, 6:42am UTC](https://www.ruby-forum.com/t/how-to-catch-and-rescue-a-login-error-through-telnet/126102/4 "2008-01-16T06:42:46Z")

</div>

Just curious to know if there exists any error if the login fails ???
