需要root 免密到需要修改的机器


#!/bin/bash
#2015-11-11
#reset root passwd @by xbzy

User="$1"
LogFile="modify_passwd_"


function Change_passwd()
{

IP=$1 

_passwd=$(head  /dev/urandom |md5sum |head -c 16)
 
ssh  -o ConnectTimeout=15 -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no  $IP   "echo '$User:${_passwd}' | chpasswd" 
test $? -ne 0 && echo -e "\e[1;31mPassword modify Failed\e[0m" &&exit -2||echo -e "Password modify\e[1;32m      [ success ]\e[0m\n"

sed -i "/$IP/d"  $LogFile$User
echo "$(date +%F_%H-%M-%S)      Host:$IP  User:$User      PASS:${_passwd}" |tee -a $LogFile$User

}


function More_IP()
{

#_passwd=$(head  /dev/urandom |md5sum |head -c 16)
List="$1"
for i in `cat $List`
  do
    echo -e "########$i\n"
#       Change_passwd $i  ${_passwd}
        Change_passwd $i
  done
}

function One_IP()
{
#        _passwd=$(head  /dev/urandom |md5sum |head -c 16)
        Change_passwd $1
} 

function Mesage()
{

 echo -e "\e[1;32mUsage:sh $0 username IP 
     :sh $0 username  -f iplist\e[0m"
 exit -1
}

if [ $# -ne 2 ] && [ $# -ne 3 ];then
   Mesage
fi

if [ "$2" == "-f" ] && [ $# -eq 3 ];then
   More_IP $3
elif [ $# -eq 2 ];then
   echo $2 |  grep  -E "([a-z]+\-)([a-z]+\-)([a-z]+[0-9]+\.)([a-z])|([0-9]+\.)([0-9]+\.)([0-9]+\.)[0-9]+"  && One_IP $2||Mesage 
else
   Mesage
fi