LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Tags used in this thread
Popular LQ Tags , , , , , , , , ,

Reply
 
Thread Tools
Old 08-05-2008, 11:34 AM   #1
rahmad
LQ Newbie
 
Registered: Aug 2007
Posts: 28
Thanked: 0
Reset users passwords using shell script


[Log in to get rid of this advertisement]
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
rahmad is offline     Reply With Quote
Old 08-05-2008, 11:39 AM   #2
colucix
Guru
 
Registered: Sep 2003
Location: Bologna, Italia
Distribution: OpenSUSE 11.1 CentOS 5.4 VectorLinux 6.0
Posts: 5,127
Thanked: 463
How about your experience with shell scripting? What have you tried so far?
colucix is offline     Reply With Quote
Old 08-05-2008, 12:05 PM   #3
rahmad
LQ Newbie
 
Registered: Aug 2007
Posts: 28
Thanked: 0

Original Poster
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
rahmad is offline     Reply With Quote
Old 08-05-2008, 12:26 PM   #4
colucix
Guru
 
Registered: Sep 2003
Location: Bologna, Italia
Distribution: OpenSUSE 11.1 CentOS 5.4 VectorLinux 6.0
Posts: 5,127
Thanked: 463
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.
colucix is offline     Reply With Quote
Old 08-05-2008, 01:15 PM   #5
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159
Thanked: 2
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 01:16 PM..
kenoshi is offline     Reply With Quote
Old 08-05-2008, 01:32 PM   #6
colucix
Guru
 
Registered: Sep 2003
Location: Bologna, Italia
Distribution: OpenSUSE 11.1 CentOS 5.4 VectorLinux 6.0
Posts: 5,127
Thanked: 463
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.
colucix is offline     Reply With Quote
Old 08-05-2008, 06:50 PM   #7
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159
Thanked: 2
Ah nice, didn't know about the --stdin option, thanks for the great tip!
kenoshi is offline     Reply With Quote
Old 08-05-2008, 06:51 PM   #8
rahmad
LQ Newbie
 
Registered: Aug 2007
Posts: 28
Thanked: 0

Original Poster
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
rahmad is offline     Reply With Quote
Old 08-06-2008, 02:24 AM   #9
colucix
Guru
 
Registered: Sep 2003
Location: Bologna, Italia
Distribution: OpenSUSE 11.1 CentOS 5.4 VectorLinux 6.0
Posts: 5,127
Thanked: 463
Quote:
Originally Posted by rahmad View Post
I would appreciate if you send me the complete script
I have already done... and kenoshi too...
colucix is offline     Reply With Quote
Old 08-06-2008, 04:13 PM   #10
rahmad
LQ Newbie
 
Registered: Aug 2007
Posts: 28
Thanked: 0

Original Poster
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
rahmad is offline     Reply With Quote
Old 08-06-2008, 06:55 PM   #11
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159
Thanked: 2
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
kenoshi is offline     Reply With Quote
Old 08-07-2008, 12:50 AM   #12
estabroo
Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 657
Blog Entries: 2
Thanked: 18
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.
estabroo is offline     Reply With Quote
Old 09-01-2008, 07:12 PM   #13
Gux
LQ Newbie
 
Registered: Sep 2008
Posts: 1
Thanked: 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
Gux is offline  
Tag This Post , , , , , , , , ,
Reply With Quote
Old 09-02-2008, 05:04 AM   #14
rahmad
LQ Newbie
 
Registered: Aug 2007
Posts: 28
Thanked: 0

Original Poster
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
rahmad is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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


All times are GMT -5. The time now is 10:16 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration