Skip to content

Instantly share code, notes, and snippets.

@KINGSABRI
Created October 30, 2011 13:15
Show Gist options
  • Save KINGSABRI/cfb801c102f31c327eb5 to your computer and use it in GitHub Desktop.
Save KINGSABRI/cfb801c102f31c327eb5 to your computer and use it in GitHub Desktop.
Change Password remotly
require 'net/ssh'
Net::SSH.start('127.0.0.1', 'user', :password => "pass") 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("NewpassOfUserName\n")
sleep 0.1
end
else
puts "FAILED!!"
end
end
channel.wait
puts "SUCCESS!!" if $exit_status == 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment