HowTo use passwords in scripts

31.08.2020

Last modified: 31.08.2020
estimated reading time < 1 min

You might want to use passwords in scripts. as the interactive password entering is not possible in scripts, here is a work-around.

First install expect (assuming you are using Ubuntu or a similar distribution type the following:

sudo /apt-get install expect

after that the following can be used as a script – in the example below it is used to connect to a computer with ssh

#!/usr/bin/expect -f
spawn ssh user@my.server.com
expect "assword:"
send "mypassword\r"
interact

save the script (script.sh), close the script and make it executable by typing

chmod a+x script.sh

That’s all. Now you can execute your script by typing

./script.sh
Tags:
Was this article helpful?
Dislike 0
Views: 27

Search