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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-05-2008, 10:34 AM
|
#1
|
|
Member
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65
Rep:
|
Reset users passwords using shell script
hi all,
I need someone please to provide me with shell/bash script that reset users' passwords. this script should read the usernames list from a file and the passwords list from another file. the usernames and passwords files format is entry per line.
please help!!
thx
rahmad
|
|
|
|
08-05-2008, 10:39 AM
|
#2
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
How about your experience with shell scripting? What have you tried so far?
|
|
|
|
08-05-2008, 11:05 AM
|
#3
|
|
Member
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65
Original Poster
Rep:
|
I am not that good in writing scripts. I think I need two nested loops.
I tried that before a long time but it didnt work. now I need it urgently and quickly because that I posted it here now.
please send me the script if you know it?
thanks in advance
|
|
|
|
08-05-2008, 11:26 AM
|
#4
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
Using two nested loops is more difficult, since you have to read both files one line at a time. You can previously merge the two files side by side, then read the two fields in a single while loop. You can also do this on the fly. Suppose you have two files called users and passwd:
Code:
$ cat users
user1
user2
user3
$ cat passwd
passwd1
passwd2
passwd3
you can pass the output of the paste command, which merges the files side by side, to the while loop using process substitution. The syntax is:
Code:
#!/bin/bash
while read user passwd
do
echo user is $user password is $passwd
done < <(paste -d\ users passwd)
As you can see the read statement can read two (or more) variables simultaneously. Running this script gives
Code:
$ ./test.sh
user is user1 password is passwd1
user is user2 password is passwd2
user is user3 password is passwd3
Then you have only to write the proper command to reset users' passwords. Hope this helps.
|
|
|
|
08-05-2008, 12:15 PM
|
#5
|
|
Member
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159
Rep:
|
Why does this sound like a homework assignment  Anyway, to add to colucix's excellent advice, use an expect script to actually change the password. If you don't have it, use the package management utility for your distro and install it.
Once that's done, create an expect script called changepw.exp as follows:
Code:
#!/usr/bin/expect -f
set username [lrange $argv 0 0]
set password [lrange $argv 1 1]
set timeout -1
spawn passwd $username
expect "*?assword:*"
send -- "$password\r"
expect "Retype*"
send -- "$password\r"
expect eof
And just change colucix's example as follows:
Code:
#!/bin/bash
while read user passwd
do
changepw.exp $user $passwd
done < <(paste -d\ users passwd)
Should work just fine. Hope this helps.
Last edited by kenoshi; 08-05-2008 at 12:16 PM.
|
|
|
|
08-05-2008, 12:32 PM
|
#6
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
Good call, kenoshi. However I think the --stdin option of passwd is enough. I would try something like
Code:
echo "$passwd" | passwd --stdin "$user"
It should work fine as well.
Just a side note: if the passwords contain some special characters, most likely they must be escaped. For example a literal backslash \ should be escaped with another backslash \\ so that the script can interpret it correctly.
|
|
|
|
08-05-2008, 05:50 PM
|
#7
|
|
Member
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159
Rep:
|
Ah nice, didn't know about the --stdin option, thanks for the great tip!
|
|
|
|
08-05-2008, 05:51 PM
|
#8
|
|
Member
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65
Original Poster
Rep:
|
hi colucix,
after using the merged file, how can I extract from each line the username and password and put them on two different place holders in order to use them in echo "$passwd" | passwd --stdin "$user"
I guess we may need to use awk, is that right?? I would appreciate if you send me the complete script
thx
|
|
|
|
08-06-2008, 01:24 AM
|
#9
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
Quote:
Originally Posted by rahmad
I would appreciate if you send me the complete script 
|
I have already done... and kenoshi too... 
|
|
|
|
08-06-2008, 03:13 PM
|
#10
|
|
Member
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65
Original Poster
Rep:
|
thanks, its done now
but you made a mistake when you put -d\ in done < <(paste -d\ users passwd)
for all newbie this is the complete script:
while read user passwd
do
echo "$passwd" | passwd --stdin "$user"
done < <(paste user passwd)
cheers
|
|
|
|
08-06-2008, 05:55 PM
|
#11
|
|
Member
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159
Rep:
|
No he didn't, its 2 spaces after -d\ not one, he's replacing tab with space as delimiter. Its the same as saying:
-d" "
His way is more elegant 
|
|
|
|
08-06-2008, 11:50 PM
|
#12
|
|
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,051
Rep:
|
well if you aren't tied to an actual shell script perl has modules on cpan for modifying password entries and I'm pretty sure python has something similar.
|
|
|
|
09-01-2008, 06:12 PM
|
#13
|
|
LQ Newbie
Registered: Sep 2008
Posts: 1
Rep:
|
Reset users passwords using shell script
Hi, Rahmad and the rest:
I am, in fact, totally newbie to linux. I'm trying to add a bunch of users using scripts but I got stuck setting users' password via script and I found this thread. Could you please be more specific on your code? Is that a .sh file? Did you use a separate files or a single one for the user/pass pairs?
Thanks beforehand,
Gux.
Quote:
Originally Posted by rahmad
thanks, its done now
but you made a mistake when you put -d\ in done < <(paste -d\ users passwd)
for all newbie this is the complete script:
while read user passwd
do
echo "$passwd" | passwd --stdin "$user"
done < <(paste user passwd)
cheers
|
|
|
|
|
09-02-2008, 04:04 AM
|
#14
|
|
Member
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65
Original Poster
Rep:
|
hi
you have to put the code below in .sh file and then make the file executable by running chmod u+x file.sh
you should have also two another files in the same file.sh's location one for users and the other for passwords.
I suggest you to do a little reading about bash scripting
good luck
|
|
|
|
|
Tags
|
code, dat, done, executing, file, help, option, passwd, password, paste, script, scripting, setting, stdin, tried, unrecognized, user  |
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:18 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|