- 論壇徽章:
- 0
|
批量修改遠(yuǎn)程linux服務(wù)器密碼
一、建立信任關(guān)系
192.168.9.203 為管理機(jī)
192.168.9.201 192.168.9.202 為遠(yuǎn)程linux服務(wù)器
1、在管理機(jī)生成證書(shū)、- [root@manage ~]# ssh-keygen -t rsa
復(fù)制代碼 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、將管理機(jī)上的公鑰傳送到各遠(yuǎn)程服務(wù)器
如遠(yuǎn)程服務(wù)器更改了默認(rèn)的ssh端口號(hào),就使用scp -P 17173,17173為端口號(hào)- [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
復(fù)制代碼 管理機(jī)與遠(yuǎn)程主機(jī)信任關(guān)系建立完畢
二、通過(guò)shell腳本批量修改遠(yuǎn)程服務(wù)器密碼
如果要調(diào)用mkpasswd就得安裝expect,使用mkpasswd可以隨機(jī)產(chǎn)生密碼
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)
比如說(shuō)你要指定一個(gè)長(zhǎng)度為8,而且至少有三個(gè)大寫(xiě)字母的密碼,那么可以這樣輸入:
mkpasswd -l 8 - C 3,好了,密碼就會(huì)按你的要求隨機(jī)產(chǎn)生了ip_list.txt為遠(yuǎn)程服務(wù)器IP列表- [root@manage .ssh]# cat ip_list.txt
復(fù)制代碼 192.168.9.201
192.168.9.202
如果遠(yuǎn)程服務(wù)器修改了默認(rèn)ssh的端口號(hào),就使用ssh -p 17173,17173為端口號(hào)- #!/bin/bash
-
- #============== Though ssh remote server ,auto modify ROOT passwd =============#
- for IP in `cat /root/ip_list.txt` #導(dǎo)入遠(yuǎn)程要修改主機(jī)的IP
-
- do
- #========================= 創(chuàng)建遠(yuǎn)程主機(jī)密碼 ==========================#
-
- TMP_PWD=`mkpasswd -l 8 -C 3`
-
- R_PWD=`echo ${IP}_${TMP_PWD}`
-
- echo "${IP}_${TMP_PWD}" > R_PWD.txt
-
-
-
- #=========================== 修改遠(yuǎn)程主機(jī)密碼 ========================#
-
- 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 fail\tplease check!\t" >> M_pass.log
-
- fi
-
- if [ $? = 0 ] ; then
-
- echo -e "$(date "+%Y-%m-%d %H:%M:%S")\tThe ${IP} passwd is modify OK\t" >> M_pass.log
-
- else
-
- echo -e "$(date "+%Y-%m-%d %H:%M:%S")\tThe ${IP} passwd is modify fail\tplease check!\t" >> M_pass.log
-
- fi
-
- done
復(fù)制代碼 |
|