关于expect 维护TOMCAT的脚本
时间:2007-09-03 来源:hansonhan
基本上能凑合的用:)代码严谨性还是比较差的,需要斑竹多给予纠正:)自己可以扩展功能.
环境:
1.查看本机tcl 包安装的路径
$>whereis tcl
tcl: /usr/bin/tcl /usr/lib/tcl8.4 /usr/share/tcl8.4
2.安装expect
tar -zxvf expect-5.43.0.tar.gz
$>./configure --prefix=/usr \
--with-tcl=/usr/lib \
--with-tclinclude=/usr/lib/tcl8.4 \
--enable-shared
$>make
$>make test
$>make install
3.Tcl 安装(如果在安装expect时候提示找不到tcl包,或者就你的机器上没有这个包就安装它):
tar -zxvf tcl8.4.15-src.tar.gz
$>cd unix &&
$>./configure --prefix=/usr --enable-threads && make
$> make test
$> make install && make install-private-headers && ln -v -sf tclsh8.4 /usr/bin/tclsh
文件 1. hostlist.txt
#If there are some uesless IP addresses,you can use "#'before them to remark.---2007-08-29---
192.168.9.12|password|root|/home/tomcat
#192.168.9.13|password|root|/home/tomcat
192.168.9.19|password|root|/home/tomcat
文件2. loginssh.exp
#!/usr/bin/expect -f
##******************************************************************************************
## Program: loginssh, Version 0.2
## Author: hanson
## File: loginssh.exp
## Changelog: 2007.08.02 Version 0.1
## Initial release
## 2007.08.30 Version 0.2
## Bugfix: No error message when timeout
## Description:
## Given a hostname tomcate path user Password this script will Stop Start
## CheckTomcat.
## Example:
## chmod +x loginssh.exp
## ./loginssh.exp 123456 192.168.1.9 root CheckState TomcatPath
##Self-evaluation:The stringent code 3
##******************************************************************************************
# Init variables
set MyPasswordexp [lrange $argv 0 0]
set MyServerexp [lrange $argv 1 1]
set MyUserexp [lrange $argv 2 2]
set MyStateexp [lrange $argv 3 3]
set MyTomcatPathexp [lrange $argv 4 4]
#set username "hanson" --Variable definitions
set timeout 15
#-----------------------------Supporting Functions-------------------------------
# expect : wait fom some input
# send : send something to th program
# send_user :standard output
# send_error :standard error
# exit : Exit program with appropriate error code
#
#
proc Do_TomcatLogin {MyServerexp MyPasswordexp} {
set timeout 25
#The first method
#expect {
# -re "Are you sure you want to"
# {
# send "yes\n"
# exp_continue
# }
# }
#expect "Password:"
#send "$MyPasswordexp\n"
#expect "Login:"
#send_user "\nLogin Successfully...\n"
#The second method
#expect {
# -re "Are you sure you want to"
# {
# send "yes\n"
# exp_continue
# }
# }
expect {
-re "to continue" {
send "yes\n";
exp_continue
}
"password:" {
send "$MyPasswordexp\n";
send_user "\nLogin Successfully...\n"
}
timeout {
send_error "Failed to make ssh connection, exiting!\n";
close;
wait;
exit 1
}
}
}
proc Do_TomcatState {MyServerexp MyTomcatPathexp} {
#
set timeout 8
send "\n"
expect "state"
send "ps -ef | grep java |grep -v 'grep'| awk '{print \"user:\" \$1 \" PID:\" \$2}'\n"
expect "Login:"
send_user "\nServer IP :$MyServerexp Check State Finished...\n"
}
proc Do_TomcatStartup {MyServerexp MyTomcatPathexp} {
#
if { $MyServerexp == "192.168.1.12" } {
set timeout 45
} else {
set timeout 15
}
send "\n"
expect "startup"
send "$MyTomcatPathexp/bin/startup.sh\n"
expect "startupst"
send_user "\nServer iP:$MyServerexp start Finished...\n"
}
proc Do_TomcatStop {MyServerexp MyTomcatPathexp} {
#
set timeout 15
send "\n"
expect "stop:"
send "$MyTomcatPathexp/bin/shutdown.sh\n"
expect "stopst:"
send_user "\nServer iP:$MyServerexp stop Finished...\n"
}
proc Do_Tomcatkill {MyServerexp} {
#
set timeout 5
send "\n"
expect "kill"
send "kill -9 `ps -ef|grep java|grep -v 'grep'|awk '{print \$2}'`\n"
expect "killstate:"
send_user "\nServer iP:$MyServerexp tomcat kill Finished...\n"
}
proc Do_T {} {
#Not realizing
set timeout 10
send "\n"
expect -re " $"
send "ps -auxww|grep "usr"| wc -l\r"
expect -re " $"
send "echo $?\r"
}
if {$argc<5} {
puts stderr "Usage: $argv0 Password Server User State TomcatPath.\n "
exit 1
}
# *************************End of Supporting Functions*****************************
#**************************Main program started************************************
# Start a program
spawn ssh $MyUserexp@$MyServerexp
#
Do_TomcatLogin $MyServerexp $MyPasswordexp
switch $MyStateexp {
"CheckState" {
Do_TomcatState $MyServerexp $MyTomcatPathexp
}
"Start" {
Do_TomcatStartup $MyServerexp $MyTomcatPathexp
}
"Stop" {
#Do_TomcatStop $MyServerexp $MyTomcatPathexp
#if Do_TomcatStop Finished then
Do_Tomcatkill $MyServerexp
}
}
#****************************Main program end************************************
close
wait
exit
文件3. sshlogin.sh
#!/bin/bash
##******************************************************************************************
## Program: sshlogin, Version 0.1
## Author: hanson
## File: sslogin.sh
## Changelog: 2007.07.22 Version 0.1
## Initial release
## Description:
## Start up the Tomcat engine.
## Example:
## chmod +x sshlogin.sh
## ./loginssh.exp state
##Self-evaluation:The stringent code 3
##******************************************************************************************
# Source function library.
. /etc/init.d/functions
function StrSearch()
# Returns a string header
{
Str=${1##' '}
Str1=`expr substr $Str 1 1`
if [ $Str1 == "#" ]; then
return 0
else
return 1
fi
}
function PingHost()
# Using PING order checks network connectivity
{
local bool="T"
exec 3<&0 # 把标准输入关联到文件描述符.
exec 0<$FILE # 重定向标准输入.
while read line
do
MyServer=$(echo $line | cut -d '|' -f1)
StrSearch $MyServer
if [ "$?" -eq 0 ] ; then
continue
fi
ping -c 1 -w 1 $MyServer > /dev/null 2>&1;
if [ "$?" -eq "1" ]; then
echo "Can't ping $MyServer Quitting!"
bool="F"
else
echo "Ping to $MyServer was successful!"
fi
done
exec 0<&3 # 恢复旧的标准输入
echo -e "---------------------------------------------------------------Check end\n"
if [ "$bool" = "T" ] ; then
return 0
else
return 1
fi
}
function Login ()
# Users Login ,relevant file with loginssh.exp
{
local intstr=1
exec 3<&0
exec 0<$FILE
while read line
do
MyServer=$(echo $line | cut -d '|' -f1)
MyPassword=$(echo $line | cut -d '|' -f2)
MyUser=$(echo $line | cut -d '|' -f3)
MyTomcatPath=$(echo $line | cut -d '|' -f4)
StrSearch $MyServer
if [ "$?" -eq 0 ] ; then
continue
fi
echo "-----------Running ssh $MyUser@$MyServer...---------------------------$intstr"
$CONNECT $MyPassword $MyServer $MyUser $MyState $MyTomcatPath
echo -e "-----------$MyState Finished $MyUser@$MyServer...------------------\n"
intstr=$[$intstr+1]
done
exec 0<&3
}
# function TomcatLog ()
# {
# 1.定义一个关于Tomcat错误的数据字典,比如采集的服务器上有30个以上僵死的采集线程,监测每个TOMCAT的日志,
# 制定自动采集重启规则(条件),使之让此脚本真正意
# 义上实现自动化管理.有点理想,也容易:)
# 2...
# }
# --------------------------------------------------------------------------------------------
# Init variables
# Host List
FILE="`pwd`/hostlist.txt"
CONNECT="`pwd`/loginssh.exp"
MyServer=""
MyUser=""
MyPassword=""
MyTomcatPath=""
MyState=""
if [ ! -r $FILE ]; then
echo " Cannot read $FILE or it could not be found!"
exit
fi
case "$1" in
state)
MyState="CheckState"
PingHost
#exit
if [ "$?" -eq 0 ] ; then
Login
else
echo "Error:Each computer must be ensure that the normal network connection......"
exit;
fi
;;
start)
MyState="Start"
PingHost
if [ "$?" -eq 0 ] ; then
Login
else
echo "Error:Each computer must be ensure that the normal network connection......"
exit;
fi
;;
stop)
MyState="Stop"
PingHost
if [ "$?" -eq 0 ] ; then
Login
else
echo "Error:Each computer must be ensure that the normal network connection......."
exit;
fi
;;
*)
echo $"Usage: $0 {start|stop|state}"
exit
;;
esac
exit
环境:
1.查看本机tcl 包安装的路径
$>whereis tcl
tcl: /usr/bin/tcl /usr/lib/tcl8.4 /usr/share/tcl8.4
2.安装expect
tar -zxvf expect-5.43.0.tar.gz
$>./configure --prefix=/usr \
--with-tcl=/usr/lib \
--with-tclinclude=/usr/lib/tcl8.4 \
--enable-shared
$>make
$>make test
$>make install
3.Tcl 安装(如果在安装expect时候提示找不到tcl包,或者就你的机器上没有这个包就安装它):
tar -zxvf tcl8.4.15-src.tar.gz
$>cd unix &&
$>./configure --prefix=/usr --enable-threads && make
$> make test
$> make install && make install-private-headers && ln -v -sf tclsh8.4 /usr/bin/tclsh
文件 1. hostlist.txt
#If there are some uesless IP addresses,you can use "#'before them to remark.---2007-08-29---
192.168.9.12|password|root|/home/tomcat
#192.168.9.13|password|root|/home/tomcat
192.168.9.19|password|root|/home/tomcat
文件2. loginssh.exp
#!/usr/bin/expect -f
##******************************************************************************************
## Program: loginssh, Version 0.2
## Author: hanson
## File: loginssh.exp
## Changelog: 2007.08.02 Version 0.1
## Initial release
## 2007.08.30 Version 0.2
## Bugfix: No error message when timeout
## Description:
## Given a hostname tomcate path user Password this script will Stop Start
## CheckTomcat.
## Example:
## chmod +x loginssh.exp
## ./loginssh.exp 123456 192.168.1.9 root CheckState TomcatPath
##Self-evaluation:The stringent code 3
##******************************************************************************************
# Init variables
set MyPasswordexp [lrange $argv 0 0]
set MyServerexp [lrange $argv 1 1]
set MyUserexp [lrange $argv 2 2]
set MyStateexp [lrange $argv 3 3]
set MyTomcatPathexp [lrange $argv 4 4]
#set username "hanson" --Variable definitions
set timeout 15
#-----------------------------Supporting Functions-------------------------------
# expect : wait fom some input
# send : send something to th program
# send_user :standard output
# send_error :standard error
# exit : Exit program with appropriate error code
#
#
proc Do_TomcatLogin {MyServerexp MyPasswordexp} {
set timeout 25
#The first method
#expect {
# -re "Are you sure you want to"
# {
# send "yes\n"
# exp_continue
# }
# }
#expect "Password:"
#send "$MyPasswordexp\n"
#expect "Login:"
#send_user "\nLogin Successfully...\n"
#The second method
#expect {
# -re "Are you sure you want to"
# {
# send "yes\n"
# exp_continue
# }
# }
expect {
-re "to continue" {
send "yes\n";
exp_continue
}
"password:" {
send "$MyPasswordexp\n";
send_user "\nLogin Successfully...\n"
}
timeout {
send_error "Failed to make ssh connection, exiting!\n";
close;
wait;
exit 1
}
}
}
proc Do_TomcatState {MyServerexp MyTomcatPathexp} {
#
set timeout 8
send "\n"
expect "state"
send "ps -ef | grep java |grep -v 'grep'| awk '{print \"user:\" \$1 \" PID:\" \$2}'\n"
expect "Login:"
send_user "\nServer IP :$MyServerexp Check State Finished...\n"
}
proc Do_TomcatStartup {MyServerexp MyTomcatPathexp} {
#
if { $MyServerexp == "192.168.1.12" } {
set timeout 45
} else {
set timeout 15
}
send "\n"
expect "startup"
send "$MyTomcatPathexp/bin/startup.sh\n"
expect "startupst"
send_user "\nServer iP:$MyServerexp start Finished...\n"
}
proc Do_TomcatStop {MyServerexp MyTomcatPathexp} {
#
set timeout 15
send "\n"
expect "stop:"
send "$MyTomcatPathexp/bin/shutdown.sh\n"
expect "stopst:"
send_user "\nServer iP:$MyServerexp stop Finished...\n"
}
proc Do_Tomcatkill {MyServerexp} {
#
set timeout 5
send "\n"
expect "kill"
send "kill -9 `ps -ef|grep java|grep -v 'grep'|awk '{print \$2}'`\n"
expect "killstate:"
send_user "\nServer iP:$MyServerexp tomcat kill Finished...\n"
}
proc Do_T {} {
#Not realizing
set timeout 10
send "\n"
expect -re " $"
send "ps -auxww|grep "usr"| wc -l\r"
expect -re " $"
send "echo $?\r"
}
if {$argc<5} {
puts stderr "Usage: $argv0 Password Server User State TomcatPath.\n "
exit 1
}
# *************************End of Supporting Functions*****************************
#**************************Main program started************************************
# Start a program
spawn ssh $MyUserexp@$MyServerexp
#
Do_TomcatLogin $MyServerexp $MyPasswordexp
switch $MyStateexp {
"CheckState" {
Do_TomcatState $MyServerexp $MyTomcatPathexp
}
"Start" {
Do_TomcatStartup $MyServerexp $MyTomcatPathexp
}
"Stop" {
#Do_TomcatStop $MyServerexp $MyTomcatPathexp
#if Do_TomcatStop Finished then
Do_Tomcatkill $MyServerexp
}
}
#****************************Main program end************************************
close
wait
exit
文件3. sshlogin.sh
#!/bin/bash
##******************************************************************************************
## Program: sshlogin, Version 0.1
## Author: hanson
## File: sslogin.sh
## Changelog: 2007.07.22 Version 0.1
## Initial release
## Description:
## Start up the Tomcat engine.
## Example:
## chmod +x sshlogin.sh
## ./loginssh.exp state
##Self-evaluation:The stringent code 3
##******************************************************************************************
# Source function library.
. /etc/init.d/functions
function StrSearch()
# Returns a string header
{
Str=${1##' '}
Str1=`expr substr $Str 1 1`
if [ $Str1 == "#" ]; then
return 0
else
return 1
fi
}
function PingHost()
# Using PING order checks network connectivity
{
local bool="T"
exec 3<&0 # 把标准输入关联到文件描述符.
exec 0<$FILE # 重定向标准输入.
while read line
do
MyServer=$(echo $line | cut -d '|' -f1)
StrSearch $MyServer
if [ "$?" -eq 0 ] ; then
continue
fi
ping -c 1 -w 1 $MyServer > /dev/null 2>&1;
if [ "$?" -eq "1" ]; then
echo "Can't ping $MyServer Quitting!"
bool="F"
else
echo "Ping to $MyServer was successful!"
fi
done
exec 0<&3 # 恢复旧的标准输入
echo -e "---------------------------------------------------------------Check end\n"
if [ "$bool" = "T" ] ; then
return 0
else
return 1
fi
}
function Login ()
# Users Login ,relevant file with loginssh.exp
{
local intstr=1
exec 3<&0
exec 0<$FILE
while read line
do
MyServer=$(echo $line | cut -d '|' -f1)
MyPassword=$(echo $line | cut -d '|' -f2)
MyUser=$(echo $line | cut -d '|' -f3)
MyTomcatPath=$(echo $line | cut -d '|' -f4)
StrSearch $MyServer
if [ "$?" -eq 0 ] ; then
continue
fi
echo "-----------Running ssh $MyUser@$MyServer...---------------------------$intstr"
$CONNECT $MyPassword $MyServer $MyUser $MyState $MyTomcatPath
echo -e "-----------$MyState Finished $MyUser@$MyServer...------------------\n"
intstr=$[$intstr+1]
done
exec 0<&3
}
# function TomcatLog ()
# {
# 1.定义一个关于Tomcat错误的数据字典,比如采集的服务器上有30个以上僵死的采集线程,监测每个TOMCAT的日志,
# 制定自动采集重启规则(条件),使之让此脚本真正意
# 义上实现自动化管理.有点理想,也容易:)
# 2...
# }
# --------------------------------------------------------------------------------------------
# Init variables
# Host List
FILE="`pwd`/hostlist.txt"
CONNECT="`pwd`/loginssh.exp"
MyServer=""
MyUser=""
MyPassword=""
MyTomcatPath=""
MyState=""
if [ ! -r $FILE ]; then
echo " Cannot read $FILE or it could not be found!"
exit
fi
case "$1" in
state)
MyState="CheckState"
PingHost
#exit
if [ "$?" -eq 0 ] ; then
Login
else
echo "Error:Each computer must be ensure that the normal network connection......"
exit;
fi
;;
start)
MyState="Start"
PingHost
if [ "$?" -eq 0 ] ; then
Login
else
echo "Error:Each computer must be ensure that the normal network connection......"
exit;
fi
;;
stop)
MyState="Stop"
PingHost
if [ "$?" -eq 0 ] ; then
Login
else
echo "Error:Each computer must be ensure that the normal network connection......."
exit;
fi
;;
*)
echo $"Usage: $0 {start|stop|state}"
exit
;;
esac
exit
相关阅读 更多 +