[SOLVED] How to pass password in a loop in shell script?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
serverlist="server1 server2 server3 server4... server10"
for server in $serverlist
do
ldapsearch -h $server -p ...... > /home/jack/output.txt
.....
.....
echo "Result"
done
The ldapsearch command always needs a password to fetch the output data from the $server. So when I run this script, I have to enter the password 10 times for 10 servers.
Is there any way so can I pass the password only once - either inside the script or after invoking it?
One more thing, since it needs password for every server, so after display echo message "Result" it dispalys "Enter password".. which also I don't want to display. I just want to display the result message.
I really want to recomend against using $1 as the way to read in a pasword. If you really trust your enviroment it could be OK but someone "sholder surfing" could see your pasword in plain text. This will also copy your password to ~/.bash_history. OK again you could argue that it's safe but as there is an easy fix why risk it.
I use the command "read -p "Enter Password: " -s password_store" (without quotes) to read a password into a script.
read reads in a file if no file is specified then stdin is used, the "-p" flag print a string, the "-s" flag suppresses typed values being used output to stdout. The final term is a varible to store the string passed to.
I have an example file. It was copied out of someone elses code (I can't rember who).
Code:
#! /bin/bash
#A short example of how to read a string into a script
#while suppressing typed characters on stdout.
#Used to prevent "sholder surfing" and passwords being stored in bash_history
read -p "Enter Password: " -s password
#As this is an example we will now write password to stdout.
echo -e "\n" $password
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.