Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 09-05-2013, 02:11 AM   #1
LQ Newbie
Registered: Jan 2009
Posts: 15

Rep: Reputation: 0
Need help in creating a script which will change password of user on multiple servers

Hi Friends,

Please help me in creating a script which will help in resettting the password of a user on multiple servers.

The account which will be used have a sudo previlages and hence cannot execute the script, as unable to become root by supplying the credentials.

Here is the code which i have done but its not working.

# Password Change Script
echo "Enter User name with Sudo Previleges which will be used to change the passwords on remote Servers"
read ADMIN
echo "Enter Password to connect Sudo Previleges which will be used to change the passwords on remote Servers"
read PASS
echo " "
echo "Enter User name Whose passwords needs to be changed on remote Servers"
echo "Enter NEW password for $TARGET user "


for i in $(cat $file_name);
hostname=$(echo $i| cut -f1 -d',')

ssh -t $login_id@$hostname 'echo $PASS | sudo -S |`echo $TARGETPASS | openssl passwd -1 -stdin`' read $TARGET'

echo ">>>>>>>>> PASSWORD CHANGED SUCCESSFULLY >>>>>>>>>>"
exit 0
Old 09-05-2013, 05:02 AM   #2
Registered: Feb 2006
Posts: 47

Rep: Reputation: 29
Your best bet is to use expect (

Expect is a program specifically designed for this type of interaction.

If you run autoexpect and enter all the necessary commands and info to change one password, it will generate a script which you can edit and use for the general case by putting in variables in the appropriate places - e.g. for usernames and passwords.

here is how i would do it:
% mkdir workspace
% cd workspace
% autoexpect ssh user@hostname
autoexpect will start the ssh session and record all input/output to script.exp. Do all the steps to change password here. Then exit the ssh session. This will create script.exp which you can edit.

NB. you don't need or want the ssh -t option with expect.

Then call your expect script from within your loop, passing it the hostname, username, etc.

Hope this helps.
Old 09-05-2013, 06:12 AM   #3
LQ Newbie
Registered: Jan 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks @bonnydeal

Will try the except one, but for above passing arguments via ssh can you help me out.

ssh -t user@server 'echo password | sudo -S /usr/sbin/usermod -p `echo ssss | openssl passwd -1 -stdin` 'test''
password of sudo user New password User whose password needs to be changed.

this works with normal command line, but when used in the script above the varibales doesnt seems to be passed correctly and hence password is not changed.
Old 09-05-2013, 12:57 PM   #4
LQ Newbie
Registered: Sep 2013
Location: UK
Distribution: ubuntu 13.04
Posts: 3

Rep: Reputation: Disabled
Please see the reply to your other thread at
for my reply and explanation there.
Old 09-06-2013, 04:48 AM   #5
Registered: Feb 2006
Posts: 47

Rep: Reputation: 29
ssh needs a method of authentication
One way is shared keys.
Another is username/password combination where ssh will prompt you for a password. (there are others but not important here, I think).

In your script you do not supply a password to ssh,
So if you have not set up shared keys your ssh session will not start.

If you do not want to set up shared keys, then using expect is the best way to supply a password.

Hope this helps
Old 09-07-2013, 04:04 AM   #6
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,880

Rep: Reputation: 1489Reputation: 1489Reputation: 1489Reputation: 1489Reputation: 1489Reputation: 1489Reputation: 1489Reputation: 1489Reputation: 1489Reputation: 1489
Depending on the number of servers (more than 3?) it may be better to go to a centralized password/account management (LDAP).
1 members found this post helpful.


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
Creating user accounts from a csv file, force to change password and create alias rojasm Linux - Newbie 13 04-02-2013 11:19 PM
[SOLVED] Shell script to change password for current user treznik Linux - General 7 09-12-2010 05:17 PM
expect script to change user password sherimm Linux - Software 1 03-13-2009 06:36 AM
Can my shell script change the password of user ? prabhatsoni Linux - Software 1 05-27-2006 03:06 AM
what is the command to make a user change their password after creating a new user? naweenio Linux - Newbie 7 01-05-2005 08:07 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:51 AM.

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