Skip to content

Instantly share code, notes, and snippets.

@ashrithr
Created May 27, 2013 03:52
Show Gist options
  • Save ashrithr/5655105 to your computer and use it in GitHub Desktop.
Save ashrithr/5655105 to your computer and use it in GitHub Desktop.
ruby script to change the passwords of remote servers using net-ssh
require 'net/ssh'
hosts = IO.readlines('servers.txt') # full path of servers' list
port = 22 # SSH port
user = 'root' # username
old_password = 'secret123'
new_password = 'secret@123'
hosts.each do |host|
Net::SSH.start(host, user , :password => old_password , :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_password}\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
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment