Created
May 19, 2011 03:50
-
-
Save albb0920/980153 to your computer and use it in GitHub Desktop.
Simple sudo under net/ssh
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
#!/usr/local/bin/ruby19 | |
require 'rubygems' | |
require 'net/ssh' | |
PASSWORD = '.....' | |
def shell | |
Net::SSH.start('127.0.0.1','user',:password => PASSWORD) do |ssh| | |
yield(SudoWrapper.new(ssh, PASSWORD)) | |
end | |
end | |
class SudoWrapper | |
def initialize ssh, password | |
@ssh = ssh | |
@password = password | |
end | |
def exec! command | |
@ssh.exec! "sudo -p 'SUDO_PASSWORD' #{command}" do |ch, stream, data| | |
if data =~ /SUDO_PASSWORD/ | |
ch.send_data "#{@password}\n" | |
else | |
return data | |
end | |
end | |
end | |
end | |
shell do |sh| | |
puts sh.exec! 'whoami' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in `block in exec!': unexpected return (LocalJumpError)