Skip to content

Instantly share code, notes, and snippets.

@mosjin
Created November 21, 2018 08:57
Show Gist options
  • Save mosjin/c2398302104ee4924da039ef7a3baa4e to your computer and use it in GitHub Desktop.
Save mosjin/c2398302104ee4924da039ef7a3baa4e to your computer and use it in GitHub Desktop.
Update user password for same user under many CentOSes
#!/bin/bash
#. update passwrods in batch mode
#. root as yourself
currDir=$(dirname $(readlink -f "$0"))
#echo $currDir
source ${currDir}/commonFuncs.bash
user2change="$1"
hostsCfg=~/chpasswdHosts
currentPwdFile=~/chpasswdOld
newPwdFile=~/chpasswdNew
function usage()
{
local hostF=$1
local oldPWDf=$2
local newPWDf=$3
redAlert "Usage: $0 <userName2change>"
redAlert "\n$0 reads:\n\n${hostF} for hosts,\n${oldPWDf} for OLD pwd,\n${newPWDf} for NEW pwd to change to.\
n"
}
if [ ! -f "${hostsCfg}" ] || [ ! -f "${currentPwdFile}" ] || [ ! -f "${newPwdFile}" ]; then
usage "${hostsCfg}" "${currentPwdFile}" "${newPwdFile}"
exit 0
fi
if [ "J" = "J${user2change}" ]; then
usage "${hostsCfg}" "${currentPwdFile}" "${newPwdFile}"
exit 0
fi
newPasswd=$(cat ${newPwdFile} | sed 's/\s*$//')
greenAlert "Will change password for hosts:"
cat ${hostsCfg}
redAlert "\nyes/no?"
yes=$(readYesAnswer)
if [ "yes" != "${yes}" ]; then
echo "ok, do nothing."
exit 0
fi
cat ${hostsCfg} | while read host
do
echo "Changing ${user2change}@${host}."
sshpass -f ${currentPwdFile} ssh ${user2change}@${host} "echo ${user2change}:${newPasswd} | chpasswd" < /dev
/null
redAlert "finished."
#sshpass -f ${newPasswd} ssh ${user2change}@${host} "pwd"
echo
sleep 1
done
echo
echo "Done."
#!/bin/bash
function colorAlert()
{
local color=$1
local content=$2
echo -e "\033[1;${color}m${content}\033[0m"
}
function redAlert()
{
colorAlert 31 "$@"
}
function greenAlert()
{
colorAlert 32 "$@"
}
function getCurrDir()
{
local currDir=$(dirname $(readlink -f "$0" ) )
echo $currDir
}
function echoAlert()
{
colorAlert "" "$@"
}
function readYesAnswer()
{
local answYes="no"
read answer
pat="^[Yy]"
if [ "A${answer}" = "A" ]; then
#echo "OK. do nothing."
answYes="no"
fi
if [[ ${answer} =~ ${pat} ]]; then
#echo "OK. do!"
answYes="yes"
fi
echo $answYes
}
function reverse()
{
local out=()
while [ $# -gt 0 ]; do
out=("$1" "${out[@]}")
shift 1
done
echo "${out[@]}"
}
function rsyncFiles()
{
local pwdF=$1
local files=$2
local user=$3
local ip=$4
local dir=$5
redAlert "PASSWD file:${pwdF}"
echoAlert "files:${files}, \nuser:${user}, \nip:${ip}, \ndir:${dir}"
if [ "J" = "J{$pwdF}" ] || [ "J" = "J${files}" ] || [ "J" = "J${user}" ] || [ "J" = "J${ip}" ] || [ "J" = "J
${dir}" ] ; then
redAlert "Invalid para. exit."
return 0
fi
redAlert "Copy\n\n${files}\n\nto ${user}@${ip}:${dir}, Yes/No?"
yes=$(readYesAnswer)
if [ "no" == "${yes}" ]; then
greenAlert "ok, do nothing."
return 0
fi
sshpass -f ${pwdF} rsync --progress -lptrhK -z --compress-level=9 -C -F --include="*.so" --include="*.a" -e
"ssh -o StrictHostKeyChecking=no" ${files} ${user}@${ip}:${dir}
return 0
}
function getExePid()
{
local exeName3=$1
local pid3=""
if [ "J" != "J${exeName3}" ]; then
#echo "pid3=ps -ef | egrep -i "\b${exeName3}\b" | egrep -v grep | awk '{print $1}' | sed 's|\s*||g')"
pid3=$(ps -ef | egrep -i "\b${exeName3}\b" | egrep -v grep | awk '{print $2}' | sed 's|\s*||g')
fi
#echo "exename: ${exeName3}, pid:${pid3},"
echo ${pid3}
}
function getExePidCurrentUser()
{
local exeName3=$1
local pid3=""
if [ "J" != "J${exeName3}" ]; then
#echo "pid3=ps -ef | egrep -i "\b${exeName3}\b" | egrep -v grep | awk '{print $1}' | sed 's|\s*||g')"
pid3=$(ps ux | egrep -i "\b${exeName3}\b" | egrep -v grep | awk '{print $2}' | sed 's|\s*||g')
fi
#echo "exename: ${exeName3}, pid:${pid3},"
echo ${pid3}
}
function getPidDir()
{
local pid23=$1
local pidDir=""
if [ -f "/proc/${pid23}/exe" ]; then
#echo "pidDir=ls -lt /proc/${pid23}/exe | cut -d'>' -f2 | sed 's/\s*//g' | xargs -i{} readlink -f {}"
pidDir=$(ls -lt /proc/${pid23}/exe | cut -d'>' -f2 | sed 's/\s*//g' | xargs -i{} readlink -f {})
fi
#echo "pidDir:${pidDir},"
echo ${pidDir} | sed 's|\s*||g'
}
#echo $(getPidDir 9199)
#echo $(reverse a1 b2 c3)
#echo "pid:bash: $(getExePid bash),"
#echo "pid:bash:currentUser: $(getExePidCurrentUser 'mingyunJ.sh'),"
#yes=$(readYesAnswer)
#echo "yes:${yes}"
#rsyncFiles ~/cfgPass ../confg root 192.168.3.253 /tmp
#echo $(getCurrDir)
#redAlert "hi jin!"
#greenAlert "green garden!"
@mosjin
Copy link
Author

mosjin commented Nov 21, 2018

chpasswdBatch.bash is a utils to change password for SAME user under many target CentOSes.
Please keep commonFuncs.bash and chpasswdBatch.bash in the SAME dir.

Tips:

  1. You may need install sshpass by:
yum -y install sshpass
  1. You may need install ssh on target and local box by:
yum -y install openssh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment