# Automating a password reset via net-telnet

**URL:** <https://www.ruby-forum.com/t/automating-a-password-reset-via-net-telnet/109028>\
**Category:** Ruby\
**Created:** [July 10, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/automating-a-password-reset-via-net-telnet/109028 "2007-07-10T18:59:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [July 10, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/automating-a-password-reset-via-net-telnet/109028/1 "2007-07-10T18:59:17Z")

</div>

Hi all,

I’m trying to automate a password reset via telnet for a report. This  
happens twice per year, and right now I’m handling things manually. My  
attempts so far have been unsuccessful. Here’s what I’ve tried:

require ‘net/telnet’  
include Net

session = Telnet.new(“Host” =\> host, “Output\_log” =\>  
‘ftp\_test.output’)  
session.login(user, pass)

session.cmd(“passwd”)  
#session.cmd(“String” =\> “passwd”, “Match” =\> /password:/i) # Tried  
this, too

session.puts(“old\_password”)  
session.puts(“new\_password”)  
session.puts(“new\_password”)

session.close

But, this doesn’t seem to work. The output file shows that it’s  
waiting for the current password, but the program fails with a timeout  
error. If I use the second cmd variant that I showed above, the script  
fails silently.

Any ideas?

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [July 10, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/automating-a-password-reset-via-net-telnet/109028/2 "2007-07-10T23:17:15Z")

</div>

On Jul 10, 10:58 am, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> I’m trying to automate a password reset via telnet for a report. This  
> happens twice per year, and right now I’m handling things manually. My  
> attempts so far have been unsuccessful.

The documentation from Perl’s Net::Telnet module suggested, for  
interactive programs, to always use puts/print + waitfor. So, I went  
with that approach. Here’s what ultimately succeeded:

require ‘net/telnet’  
include Net

session = Telnet.new(“Host” =\> host)  
session.login(user, “XXXX”)  
session.puts(“passwd”)

session.waitfor(/existing login password:\s\*/im)  
session.puts(current\_password)

session.waitfor(/new password:\s\*/im)  
session.puts(new\_password)

session.waitfor(/new password:\s\*/im)  
session.puts(new\_password)

session.waitfor(/password successfully changed/) # !!!

session.close

Note the multi-line regexes - that was crucial. Also, be sure to wait  
for the success message. If you don’t your session will close too  
early and it will appear that your program succeeded, when in fact it  
did not.

Regards,

Dan
