LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-05-2008, 10:34 AM   #1
rahmad
Member
 
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65

Rep: Reputation: 15
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
 
Old 08-05-2008, 10:39 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
How about your experience with shell scripting? What have you tried so far?
 
Old 08-05-2008, 11:05 AM   #3
rahmad
Member
 
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65

Original Poster
Rep: Reputation: 15
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
 
Old 08-05-2008, 11:26 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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.
 
Old 08-05-2008, 12:15 PM   #5
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
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.
 
Old 08-05-2008, 12:32 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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.
 
Old 08-05-2008, 05:50 PM   #7
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
Ah nice, didn't know about the --stdin option, thanks for the great tip!
 
Old 08-05-2008, 05:51 PM   #8
rahmad
Member
 
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65

Original Poster
Rep: Reputation: 15
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
 
Old 08-06-2008, 01:24 AM   #9
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by rahmad View Post
I would appreciate if you send me the complete script
I have already done... and kenoshi too...
 
Old 08-06-2008, 03:13 PM   #10
rahmad
Member
 
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65

Original Poster
Rep: Reputation: 15
Thumbs up

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
 
Old 08-06-2008, 05:55 PM   #11
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
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
 
Old 08-06-2008, 11:50 PM   #12
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,094
Blog Entries: 2

Rep: Reputation: 111Reputation: 111
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.
 
Old 09-01-2008, 06:12 PM   #13
Gux
LQ Newbie
 
Registered: Sep 2008
Posts: 1

Rep: Reputation: 0
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 View Post
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
 
Old 09-02-2008, 04:04 AM   #14
rahmad
Member
 
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65

Original Poster
Rep: Reputation: 15
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
 
  


Reply

Tags
code, dat, done, executing, file, help, option, passwd, password, paste, script, scripting, setting, stdin, tried, unrecognized, user


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
Menu bashed shell script for users shell 0.o Linux - Server 6 02-14-2013 06:00 AM
shell script help for users cmontr Programming 11 11-15-2007 05:09 PM
Request : set passwords for many users [user accounts exist] using a shell script bv_uma Linux - Software 3 08-19-2006 09:01 AM
Reset all Passwords nickyboy Linux - Newbie 3 04-22-2006 03:22 PM
Need shell script to set passwords for already created users naren_0101bits Programming 2 08-28-2005 02:02 PM


All times are GMT -5. The time now is 10:37 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