If you have root privileges on the remote server, I would set up sudoers to allow the user you use for ssh run passwordless sudo. You can be really specific about the commands you want to allow without password. In terms of security your ssh key should provide a better level of authentication than a password anyway.
If you not control the server and it would be a hassle to ask the admin to allow you to sudo without password, the Net::SSH documentation gives a neat example of how you can respond to the password prompt:
Net::SSH.start("host", "user") do |ssh|
ssh.exec! "cp /some/file /another/location"
hostname = ssh.exec!("hostname")
ssh.open_channel do |ch|
ch.exec "sudo -p 'sudo password: ' ls" do |ch, success|
abort "could not execute sudo ls" unless success
ch.on_data do |ch, data|
print data
if data =~ /sudo password: /
ch.send_data("password\n")
end
end
end
end
ssh.loop
end