Created
January 26, 2011 12:41
-
-
Save brianwebb01/796628 to your computer and use it in GitHub Desktop.
Update a password on a remote system via SSH using ruby of course
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'net/ssh' | |
def update_remote_password(hostname, port, username, password, newpassword, print_output=false) | |
begin | |
Net::SSH.start( hostname, username, :password => password, :port => port ) do |session| | |
command = "passwd" | |
session.exec( command ) do |channel,stream,data| | |
case stream | |
when :stderr | |
puts ":~# #{data}" if print_output | |
case data | |
when /\(current\) UNIX password:/i | |
channel.send_data password + "\n" | |
puts "********" if print_output | |
when /new UNIX password:/i | |
channel.send_data newpassword + "\n" | |
puts "********" if print_output | |
when /Retype new UNIX password:/i | |
channel.send_data newpassword + "\n" | |
puts "********" if print_output | |
when /passwd: password updated successfully/i | |
puts "Done!" if print_output | |
else | |
puts "UNKNOWN PROMPT!!! => #{data}" if print_output | |
#exit | |
end | |
when :stdout | |
puts "O-> #{data}" if print_output | |
end | |
end #end session.exec | |
end #end SSH.start | |
rescue | |
puts "\t\t\tok (#{$!.message})" | |
end | |
end #end function | |
hostname = "myserver.com" | |
username = "myuser" | |
port = "24356" | |
password = "CurrentPassW0rd" | |
new_pass = "NewPass!" | |
update_remote_password(hostname, port, username, password, new_pass, false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment