[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!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
serverlist="server1 server2 server3 server4... server10"
for server in $serverlist
ldapsearch -h $server -p ...... > /home/jack/output.txt
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).
#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