Forum: Ruby Automating a password reset via net-telnet

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2007-07-10 18:59
(Received via mailing list)
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
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2007-07-10 23:17
(Received via mailing list)
On Jul 10, 10:58 am, Daniel Berger <djber...@gmail.com> 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.

<snip>

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
This topic is locked and can not be replied to.