批量更改主机密码

阅读更多
require "rubygems"
require "net/ssh"

def rch_passwd(server, username, password)
  Net::SSH.start(server, username, :password => password,:timeout => 120) do |ssh|
    ssh.exec!("echo \"root:girl75B\" | chpasswd")
  end
end

# File format
# 10.2.226.87:root:wwtest1
# 10.2.226.88:root:wwtest2
# 10.2.226.89:root:wwtest3
# 10.2.226.90:root:wwtest4
def load_data(file)
  raise ArgumentError unless file
  data = []
  bad = []
  begin
    IO.readlines(file).each do |l|
      t = l.chomp($/).split(':')
      t.size == 3 ? data << t : bad << t
    end
  rescue Exception => e
    p "Can't access #{file}"
  end
  return data,bad
end



print "#  0.0.0.0:username:password#{$/}"

data,bad = load_data('./host.txt')
i = 1
r = Range.new(i,-1)

data[r].each do |d|
  print i," ",d.join(":"),$/
  i+=1
  rch_passwd(*d)
end

p "Some data are bad."
bad.each{|b|p b.inspect}

你可能感兴趣的:(SSH,rubygems,Ruby,.net,Access)