使用expect配合ssh的key认证实现多台服务器的自动化处理

Posted by 4Aiur on 03/31/2010 in SysAdmin |

使用expect配合ssh的key认证实现多台服务器的自动化处理

使用以下方法可以方便、快速的实现多台服务器(500+)的管理,并且对中央管理服务器的配置要求不高。

使用crontab自动更新配置.

[root@4Aiur ~]# crontab -l
0 * * * * /usr/sbin/ntpdate -u -t 5 cn.pool.ntp.org >/var/log/ntpsync.log 2>&1
0 23 * * * /Application/Update/run.sh >/dev/null 2>&1

把加载key加入到系统自启动.

[root@4Aiur ~]# cat /etc/rc.local

#!/bin/sh
#
/root/.Batch/agent.exp

加载key的expect脚本.

[root@4Aiur ~]# cat /root/.Batch/agent.exp

#!/usr/bin/expect
spawn $env(SHELL)
send "cd /root/.Batch/\r"
expect "*"
send "killall ssh-agent\r"
expect "*"
send "ssh-agent | head -2 > /root/.Batch/.agent.env\r"
expect "*"
send ". /root/.Batch/.agent.env\r"
expect "*"
send "ssh-add key\r"
expect "Enter passphrase for key: "
send "example\r"
expect eof

定时在23点的自动更新程序.

[root@4Aiur ~]# cat /Application/Update/run.sh

#/bin/sh
# set -x
. /root/.Batch/.agent.env

DEVICE_LIST="/root/4Aiur/iplist"
SH_PATH="shell"
LOG_PATH="logs"

cd /Application/Update
/usr/java/jdk1.5.0_06/bin/java -jar Config.jar 2>/dev/null

mkdir -p ${SH_PATH} ${LOG_PATH}
rm -f ${SH_PATH}/* ${LOG_PATH}/*

while read SN HOSTNAME IPADDRESS
do

(SH="${SH_PATH}/${HOSTNAME}"
LOG="${LOG_PATH}/${HOSTNAME}"
cat > ${SH}  ${LOG} 2>&1 &
)
done 

Tags:

Copyright © 2010-2024 4Aiur All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.5, from BuyNowShop.com.