How to automate password entry? How to automate password entry? bash bash

How to automate password entry?


Here’s an expect example that sshs in to Stripe’s Capture The Flag server and enters the password automatically.

expect <<< 'spawn ssh level01@ctf.stri.pe; expect "password:"; send "e9gx26YEb2\r";'


With SSH the right way to do it is to use keys instead.

# ssh-keygen

and then copy the *~/.ssh/id_rsa.pub* file to the remote machine (root@$node) into the remote user's .ssh/authorized_keys file.


You can perform the task using empty, a small utility from sourceforge. It's similar to expect but probably more convenient in this case. Once you have installed it, your first scp will be accomplished by following two commands:

./empty -f scp InstallSWIG.sh root@$node:/root/InstallSWIG.shecho YOUR_SECRET_PASSWORD | ./empty -s -c

The first one starts your command in the background, tricking it into thinking it's running in interactive mode on a terminal. The other one sends it data from stdin. Of course, putting your password anywhere on command line is risky due to shell history being preserved, users being able to see it in ps results etc. Not secure either, but a bit better thing would be to store the password in a file and redirect the second command's input from that file instead of using echo and a pipe.

After copying to the server, you can run the script in a similar manner:

./empty -f ssh root@$node sh InstallSWIG.shecho YOUR_SECRET_PASSWORD | ./empty -s -c