Friday, January 13, 2012

Change your own password in multiple Linux servers

All Unix administrations are searching the web to find out automation method to do their work. Most of the system administrator getting job as change password, creating users, deleting user or change ownership etc.. If you read basic Unix tutorial you can do these job without others support. Problem or pain comes when you have to repeat same thing on range of servers.

As example lets see you want to change password on one user on 100 servers. Well very easy...
login each server and execute 'passwd' command. :)

Here I try to automate this job. Anyway changing password is manual intervention. Here all servers required SSH enable and if you are not configured authentication keys you have login to each server
First you create list of servers and save it as server_list.csv (very easy when you use excel sheet)

then create script
#!/bin/bash
#####################################################
# #
# CHANGE PASSWORD ON REMOTE HOST #
# Version 1.2 #
# Created by Aruna #
# Created Date 21/12/2011 #
# Last Modified Date 22/12/2011 #
# INSTRUCTIONS- #
# Save both script and server_list.csv file in same #
# location #
#####################################################
host_name=""
file_name="server_list.csv"
echo -n "Enter username you want to change the password on the list of server : "
read username
echo -n " The username you entered is "$username". Is it correct (y/n)?"
read answer
if [ $answer = y ]
then
for i in $(cat $file_name);
do
hostname=$(echo $i| cut -f1 -d',')
ssh $username@$hostname 'passwd'
echo ">>>>>>>>> SUCCESSFULLY CHANGED THE PASSWORD >>>>>>>>>>"
done
else
echo "!!!!!!!!! your answer does not match to continue. Thank you!!!!!!!!!!!!"
exit 1
fi
exit 0
You have to know your current password to execute this script.

No comments:

Post a Comment