技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機純凈版,64位旗艦版,綠色軟件,免費軟件下載基地!

當(dāng)前位置:主頁 > 教程 > 服務(wù)器類 >

批量修改遠(yuǎn)程linux服務(wù)器密碼

來源:技術(shù)員聯(lián)盟┆發(fā)布時間:2019-05-07 06:06┆點擊:

  # MAIL:king_819@163.com

  # BLOG:

  # Please manual operation yum of before Operation.....

  一、建立信任關(guān)系

  192.168.9.203 為管理機

  192.168.9.201 192.168.9.202 為遠(yuǎn)程linux服務(wù)器

  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、將管理機上的公鑰傳送到各遠(yuǎn)程服務(wù)器

  如遠(yuǎn)程服務(wù)器更改了默認(rèn)的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

  管理機與遠(yuǎn)程主機信任關(guān)系建立完畢

  二、通過shell腳本批量修改遠(yuǎn)程服務(wù)器密碼

  如果要調(diào)用mkpasswd就得安裝expect,使用mkpasswd可以隨機產(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)

  比如說你要指定一個長度為8,而且至少有三個大寫字母的密碼,那么可以這樣輸入:

  mkpasswd -l 8 - C 3,好了,密碼就會按你的要求隨機產(chǎn)生了

  yum -y install expect

  ip_list.txt為遠(yuǎn)程服務(wù)器IP列表

  [root@manage .ssh]# cat ip_list.txt

  192.168.9.201

  192.168.9.202

  如果遠(yuǎn)程服務(wù)器修改了默認(rèn)ssh的端口號,就使用ssh -p 17173,17173為端口號

  #!/bin/bash

  #============== Though ssh remote server ,auto modify ROOT passwd =============#

  for IP in `cat /root/ip_list.txt` #導(dǎo)入遠(yuǎn)程要修改主機的IP

  do

  #========================= 創(chuàng)建遠(yuǎn)程主機密碼 ==========================#

  TMP_PWD=`mkpasswd -l 8 -C 3`

  R_PWD=`echo ${IP}_${TMP_PWD}`

  echo "${IP}_${TMP_PWD}" > R_PWD.txt

  #=========================== 修改遠(yuǎn)程主機密碼 ========================#

  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