Skip to content

Instantly share code, notes, and snippets.

@albb0920
Created May 19, 2011 03:50
Show Gist options
  • Save albb0920/980153 to your computer and use it in GitHub Desktop.
Save albb0920/980153 to your computer and use it in GitHub Desktop.
Simple sudo under net/ssh
#!/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
@googya
Copy link

googya commented Dec 18, 2012

in `block in exec!': unexpected return (LocalJumpError)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment