开发者

Executing Password Change over Ruby Net-SSH

开发者 https://www.devze.com 2022-12-31 21:55 出处:网络
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|

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 bec开发者_开发问答ause 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.


This one will solve your issue ,, note this script to change password for a list of servers list in file

#~~~~~~~~~~~~~~~~~~~~~~~
# Change  Password is a simple script to change the password for a list of servers 
# Coded by : Sabry Saleh
# License : GPL2
#~~~~~~~~~~~~~~~~~~~~~~~
#=-Notes-=
# You have to install ruby + net-ssh gems 
# sudo gem install net-ssh
#~~~~~~~~~~~~~~~~~~~~~~~

require 'net/ssh'

host = IO.readlines('test1.txt') # full path of servers' list
port = 22       # SSH port
user = 'username'   # username
  i  = 0

while i < host.length 

  Net::SSH.start(host[i], user , :password => "User pass" , :port=> port) do |ssh|
    ssh.open_channel do |channel|
    channel.on_request "exit-status" do |channel, data|
      $exit_status = data.read_long
    end
      channel.request_pty do |channel, success| 
      channel.exec("sudo passwd UserName")  # Logged user shuold be root or sudoers memeber
      if success
        channel.on_data do |channel, data|
          puts data.inspect.chomp("\r\n")
          channel.send_data("New pass\n") # put the New password you need to set
          sleep 0.1
        end
      else
        puts "FAILED!!"
      end
    end
    channel.wait
    puts "SUCCESS!!" if $exit_status == 0
    end
  end
i += 1
end
0

精彩评论

暂无评论...
验证码 换一张
取 消