We have a number of Red Hat linux servers in our IT environment. I am being asked by my team members to write a script (preferably shell script) to change a user's password on each one of those in a single go, using SSH.
I have tried to find a solution but many of the scripts I found are using Expect. We do not have Expect installed on our servers and the system admins have refused to let us install it. Also, the users do not have root access so passwd --stdin
or chpasswd
cannot be used.
Is there any way a script can be written so that a user can run it and change the password of only his own user on all the servers in a list?
Building on squashbuff's example, I tried the following, which worked well for me:
Security wise, Could be improved to take input without echoing to the screen OR saving the plaintext to disk.
Another possibility: change it manually on one server. Get the encrypted password out of /etc/shadow. Now, do something like this:
Of course, 'encrypted_passwd" is what you got out of /etc/shadow where you manually changed the password. And $HOST_LIST is a list of hosts where you want the password changed. That could be created simply with:
Or perhaps with a file (as others have suggested):
Where the file "host_list.txt" has a list of all the systems where you want the password changed.
Edit: if your version of passwd doesn't support the -p option, you might have the 'usermod' program available. The example above remains the same, simply replace 'passwd' with 'usermod'.
Furthermore, you might consider the useful tool pdsh, which would simplify the above example to something like this:
One last "gotcha" to look out for: the encrypted password likely contains the dollar sign character ('$') as a field separator. You'll probably have to escape those in your for loop or pdsh command (i.e. "$" becomes "\$").
Have you tried App::Unix::RPasswd
You should try pssh (parallel ssh at the same time).
The passmass script (man page) that comes with Expect doesn't require Expect to be installed on the remote machines.
Thought I should put my solution in an answer field - not sure if this should be a part of the question..
OK, I have put together a partially working solution using Dennis' suggestion.
servers.txt looks like:
I am using:
This produces:
So here, I still need to type my old password once for each server. Can this be avoided?