通过shell脚本实现批量更改密码
一、建立信任關系
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
?
使用mkpasswd產生隨機密碼
?[root@manage .ssh]# mkpasswd -l 32
megVjmbpo8tw2zn$nmcgguensluxayCw
?
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 /bak/scripts/ip_list.txt`??????????????? #導入遠程要修改主機的IP
do
#========================= 創建遠程主機密碼 ===================================#
echo "megVjmbpo8tw2zn$nmcgguensluxayCw" > R_PWD.txt????? # 可以寫成自己需要的密碼
R_PWD=`cat 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" >> /var/log/R_Server.log
else
?? echo -e "$(date "+%Y-%m-%d %H:%M:%S")\t${IP} R_PWD.txt is create fail\tplease check!\t" >> /var/log/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" >> /var/log/M_pass.log
fi
done
?
最后執行shell腳本?
轉載于:https://blog.51cto.com/liweizhong/565789
總結
以上是生活随笔為你收集整理的通过shell脚本实现批量更改密码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NAGIOS安装指南
- 下一篇: 访问FTP (转)