公司linux服務200多臺,平常運維須要每季度修改密碼,很是麻煩,並且因爲作了安全加固,禁止root用戶遠程登陸,更增長了複雜度。現採用以下方法能夠解決:
一、管理機commuser生成的公鑰文件.ssh/id_rsa.pub,放到遠程機root的.ssh/authorized_keys中,管理機commuser用戶登陸ssh root@遠程機無需密碼;管理機root用戶登陸遠程機須要密碼。
二、自動修改密碼腳本:
1)管理機生成公鑰文件,copy到遠程機,有多少遠程機,就須要copy臺;同時遠程機必須安裝好expect。
2)遠程機/root/pwd目錄下創建以下腳本chg_pwd.sh,附上755執行權限。
chg_pwd.sh腳本以下:linux
#!/usr/expect/bin/expectshell
set oldrootpwd [lindex $argv 0]
set oldoperpwd [lindex $argv 1]
set newrootpwd [lindex $argv 2]
set newoperpwd [lindex $argv 3]安全
#-----login timed out or refused -----
expect {
"timed out" exit
"refused" exit
Connect
}bash
#expect "$"運維
#-----first login-----
expect {
"(yes/no)?" {
send "yes\n"
}
}
#expect "#"ssh
#-----login root-----
spawn su - root
expect "password:"
send "$oldrootpwd\r"
expect "#"ide
#-----set root password -----
send "passwd root\r"
expect "password:"
send "$newrootpwd\r"
expect "password:"
send "$newrootpwd\r"
expect "#"優化
#-----set commuser password -----
send "passwd commuser\r"
expect "password:"
send "$newoperpwd\r"
expect "password:"
send "$newoperpwd\r"
expect "#"
send "exit\r"spa
interact.net
3)管理機在/home/commuser/.ssh/目錄下創建auto_pwd.sh,list.demo,log.txt 3個文件。
auto_pwd.sh
ssh 10.60.160.254 /usr/expect/bin/chg_pwd.sh testpasswd testpasswd testpasswd testpasswd
ssh 10.60.10.10 /usr/expect/bin/chg_pwd.sh testpasswd testpasswd testpasswd testpasswd
ssh 10.60.160.156 /usr/expect/bin/chg_pwd.sh testpasswd testpasswd testpasswd testpasswd
ssh root@10.60.160.125 /usr/expect/bin/chg_pwd.sh testpasswd ------------- testpasswd -------------
ssh 10.60.160.252 -------------------------- testpasswd testpasswd testpasswd testpasswd
list.demo
shell ip address shell remotee path oldrootpwd oldoperpwd newrootpwd newoperpwd note
ssh 10.60.160.254 /usr/expect/bin/chg_pwd.sh testpasswd testpasswd testpasswd testpasswd ip esxit no ssh
ssh 10.60.10.10 /usr/expect/bin/chg_pwd.sh testpasswd testpasswd testpasswd testpasswd no ip
ssh 10.60.160.156 /usr/expect/bin/chg_pwd.sh testpasswd testpasswd testpasswd testpasswd ok
ssh root@10.60.160.125 /usr/expect/bin/chg_pwd.sh testpasswd ------------- testpasswd ------------- no testoper
ssh 10.60.160.252 -------------------------- testpasswd testpasswd testpasswd testpasswd no expect
auto_pwd.sh附上755執行權限,修改密碼時候commuser登陸管理機執行./auto_pwd.sh > log.txt即可。執行無權限是給/usr/tcl usr/expect 755權限
補充:1)對遠程機不能進行安全加固只有root用戶的,複製管理機commuser的公鑰文件過去也是能夠ssh登陸,只需優化的是對chg_pwd.sh判斷入參是否有old commuser用戶。
2)expect安裝參考:
Expect是在Tcl基礎上建立起來的,它還提供了一些Tcl所沒有的命令,它能夠用來作一些linux下沒法作到交互的一些命令操做,在遠程管 理方面發揮很大的做用。
spawn命令激活一個Unix程序來進行交互式的運行。
send命令向進程發送字符串。
expect 命令等待進程的某些字符串。
expect支持正規表達式並能同時等待多個字符串,並對每個字符串執行不一樣的操做.
A. Tcl 安裝
主頁: http://www.tcl.tk
下載地址: http://www.tcl.tk/software/tcltk/downloadnow84.tml
1.下載源碼包
wget https://nchc.dl.sourceforge.net/project/tcl/Tcl/8.6.8/tcl8.6.8-src.tar.gz
2.解壓縮源碼包
tar xfvz tcl8.6.8-src.tar.gz
3.安裝配置
cd tcl8.6.8/unix
./configure --prefix=/usr/tcl --enable-shared
make && make install
安裝完畢之後,進入tcl源代碼的根目錄,把子目錄unix下面的tclUnixPort.h copy到子目錄generic中。cp tclUnixPort.h ../generic
暫時不要刪除tcl源代碼,由於expect的安裝過程還須要用。
B. expect 安裝 (需Tcl的庫)
主頁: https://sourceforge.net/projects/expect/
1.下載源碼包
wget https://nchc.dl.sourceforge.net/project/expect/Expect/5.45.3/expect5.45.3.tar.gz
2.解壓縮源碼包
tar xzvf expect5.45.3.tar.gz
3.安裝配置
cd expect5.45.3
./configure --prefix=/usr/expect --with-tcl=/usr/tcl/lib --with-tclinclude=../tcl8.6.8/generic
make && make install
ln -s /usr/tcl/bin/expect /usr/expect/bin/expect 此步驟可不需執行
自動修改密碼方式:
採用此方式無需創建ssh互信,但仍是須要安裝expect,安裝後把autochg_pwd.sh放置在/root/pwd下,並chmod 755 賦權,計劃任務crontab -e 創建一個每季度第一天凌晨3點的定時任務:00 3 1 1,4,7,10 * /bin/sh /root/pwd/autochg_pwd.sh
autochg_pwd.sh腳本以下:
#!/bin/bash
ystr=date +%Y
mstr=date +%m
dstr=date +%d
if [ "$mstr" == "01" ];then
mstr="!"
fi
if [ "$mstr" == "02" ];then
mstr="!"
fi
if [ "$mstr" == "03" ];then
mstr="!"
fi
if [ "$mstr" == "04" ];thenbr/>mstr="@"
fi
if [ "$mstr" == "05" ];thenbr/>mstr="@"
fi
if [ "$mstr" == "06" ];thenbr/>mstr="@"
fi
if [ "$mstr" == "07" ];then
mstr="#"
fi
if [ "$mstr" == "08" ];then
mstr="#"
fi
if [ "$mstr" == "09" ];then
mstr="#"
fi
if [ "$mstr" == "10" ];then
mstr="$"
fi
if [ "$mstr" == "11" ];then
mstr="$"
fi
if [ "$mstr" == "12" ];then
mstr="$"
fi
if [ "$mstr" == "!" ];then
oldmstr="$"
fi
if [ "$mstr" == "@" ];then
mstr="!"
fi
if [ "$mstr" == "#" ];thenbr/>mstr="@"
fi
if [ "$mstr" == "$" ];then
mstr="#"
fi
oldrootpwd="testpwd"$ystr$oldmstr
oldoperpwd="testpwd"$ystr$oldmstr
newrootpwd="testpwd"$ystr$mstr
newoperpwd="testpwd"$ystr$mstr
/usr/expect/bin/expect <<-EOF
#-----login root-----
spawn su - root
#expect "password:"
#send "$oldrootpwd\r"
expect "#"
#-----set root password -----
send "passwd root\r"
expect "password:"
send "$newrootpwd\r"
expect "password:"
send "$newrootpwd\r"
expect "#"
#-----set testoper password -----
send "passwd testoper\r"
expect "password:"
send "$newoperpwd\r"
expect "password:"
send "$newoperpwd\r"
expect "#"
send "exit\r"
interact
expect eofEOF