LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 10-26-2012, 07:49 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
How to pass password in a loop in shell script?


Hello everyone!
I have a script like this:
Code:
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.

Last edited by shivaa; 10-26-2012 at 07:51 AM.
 
Old 10-26-2012, 07:52 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,397

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
At its most simple just use $1 as the password and that will pass the first parameter the script is executed with, e.g. "./myscript 'p4$$w0rd'"
 
1 members found this post helpful.
Old 10-26-2012, 08:00 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Quote:
Originally Posted by acid_kewpie View Post
At its most simple just use $1 as the password and that will pass the first parameter the script is executed with, e.g. "./myscript 'p4$$w0rd'"
Thanks Chris, script is executing like this:
Code:
Enter bind password:
Result
Enter bind password:
Result
Enter bind password:
Result
........
........
Can you explain your answer little more, and show that how can I pass password using $1 variable, by modifying my script? I'd be thankful to you.

Last edited by shivaa; 10-26-2012 at 08:02 AM.
 
Old 10-26-2012, 08:45 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,397

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
serverlist="server1 server2 server3 server4... server10"
for server in $serverlist
do
ldapsearch -h $server -w $1 > /home/jack/output.txt
.....
.....
echo "Result"
done
 
1 members found this post helpful.
Old 10-26-2012, 09:41 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Awesome Chris! Everything's working fine now!
Keep it up!! Cheers!!!
 
Old 10-26-2012, 09:42 AM   #6
archShade
Member
 
Registered: Mar 2006
Location: Delft NL
Distribution: Debian; Slackware; windows 7
Posts: 218

Rep: Reputation: 53
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
I'm sure there is another way using stty as well.
 
1 members found this post helpful.
Old 10-26-2012, 10:16 AM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Thanks a lot @archShade. Your tips are also helpful as it helped me to avoid any password disclosure pb.
 
  


Reply

Tags
ldap, password, passwordless, shell scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to pass password in Shell Script shivaa Linux - Newbie 3 10-04-2012 09:40 AM
How to auto pass password in shell script ! cafecoc85 Programming 6 10-01-2012 02:08 AM
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
how to pass MySQL user/pass securely in shell script? digity Linux - Newbie 5 01-07-2010 05:48 AM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM


All times are GMT -5. The time now is 03:22 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration