Search code examples
rubysudonet-sshsudoersthor

Net::SSH sudo command hangs after entering password


I've been trying to write a small library using Thor to help assist me in quick creating new projects and sites. I wrote this small method:

def ssh(cmd)
  Net::SSH.start( server_ip, user, :port => port) do |session|
    session.exec cmd
  end
end

to just assist me in running quick commands on remote servers when needed.

The problem is when I need to run a command under sudo on the remote end, the script just seems to hang on me. For example when executing this...

ssh("sudo cp #{file_from_path} #{file_to_path}" )

The script will prompt me for a password

[sudo] password for user:

But then the whole thing hhangs after typing it in.

Would anyone happen to know why it hangs exactly, and what I can do to run sudo command on a remote server under Net::SSH (or some alternative)?

*note: Before suggested, I had originally started writing this library as a recipe under Capistrano, until I came upon Thor, and thought it would be a good chance to try it out. I'm not against having to switch the whole thing back to Capistrano if needed, but I'd just be really surprised if there isn't an easy way to run sudo commands on a remote server.


Solution

  • The first thing you might want to try is using public keys instead of passwords to login. Then also try running the command from the interactive shell.

    For example:

    (This part really depends on the server/client software you have)

    $ ssh-keygen
    $ scp .ssh/id-dsa.pub server:
    $ ssh server
    server$ cat id-dsa.pub >> .ssh/authorizedkeys
    
    $ scp -c "ls"
    

    this should work without any prompts, if the key sharing was successful.