Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provide password to ssh command inside bash script, Without the usage of public keys and Expect

I want to use SSH inside a script, but this script is not going to be executed on my machine.

In my implementation there are two limitations.

  • I can not work outside shell's standards,therefore i can not use expect because i do not know if it will be available on this machine.
  • I can not expect that this machine will have public keys for the SSH.

What are the possible options-solutions ?

How can i provide ssh with the requested password with an automated and secure way without adding extra dependencies?

Will it be possible to provide the password inside the script?

Thank you all in advance :)

like image 202
stratis Avatar asked Jun 05 '13 10:06

stratis


2 Answers

Install sshpass, then launch the command:

sshpass -p "yourpassword" ssh -o StrictHostKeyChecking=no yourusername@hostname
like image 65
gino pilotino Avatar answered Sep 21 '22 16:09

gino pilotino


For security reasons you must avoid providing password on a command line otherwise anyone running ps command can see your password. Better to use sshpass utility like this:

#!/bin/bash

export SSHPASS="your-password"
sshpass -e ssh -oBatchMode=no sshUser@remoteHost

You might be interested in How to run the sftp command with a password from Bash script?

like image 24
anubhava Avatar answered Sep 20 '22 16:09

anubhava