【问题标题】:Executing Password Change over Ruby Net-SSH通过 Ruby Net-SSH 执行密码更改
【发布时间】:2010-05-20 03:42:37
【问题描述】:

我希望通过 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 获取输出,但这会挂起。

有什么想法吗?

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

【问题讨论】:

    标签: ruby net-ssh


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 2018-09-08
      • 2013-01-19
      • 2019-12-01
      相关资源
      最近更新 更多