#!/bin/bash
# BY kerryhu
# MAIL:[email protected]
# BLOG:http://kerry.blog.51cto.com
# Please manual operation yum of before Operation.....
一、建立信任關系
192.168.9.203 為管理機
192.168.9.201 192.168.9.202 為遠程linux服務器
1、在管理機生成證書、
[root@manage ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa. (私鑰)
Your public key has been saved in /root/.ssh/id_rsa.pub. (公鑰)
The key fingerprint is:
36:ec:fc:db:b0:7f:81:7e:d0:1d:36:5e:29:dd:5b:a0
2、將管理機上的公鑰傳送到各遠程服務器
如遠程服務器更改了默認的ssh端口號,就使用scp -P 17173,17173為端口號
[root@manage .ssh]# scp id_rsa.pub 192.168.9.201:/root/.ssh/authorized_keys
[root@manage .ssh]# scp id_rsa.pub 192.168.9.202:/root/.ssh/authorized_keys
管理機與遠程主機信任關系建立完畢
二、通過shell腳本批量修改遠程服務器密碼
如果要調用mkpasswd就得安裝expect,使用mkpasswd可以隨機產生密碼
usage: mkpasswd [args] [user]
where arguments are:
-l # (length of password, default = 10)
-d # (min # of digits, default = 2)
-c # (min # of lowercase chars, default = 2)
-C # (min # of uppercase chars, default = 2)
-s # (min # of special chars, default = 1)
-v (verbose, show passwd interaction)
-p prog (program to set password, default = passwd)
比如說你要指定一個長度為8,而且至少有三個大寫字母的密碼,那麼可以這樣輸入:
mkpasswd -l 8 - C 3,好了,密碼就會按你的要求隨機產生了
yum -y install expect
ip_list.txt為遠程服務器IP列表
[root@manage .ssh]# cat ip_list.txt
192.168.9.201
192.168.9.202
如果遠程服務器修改了默認ssh的端口號,就使用ssh -p 17173,17173為端口號
#!/bin/bash
#============== Though ssh remote server ,auto modify ROOT passwd =============#
for IP in `cat /root/ip_list.txt` #導入遠程要修改主機的IP
do
#========================= 創建遠程主機密碼 ==========================#
TMP_PWD=`mkpasswd -l 8 -C 3`
R_PWD=`echo ${IP}_${TMP_PWD}`
echo "${IP}_${TMP_PWD}" > R_PWD.txt
#=========================== 修改遠程主機密碼 ========================#
if [ $? = 0 ] ; then
ssh $IP passwd root --stdin < R_PWD.txt
echo -e "$(date "+%Y-%m-%d %H:%M:%S")t${IP}t${R_PWD}t" >> R_Server.log
else
echo -e "$(date "+%Y-%m-%d %H:%M:%S")t${IP} R_PWD.txt is create failtplease check!t" >> M_pass.log
fi
if [ $? = 0 ] ; then
echo -e "$(date "+%Y-%m-%d %H:%M:%S")tThe ${IP} passwd is modify OKt" >> M_pass.log
else
echo -e "$(date "+%Y-%m-%d %H:%M:%S")tThe ${IP} passwd is modify failtplease check!t" >> M_pass.log
fi
done