ruby - 通过 Ruby Net-SSH 执行密码更改

标签 ruby net-ssh

我想通过 Net-ssh 执行密码更改,这段代码似乎挂起:

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

我知道连接有效,因为使用一个简单的 exec 我可以从远程服务器上的 ls 获取输出,但是它挂起了。

有什么想法吗?

调试的最后一条消息是公钥成功。

最佳答案

这个将解决您的问题,注意这个脚本可以更改文件中服务器列表的密码

#~~~~~~~~~~~~~~~~~~~~~~~
# 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

关于ruby - 通过 Ruby Net-SSH 执行密码更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2870880/

相关文章:

ruby-on-rails - 使用 apt-get install ruby​​ 2.0.0 安装 ruby​​ 成功但未使用正确的 ruby​​ 版本

ruby - 将嵌套哈希转换为 Ruby 中以点分隔的字符串的一行代码?

ruby-on-rails - 将 erb 从数据库渲染到 View 问题请帮助!

jruby - 为什么不支持 key 类型 ecdsa-sha2-nistp256?

net-ssh - Localtunnel gem/Net::SSH 不起作用

ruby-on-rails - 在通过 Brightbox 的 PPA 安装的 Ruby 2.1.x 上安装 Mongrel 1.2.0_pre2 时出错

sql - 通过 SQL 错误更新 has_many

ruby - 如何在 Ruby 中使用 net-ssh sudo su

ruby - 如何将 SIGINT 发送到 Net::SSH session 中的进程启动?

ruby-on-rails - Net-ssh 执行超时?