Wednesday, January 17, 2007

su with ruby

# Ruby snippet demonstrating use of prompt with su

require 'net/telnet'

def loginViaTelnet server
telnet = Net::Telnet.new({'Host' => server, 'Prompt' => /[$%#>]|Password[: ]*\z/}) { |str| print str }
telnet.login("userid1", "password1") { |str| print str }
return telnet
end

def suToOtherUser telnet
telnet.cmd('su - otherUser') #{ |str| print str }
telnet.cmd('otheruserPassword') { |str| print str }
end