메뉴 닫기

expect를 사용하여 다량의 서버 패스워드 변경

우리 회사처럼 데이터센터를 운영할려면 isms(정보보호인증심사)를 주기적으로 받아야한다.

그 항목중에 서버의 패스워드를 주기적으로 바꿔야하는 부분이 있는데 운영하는 서버가 수천대에
달하여 시간적, 인적 비용이 많이 들어간다.

이에 expect를 사용하여 자동으로 다량의 서버 패스워드 변경하는걸 구축해보자.

먼저 스크립트를 실행하고 로그를 저장할 디렉토리를 만듭니다.

root@controller:~# mkdir /root/change_pwd
root@controller:~# cd change_pwd/

expect를 사용 하기 위해선 expect를 다운로드 받아야 하는데 현재 운영중인 서버 ubunt 14.04에서는
apt-get 으로 쉽게 다운받을수 있습니다.

root@controller:~/change_pwd# apt-get install expect
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libtcl8.6
Suggested packages:
  tcl8.6
The following NEW packages will be installed:
  expect libtcl8.6
0 upgraded, 2 newly installed, 0 to remove and 130 not upgraded.
Need to get 992 kB of archives.
After this operation, 4115 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://kr.archive.ubuntu.com/ubuntu/ trusty/main libtcl8.6 amd64 8.6.1-4ubuntu1 [841 kB]
Get:2 http://kr.archive.ubuntu.com/ubuntu/ trusty/main expect amd64 5.45-5ubuntu1 [152 kB]
Fetched 992 kB in 0s (2560 kB/s)
Selecting previously unselected package libtcl8.6:amd64.
(Reading database ... 130762 files and directories currently installed.)
Preparing to unpack .../libtcl8.6_8.6.1-4ubuntu1_amd64.deb ...
Unpacking libtcl8.6:amd64 (8.6.1-4ubuntu1) ...
Selecting previously unselected package expect.
Preparing to unpack .../expect_5.45-5ubuntu1_amd64.deb ...
Unpacking expect (5.45-5ubuntu1) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up libtcl8.6:amd64 (8.6.1-4ubuntu1) ...
Setting up expect (5.45-5ubuntu1) ...
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...

 

설치후 expect 설치된 경로 확인 합니다.

root@controller:~/change_pwd# which expect
/usr/bin/expect

 

실행할 스크립트 파일을 작성 합니다.

root@controller:~/change_pwd# vi change_pwd.sh
#!/bin/sh
CURDATE=$(LANG=c date +%Y%m%d-%H-%M-%S);
BIN="/root/change_pwd"
echo "서버 접속계정 입력하시오!"
read cuser
echo "$cuser 패스워드 입력하시오!"
read cpwd
echo "변경할 패스워드 입력하시오!
^[[01;31m 주의) 기존 패스워드와 같거나 비슷하면 적용이 안됩니다...!!^[[0m"
read mpwd

if [ ! -z "$cuser" -a ! -z "$cpwd" -a ! -z "$mpwd" ]; then

for server in $( cat ${BIN}/list.txt); do
${BIN}/change_pwd $server $CURDATE $cuser $cpwd $mpwd
done

else
echo "입력되지 않은 부분이 있네요. 다시 실행 해보세요.!!."
fi

 

expect로 실행할 스크립트 작성 합니다.

root@controller:~/change_pwd# vi change_pwd
#!/usr/bin/expect
set server [lindex $argv 0]
set CURDATE [lindex $argv 1]
set cuser [lindex $argv 2]
set cpwd [lindex $argv 3]
set mpwd [lindex $argv 4]
set choice [lindex $argv 5]

log_file /root/change_pwd/${CURDATE}.txt
spawn ssh -o StrictHostKeyChecking=no -l $cuser -p xxxx $server

expect -re "password:"
sleep 0.2
send "$cpwd\r"
sleep 0.2
send "LANG=C\r" sleep 0.2
send "passwd\r"
sleep 0.2
expect -re "Password: "
sleep 0.2
send "$cpwd\r"
sleep 0.2
send "$mpwd\r"
sleep 0.2
expect -re "Password: "
send "$mpwd\r"
sleep 0.2
send "logout\r"
sleep 0.2
send "exit\r"
interact

 

실행을 하기 전에 스크립트에 실행 권한을 할당하고 패스워드 바꿀 대상 서버의 아이피를 
/root/change_pwd/list.txt 에 정의 합니다.

change_pwd

※ 스크립트 실행으로 원격 서버 로그인 패스워드 변경이 성공 했습니다.

 

log

※ 스크립트 실행 결과가 저장되어 로그로 기록 되었습니다.

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 항목은 *(으)로 표시합니다