批量修改遠程linux伺服器密碼

來源:互聯網
上載者:User

  #!/bin/bash

  # BY kerryhu

  # MAIL:king_819@163.com

  # 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.