We have a script I use at work, that I did not write, that when its doing it thing prompts for same password about 5-6 times. I'd like to write something into it that when it asks for the password the first time, it stores the pw and autocompletes it each time thereafter. Is it possible to do this is a bash script? My understanding us the script is doing an ssh to another server and running an update on a file to make sure it has the most current version and if you are entering more than one user into the file it need to ask every time it updates.
Asked
Active
Viewed 857 times
-2
-
Are you sure you want to store (unencrypted) password? The prompt is probably put there for a reason. – CloudyTrees Feb 19 '20 at 20:56
-
I don't want to store it forever, I want to enter it once then have it store for the duration of the script. – Nick Paulsen Feb 19 '20 at 20:59
-
1We'd have to see the script to make suggestions. – Frank Merrow Feb 19 '20 at 23:06
-
You could use `expect` script see https://stackoverflow.com/questions/4780893/use-expect-in-bash-script-to-provide-password-to-ssh-command – Jetchisel Feb 19 '20 at 23:14
-
Is it `ssh` prompting because it's making a bunch of connections, or something else (or a combination)? If it's just `ssh` over and over, you might be able to open a [master connection](https://www.cyberciti.biz/faq/linux-unix-osx-bsd-ssh-multiplexing-to-speed-up-ssh-connections/), leave it open for the duration of the script, multiplex the others over it, then close it when the script finishes. – Gordon Davisson Feb 19 '20 at 23:56
-
The master connection option worked perfect. Thanks! – Nick Paulsen Feb 22 '20 at 00:07
1 Answers
0
First off, remember this is TERRIBLE SECURITY PRACTICE!!! With that in mind, what is the reason for the password prompt? When using sudo, you can always do this:
#!/bin/bash
read -s PASSWORD
echo $PASSWORD | sudo -S <whatever you want to do that prompts for password>
Explanation:
sudo -S reads password from stdin
Source: https://superuser.com/questions/67765/sudo-with-password-in-one-command-line
So in conclusion, you can use sudo along with password from stdin to whatever you want to do.
I hope this helps!

Math Bob
- 113
- 1
- 6