# Executing Password Change over Ruby Net-SSH

**URL:** <https://www.ruby-forum.com/t/executing-password-change-over-ruby-net-ssh/188921>\
**Category:** Ruby\
**Created:** [May 20, 2010, 3:28pm UTC](https://www.ruby-forum.com/t/executing-password-change-over-ruby-net-ssh/188921 "2010-05-20T15:28:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tesmar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dbc845/32.png) [@tesmar](https://www.ruby-forum.com/u/tesmar)\
**Post date:** [May 20, 2010, 3:28pm UTC](https://www.ruby-forum.com/t/executing-password-change-over-ruby-net-ssh/188921/1 "2010-05-20T15:28:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tesmar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dbc845/32.png) [@tesmar](https://www.ruby-forum.com/u/tesmar)\
**Post date:** [May 20, 2010, 3:57pm UTC](https://www.ruby-forum.com/t/executing-password-change-over-ruby-net-ssh/188921/2 "2010-05-20T15:57:15Z")

</div>

On Thu, May 20, 2010 at 9:28 AM, Tyler S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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:

#!/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

---

<div class="post-metadata">

**Author:** ![tesmar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dbc845/32.png) [@tesmar](https://www.ruby-forum.com/u/tesmar)\
**Post date:** [May 21, 2010, 12:04am UTC](https://www.ruby-forum.com/t/executing-password-change-over-ruby-net-ssh/188921/3 "2010-05-21T00:04:22Z")

</div>

On Thu, May 20, 2010 at 9:56 AM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Thu, May 20, 2010 at 9:28 AM, Tyler S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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:

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

---

<div class="post-metadata">

**Author:** ![tesmar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dbc845/32.png) [@tesmar](https://www.ruby-forum.com/u/tesmar)\
**Post date:** [May 21, 2010, 4:55am UTC](https://www.ruby-forum.com/t/executing-password-change-over-ruby-net-ssh/188921/4 "2010-05-21T04:55:56Z")

</div>

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
