15

一般的に使用される sudo シェル コマンド (たとえば、C-c srun (shell-command "sudo /etc/init.d/apache2 restart")) のショートカット キーを作成しようとしています。

上記のようにストレートなシェルコマンド呼び出しを使用してみましたが、次を*Shell Command Output*バッファーに出力するだけです。

[sudo] password for Inaimathi:
Sorry, try again.
[sudo] password for Inaimathi:
Sorry, try again.
[sudo] password for Inaimathi:
Sorry, try again.
sudo: 3 incorrect password attempts

実際にはパスワードを要求しません。を使用して Emacs を起動する必要はありませんがsudo emacs、他に何も機能しない場合は、それがオプションだと思います。

理想的な解決策は、Emacs 内の関数です (シェルまたはコマンドの動作を変更する OS のジガリー ポケリーとは対照的ですsudo)。(sudo-shell-command "dostuff")、またはのようなもの(with-password-prompt (shell-command "sudo dostuff"))

4

7 に答える 7

14

I frequently call commands from Emacs like aptitude update. scottfrazer's solution might not be as useful. Synchronous commands make me wait for a long time, and if you execute an unsupported program (for example, aptitude, which uses ncurses), you will hang up Emacs (C-g won't help), and CPU load will be 100%. Changing to async-shell-command solves this.

But it also introduces a new problem. If your command fails, your password will end up in *Messages* buffer:

echo PASSWORD | sudo -S aptitude: exited abnormally with code 1.

That's why i propose the following solution:

(defun sudo-shell-command (command)
  (interactive "MShell command (root): ")
  (with-temp-buffer
    (cd "/sudo::/")
    (async-shell-command command)))

Here "M" in interactive prompts for program name in minibuffer, with-temp-buffer creates a sham buffer, in which we change directory to /sudo::/ to use TRAMP for sudo prompt.

This is the solution by David Kastrup from sudo command with minibuffer password prompt @ gnu.emacs.help.

Note, you still shouldn't call aptitude directly, otherwise the subprocess will be there forever, until you send sudo pkill aptitude.

Read on shells and processes in manual.

于 2013-07-30T09:38:02.550 に答える
13

どうですか:

(shell-command (concat "echo " (shell-quote-argument (read-passwd "Password? "))
                       " | sudo -S your_command_here"))
于 2010-03-18T20:29:51.560 に答える
6

emacs22 以降を実行している場合は、emacs からシェルを起動して、そこで sudo コマンドを実行するだけです。パスワードのミニバッファ ウィンドウに自動的に移動します。

M-x shell
sudo whoami

これは、画面の下部でパスワードを要求するだけです (パスワードは表示されません)。

于 2010-03-18T18:12:52.280 に答える
3

回避策(emacs ソリューションではなく):

パスワードが不要になるように ssh 鍵ペアをセットアップします。

手順:

  1. ssh-keygenキーのペアを生成するために実行します。これに慣れたら、作成する他のすべてのものからそれらを整理しておくために、それらに便利な名前を付けます
  2. 公開アカウントを受信アカウント$HOME/.sshにコピーします
  3. 送信アカウントのプライベートなものを保持し$HOME/.sshます(複数の送信アカウントにコピーできますが、受信マシンごとに個別のキーペアを作成することをお勧めします)
  4. 送信マシンで編集$HOME/.ssh/configして、使用するキーをsshに指示します
  5. 利益
于 2010-03-18T18:38:45.943 に答える
1

編集:上記のスコットの答えは、このハックよりもはるかに好ましいです。それを使用します。

考えられる解決策:

デフォルトのパスワード要求ユーティリティを設定すると、この問題が解決することがわかりました。

私がしなければならなかったことは、ファイルに追加Defaults:ALL askpass=/usr/lib/openssh/gnome-ssh-askpassすることです(明らかにを使用して)。/etc/sudoerssudo visudo

Eval-ing(shell-command "sudo /etc/init.d/apache2 restart")は、パスワードの推測に失敗するのではなく、パスワードの入力を求めるようになりました。

より良い解決策がないことが明らかにならない限り、私はこの答えを受け入れません。/etc理想的には、ディレクトリをいじる必要がなく、Emacs の内部で問題を解決できるものが欲しいです。

于 2010-03-18T18:37:54.803 に答える
1

sudoパスワードを読み取るために端末デバイス (tty) を開こうとします。emacs プロセスに制御端末がない場合があります。sudo -Semacsから取得する必要があるパスワードに標準入力を使用するように指示します。

于 2010-03-18T18:18:22.800 に答える
0

以下を使用して、ルートとして emacs から nmap を起動しました。

http://nakkaya.com/sudoEl.markdown

于 2010-03-18T20:07:58.877 に答える