首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过Ruby Net-SSH执行密码更改

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

Stack Overflow用户
提问于 2010-05-20 11:42:38
回答 1查看 1.2K关注 0票数 2

我希望通过Net-ssh执行密码更改,但此代码似乎挂起了:

代码语言:javascript
复制
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获得输出,但是这个命令挂起了。

有什么想法吗?

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-09 10:27:43

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

代码语言:javascript
复制
#~~~~~~~~~~~~~~~~~~~~~~~
# 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2870880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档