Batch modify the remote Linux server password

  

#!/bin/bash # BY kerryhu # MAIL:[email protected] # BLOG:http://kerry.blog.51cto.com # Please manual operation yum of before Operation. ....
First, establish a trust relationship 192.168.9.203 for the management machine 192.168.9.201 192.168.9.202 for the remote Linux server 1, generate a certificate in the management machine, [root@manage ~]# ssh-keygen -t rsa Generating Enter/express saf key. /id_rsa. (Private Key) Your public key has been saved in /root/.ssh/id_rsa.pub. (public key) The key fingerprint is: 36:ec:fc:db:b0:7f:81:7e:d0 :1d:36:5e:29:dd:5b:a0 2. Transfer the public key on the management machine to each remote server. If the remote server changes the default ssh port number, use scp -P 17173, 17173 as the port number. ,null,null,3],Root@manage .ssh]# scp id_rsa.pub 192.168.9.201:/root/.ssh/authorized_keys [root@ma Nage .ssh]# scp id_rsa.pub 192.168.9.202:/root/.ssh/authorized_keys The trust relationship between the management machine and the remote host is established. 2. Modify the remote server password in batches through the shell script. If you want to call mkpasswd, you must install expect, use mkpasswd. Passwords can be randomly generated 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) For example, if you want to specify a password with a length of 8 and at least three uppercase letters, you can type: mkpasswd -l 8 - C 3, well, the password will be randomly generated according to your requirements. Yum -y install expect ip_list.txt is the remote server IP list [root@manage .ssh]# cat ip_list.txt 192.168.9.201 192.168.9.202 If the remote service The server modified the default ssh port number, using ssh -p 17173, 17173 for the port number #!/bin/bash #============== Though ssh remote server , auto modify ROOT Passwd =============# for IP in `cat /root/ip_list.txt` #import remote to modify the host's IP do #============= ============ Create Remote Host Password ==================================================================================================== -C 3` R_PWD=`echo ${IP}_${TMP_PWD}` echo "${IP}_${TMP_PWD}" > R_PWD.txt #============ =============== Modify Remote Host Password ================================= 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

Copyright © Windows knowledge All Rights Reserved