Walker News

How To Use SSH Password In Shell Script?

How to automate SSH login using password, if the destination server (e.g. home router modem) doesn’t support public-key cryptography authentication method?

The Putty’s console-based SSH client, plink, accepts password via option switch. The Linux “native” OpenSSH client, however, doesn’t have option switch that takes password.

So, when it’s not possible to add a public key to the SSH server, you may consider using Putty for Linux or sshpass, to perform non-interactive SSH login using password in shell script or cronjob (automated task by Linux scheduler).
WARNING: There is a risk of exposing the SSH login password, because the credential is in plain text mode when passing it to the ssh client via option switch!

To lower the risk level:

1. Make sure the connecting machine is highly secured.

2. If possible, configure the firewall at destination machine to only allow SSH login from that connecting machine (this option may not available at home router modem).

At this time of writing, the developer of respective open-source project doesn’t provide compiled binary. Thus, you’ve to compile the program from source files.
Don’t worry, it isn’t a rocket science. I tested the following compilation guide successfully on RHEL5 platform.

Compile sshpass for Linux
Download and decompress the latest gzipped tar archive of sshpass source files:
tar -zxvf sshpass-1.05.tar.gz

Change working directory to the extracted folder of source files to compile the sshpass binary program:
make clean
make; echo $?

If the compilation is successful, you should see “0” in the last line of output (by the “echo $?” command).

Now, let’s test the compiled sshpass program file. For example, let sshpass submits password “abc123” to ssh client that login to the server with user id “pi”:
./sshpass -p abc123 ssh pi@

Compile Putty’s plink for Linux
Download the latest gzipped archive of Putty source files and extract it:
tar -zxvf putty-0.63.tar.gz

In the extracted folder, there is a sub-folder called unix. Let’s change working directory to this folder:
cd putty-0.63/unix

Compile Putty for Linux:
make clean
make; echo $?

If the configure script cannot detect GTK 1/GTK 2 libraries, you can only build the console-based Putty utilities. This is alright, as we don’t need the GUI-based Putty SSH client. Again, you should see “0” in the last line of output, if the compilation is successful.

To test the compiled plink program file:
./plink -pw abc123 pi@

Custom Search

2016  •  Privacy Policy