输入密码后,Net :: SSH sudo命令挂起 [英] Net::SSH sudo command hangs after entering password

查看:123
本文介绍了输入密码后,Net :: SSH sudo命令挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Thor编写一个小型库,以帮助我快速创建新项目和站点.我写了这个小方法:

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.

问题是当我需要在远端的sudo下运行命令时,脚本似乎挂在我身上.例如,执行此命令时...

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}" )

脚本将提示我输入密码

[sudo] password for user:

但是输入完整个内容后,它就会挂起.

But then the whole thing hhangs after typing it in.

有人会碰巧知道它为什么会正确挂起吗,我该怎么办才能在Net :: SSH(或某些替代方式)下的远程服务器上运行sudo命令?

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:在建议之前,我最初是作为Capistrano的食谱编写该库的,直到遇到Thor为止,并认为这是一个尝试的好机会.我不反对在需要时将整个过程切换回Capistrano,但是如果没有一种简单的方法可以在远程服务器上运行sudo命令,我会感到非常惊讶.

*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.

推荐答案

您可能要尝试的第一件事是使用公钥而不是密码进行登录.然后,还尝试从交互式外壳程序运行命令.

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.

例如:

(这部分实际上取决于您拥有的服务器/客户端软件)

(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.

这篇关于输入密码后,Net :: SSH sudo命令挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆