0

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 [email protected])

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

9
  • 3
    Are keys rather than password an option for you? Commented Apr 17, 2018 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. Commented Apr 17, 2018 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. Commented Apr 17, 2018 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 Commented Apr 17, 2018 at 12:29
  • stackoverflow.com/a/43526842/13317 Commented Apr 17, 2018 at 14:38

2 Answers 2

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.

3
  • 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 Commented Apr 17, 2018 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. Commented Apr 17, 2018 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/…) Commented Apr 17, 2018 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 [email protected] "mkdir testFolder"

This command will create a test folder from script code.

1
  • 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. Commented Apr 17, 2018 at 12:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.