Search code examples
emacssshpasswordspromptminibuffer

emacs function to send password to minibuffer when asked for


I usually connect to a machine through SSH and using ssh.el. I defined a command so I can do this quickly:

(defun ssh-me()
  (interactive)
  (ssh "myuser@myhost"))

After that, the mini-buffer asks me for the password and everything works fine. I am wondering if there is a way to set my password in my function so I don't have to write it every time I want to connect to that machine, so I would have something like:

(defun ssh-me()
  (interactive)
  (ssh "myuser@myhost")
  (send-password-to-minibuffer "mypasswd"))

Is that possible?


Solution

  • Here is an example using a small custom file that is not encrypted. In this example, the filename is .authinfo_iphone and it contains just one line -- I use it to connect to my jailbroken iphone via ssh:

    machine localhost login root password alpine port ssh
    

    Then, I use a small function to connect:

    (defun lawlist-remote-iphone-data ()
    (interactive)
      (let ((auth-sources '("/Users/HOME/.0.data/.0.emacs/.authinfo_iphone")))
        (find-file "/ssh:root@localhost#2222:/private/var/mobile/.0.data/")))
    

    You could also use an external utility such as sshpass and then write-up a function -- the command-line looks like:

    /usr/bin/sshpass -p 'my-password' ssh [email protected]
    

    And here is an example function that incorporates that command line to log-in to the iPhone over ssh. It contemplates there is already an open shell-mode buffer with the process named *shell*.

    (defun shell-iphone ()
    "From an existing shell buffer with a process named `*shell*`,
    log-in to the iPhone using sshpass."
    (interactive)
      (let* (
          (password "alpine")
          (port "2222")
          (username "root")
          (host "localhost")
          (sshpass "/Users/HOME/.0.data/.0.emacs/bin/sshpass")
          (ssh "/usr/bin/ssh")
          (dir "/private/var/mobile/.0.data"))
        (comint-send-string "*shell*"
          (mapconcat 'identity `(
            ,sshpass
            "-p"
            ,password
            ,ssh
            "-p"
            ,port
            "-l"
            ,username
            ,host
            "-t"
            "\"cd " ,dir " && bash --login\"") " "))
        (comint-send-input)))