0

This question already has an answer here:

I'm new to bash scripting. I want to call a function from my script which will ssh into a remote computer (on my LAN) and run a command.

So far I have:

function run_ssh_command {
    target_ip=$1
    username=$2
    password=$3
    cmd=$4

    ssh -l ${username} ${target_ip} ${password}
}

I invoke the function from the terminal as follows:

(Note: I give script_name, username and password "real" values when executing the function on my machine. 'ifconfig' is the command I want to run on the remote machine.)

source script_name.sh; run_ssh_command 192.168.X.Y username password ifconfig

Result: Running the above command will get me to the password prompt part of the login process (e.g. same as ssh username@192.168.X.Y)

Question: I want to handle the password entry automatically using the script. What is the "best" way to go about this, in general?

marked as duplicate by αғsнιη, Christopher, Timothy Martin, Rui F Ribeiro linux Apr 17 '18 at 21:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    Are keys rather than password an option for you? – Ikaros Apr 17 '18 at 10:52
  • 1
    @Ikaros No unfortunately keys are not an option for me. Additionally i'm too new a user to determine if this question will be deemed a "duplicate" of the other you linked to. However, I had tried the "solution" to that similar question and it did not work for me. – MarkMark Apr 17 '18 at 11:07
  • Maybe sshpass is an option for you. Usage is sshpass -p <PASSWORD> ssh user@host; in your case sshpass -p <PASSWORD> ssh user@host ifconfig. – mnille Apr 17 '18 at 11:59
  • @mnille Thanks but it won't do on this occasion unfortunately. I am seeking a way to either pass the password to the ssh command directly OR a method by which my script waits for the password prompt and then enters the password – MarkMark Apr 17 '18 at 12:29
  • stackoverflow.com/a/43526842/13317 – Kenster Apr 17 '18 at 14:38
0

You can use sshpass. However, this requires you to hardcode the password, which is in general unsafe and thus not recommended, but might be required in case you can't use RSA fingerprints.

Usage:

sshpass -p somepassword ssh user@host

If you have root access to the remote machine you should defenitely consider establishing RSA keys. This explains how it is setup.

  • Thanks, but this is not a suitable method for me at this time. If you notice in the original post when I call the function I pass it 4 arguments, of which one is the password. I am seeking a way to either pass the password to the ssh command directly OR a method by which my script waits for the password prompt and then enters the password – MarkMark Apr 17 '18 at 12:27
  • Well I do not know of any possibility of doing that, other than sshpass. You can use sshpass inside the script, all you have to do is install it. – telina Apr 17 '18 at 12:48
  • I think you do not understand this correctly. sshpass does forward the password directly to ssh. There is no way to stop the script and echo the password into the password prompt. The ssh command does not allow for this."ssh uses direct TTY access to make sure that the password is indeed issued by an interactive keyboard user. Sshpass runs ssh in a dedicated tty, fooling it into thinking it is getting the password from an interactive user. " (cyberciti.biz/faq/…) – telina Apr 17 '18 at 12:56
0

Use this to run ssh from script:

ssh -i {privateKey} -o StrictHostKeyChecking=no {userName}@{serverIp} "{command}"

Example:

ssh -i privateKey -o StrictHostKeyChecking=no user@127.0.0.1 "mkdir testFolder"

This command will create a test folder from script code.

  • Thanks, I should have mentioned in the original post that I can't use keys to achieve this. I am seeking a way to either complete the login process directly (hostname, username, password) in one go OR some modification to the code shown in the original post whereby the script "sees" the password prompt and then enters the password argument. – MarkMark Apr 17 '18 at 12:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.