On Thu, May 20, 2010 at 9:56 AM,  <brabuhr / gmail.com> wrote:
> On Thu, May 20, 2010 at 9:28 AM, Tyler Smart <tyleresmart / gmail.com> wrote:
>> I am looking to execute a password change over Net-ssh...
>>
>> I know the connection works because using a simple exec I can get the
>> output from ls on the remote server, but this hangs.
>>
>> Any ideas?
>
> Here's an old script I had used to randomize weak passwords.  
> haven't tried it for years, but I hope it helps:

I had some time and took a look at it.  Net-SSH 2 no longer has the
API I had used in that old script, here is a rough update that appears
to work with NetSSH 2:

require 'rubygems'
require 'net/ssh'

usernames = [ 'alice', 'bob', 'charlie' ]
passwords = [ 'password', 'baseball', 'apple', '123456' ]
hostnames = [ 'alpha', 'beta' ]

srand

hostnames.each do |hostname|
  puts hostname
  usernames.each do |username|
    puts "\t#{username}"
    passwords.each do |password|
      puts "\t\t#{password}"
      begin
        Net::SSH.start( hostname, username, :password => password ) do |session|
          puts "\t\t\tBAD"
          command = "passwd"
          newword = ""
          (rand(32) + 32).times do
            newword << (rand(95) + 32).chr
          end

          session.exec( command ) do |channel,stream,data|
            case stream
            when :stderr
              puts "E-> #{data}"
              case data
              when /\(current\) UNIX password:/
                channel.send_data password + "\n"
                puts "********"
              when /New UNIX password:/
                channel.send_data newword + "\n"
                puts "********"
              when /Retype new UNIX password:/
                channel.send_data newword + "\n"
                puts "********"
              else
                puts "DEATH!"
                exit
              end
            when :stdout
              puts "O-> #{data}"
            end
          end
        end
      rescue
        puts "\t\t\tok (#{$!.message})"
      end
    end
  end
end