LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2011, 12:39 PM   #1
edwardcode
Member
 
Registered: Apr 2010
Posts: 161

Rep: Reputation: 17
reading password in a bash script


I am trying to write a script that will log in to all of the systems and change the password. I have the entire script written but the only problem is I use the "read" command in the script like so:
Code:
read -s PASSWORD
the only problem is that when I use the "PASSWORD" variable later on in the script it will not put in the password correctly because there are symbols in my password. Such as "$" and it will then think that everything after that symbol is a variable. Is there some way to force it to not do anything with the symbols in my password.

below you will see the script I am a tempting to change my passwd. Assume that the variables are correct.:

Code:
echo -n "Please type your current password: "
read -s OLDPASSWD
echo ""
echo -n "Please type your new password: "
read -s NEWPASSWD
echo ""

 for SERVER in ${softwareforge}; do
   /usr/bin/expect << EOF
        spawn ssh -t ${USERNAME}@${SERVER}
        expect "*?assword: "
        send "${CURRENTPASSWD=}\n"
        expect "*]$ "
        send "passwd\n"
        expect "*?assword: "
        send "${OLDPASSWD}\n"
        expect "*?assword: "
        send "${NEWPASSWD}\n"
        expect "*?assword: "
        send "${NEWPASSWD}\n"
EOF

done

Last edited by edwardcode; 08-17-2011 at 12:40 PM.
 
Old 08-17-2011, 01:06 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Maybe you can pass the strings to expect in hex?
Code:
        send "$( echo -n "$OLDPASSWD" | od -An -vtx1 | tr -d '\n' | sed 's/ /\\x/g' )\n"
Kevin Barry
 
1 members found this post helpful.
Old 08-17-2011, 01:32 PM   #3
edwardcode
Member
 
Registered: Apr 2010
Posts: 161

Original Poster
Rep: Reputation: 17
I worked! Just so I can understand what is going on can you explain this command to m?

Thanks
 
Old 08-17-2011, 01:53 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
od is a utility for converting data to other forms; normally it will show groupings of octal numbers and index the ouput lines. The options -An -vtx1 (actually -A, -v, and -t, where n goes with -A and x1 goes with -t) are what's necessary for od to print everything in hex pairs corresponding to each byte. The call to tr removes the newlines added for formatting by od and the call to sed prepends "\x" to each hex pair so expect can understand that it's hex. The -n with echo makes sure it doesn't output a newline, although you could take out -n and also "\n" from the "send" line and it would work the same. To see each step of the process, start with the echo ... | od ... on the command line, then add in tr, then sed.
Kevin Barry
 
1 members found this post helpful.
Old 08-17-2011, 02:32 PM   #5
edwardcode
Member
 
Registered: Apr 2010
Posts: 161

Original Poster
Rep: Reputation: 17
Dude you are awesome. Thanks for the lesson and the help with the command.
 
  


Reply



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
Reading encryption password with bash baltazar3 Linux - Security 3 11-22-2010 07:20 AM
Reading a conf file from a BASH script dinolinux Programming 5 08-03-2005 04:18 AM
Need help reading text file in bash script scilec Programming 3 11-25-2004 06:44 PM
Bash script - reading from text file twantrd Programming 4 11-24-2004 12:38 AM
reading file, bash script marri Programming 3 11-15-2004 09:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:50 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration