SSHPass: How to SSH into a server using a script with no password (non-interactively)

SSHPass

Ever asked yourself:

  • How do I use ssh in a shell script? How do I login non-interactively performing password authentication with SSH and shell scripts?
  • How do I login over ssh without using password less RSA / DSA public keys?

First, if you just want to log into one or two servers without having to enter a password all the time, the best way to do this is via SSH private keys. Our tutorial on how to do this is here. However, if you find yourself in a situation where you might have tens or hundreds of servers to log into then this might be the solution for you. In my case, I had a situation where I had a few hundred servers, which all had really weak root passwords. I wanted to change the passwords on each box with a nice long random password. I wanted to script this so I didn’t have to do it manually for every server. I used a simple bash script in the end, however a configuration management system such as Ansible is also a good option if you want a repeatable way to do things like this.

Using SSHPass, which is freely available for pretty much every Unix distribution is the tool you probably want, check out this handy tutorial from *nixCraft:

Login To SSH Server / Provide SSH Password Using A Shell Script

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.