Executing Password Change over Ruby Net-SSH

Hi all,

I am looking to execute a password change over Net-ssh and this code
seems to hang:

Net::SSH.start(server_ip, "user", :verbose => :debug ) do |session|

  session.process.popen3("ls") do |input, output, error|

    ["old_pass","test", "test"].each do |x|

        input.puts x

    end

  end

end

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?

The last message from debug is that the public key succeeded.

···

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

Here's an old script I had used to randomize weak passwords. I
haven't tried it for years, but I hope it helps:

#!/usr/bin/env ruby

require 'rubygems'
require 'net/ssh'

usernames = ["username"]
passwords = ["password"]
hostnames = ["hostname"]

# For example:
# usernames = [ 'alice', 'bob', 'charlie' ]
# usernames = %qw{ 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 ) do |session|
          puts "\t\t\tBAD"
          command = "passwd"
          newword = ""
          (rand(32) + 32).times do
            newword << (rand(95) + 32).chr
          end

          session.process.open( command ) do |shell|
            shell.on_success do |p|
              puts "process started"
            end

            shell.on_failure do |p|
              puts "process failed to start"
            end

            shell.on_stderr do |p,data|
              puts "E-> #{data}"
              case data
                when /\(current\) UNIX password:/
                  p.puts password
                  puts "********"
                when /New UNIX password:/
                  p.puts newword
                  puts "********"
                when /Retype new UNIX password:/
                  p.puts newword
                  puts "********"
                else
                  puts "DEATH!"
                  exit
              end
            end

            shell.on_stdout do |p,data|
              puts "O-> #{data}"
            end

            shell.on_exit do |p, status|
              puts "process finished with exit status: #{status}"
            end
          end
        end
      rescue
        puts "\t\t\tok"
      end
    end
  end
end

···

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?

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

···

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. I
haven't tried it for years, but I hope it helps:

require 'rubygems'
require 'net/ssh'
def passWord(hostname, username, password)
begin
Net::SSH.start( hostname, username, :password => password ) do |session|
  puts "\t\t\tBAD"
  command = "passwd"
  newword = ""
  (rand(32) + 32).times { newword << (rand(95) + 32).chr }
  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

usernames = [ '', ]; passwords = [ '' ]; hostnames = [ '']
srand

hostnames.each do |hostname|
puts "\t#{hostname}"
usernames.each do |username|
  puts "\t#{username}"
  passwords.each do |password|
   puts "\t\t#{passWord(hostname,username, password)}" end end end

···

---------------------------------
Test before use!