#!/bin/bash
typeset cmd=""
typeset check_cmd=""
typeset kill_cmd=""
typeset ip=""
typeset user=""
typeset passwd=""
typeset tmp_passwd=""
function rsa_login
{
ip=$1
passwd=$2
user=$3
echo "[temp]" > ${dir}/.ssh/hosts
echo "${ip} ansible_ssh_user=${user} ansible_ssh_private_key_file=${dir}/.ssh/${ip}" > ${dir}/.ssh/hosts
/bin/expect
spawn ansible -i ${dir}/.ssh/hosts temp -m ping
expect {
"es/no" { send "yes\n"; exp_continue }
"${ip}':" { send "${passwd}\n"; exp_continue }
}
EOF
return 0
}
function create_rsa
{
if [ ! -e "${dir}/.ssh" ];then
mkdir ${dir}/.ssh
fi
while read line
do
if [ "X${line}" != "X" ];then
ip=$(echo "${line}" | awk -F"," '{print $1}')
user=$(echo "${line}" | awk -F"," '{print $2}')
passwd=$(echo "${line}" | awk -F"," '{print $3}')
tmp_passwd=$(echo "${line}" | awk -F"," '{print $4}')
./dra_scp1.exp "${ip}" "${user}" "${passwd}"
if [ $? -ne 0 ];then
echo "scp create_id_rsa.sh to ${ip} is failed."
break;
fi
cmd="sh create_id_rsa.sh"
./dra.exp "${ip}" "${user}" "${passwd}" "${tmp_passwd}" "${cmd}" > tmp_file_excete
cmd="ls -al ${ip} | wc -l"
./dra_check.exp "${ip}" "${user}" "${passwd}" "${cmd}" > tmp_file
sed -i '/spawn/d' tmp_file
sed -i '/password/d' tmp_file
grep -w "0" tmp_file > /dev/null
if [ $? -ne 0 ];then
echo "${ip} : id_rsa is not create"
else
./dra.exp "${ip}" "${user}" "${passwd}" "echo ${ip}.pub >> .ssh/authorized_keys" > tmp_file_excete
./dra_scp2.exp "${ip}" "${user}" "${passwd}" > tmp_file_excete
ls -al ${dir}/.ssh/${ip} | wc -l > tmp_file
grep -w "1" tmp_file > /dev/null
if [ $? -ne 0 ];then
echo "${ip} : id_rsa is not scp success"
fi
fi
chmod 600 ${dir}/.ssh/${ip}
rsa_login "${ip}" "${user}" "${tmp_passwd}"
if [ $? -ne 0 ];then
echo "login ${ip} is failed."
fi
fi
done
return 0
}
function stop_kill
{
ip=$1
user=$2
passwd=$3
check_cmd=$4
kill_cmd=$5
./dra.exp "${ip}" "${user}" "${tmp_passwd}" "${kill_cmd}" > tmp_file_excute
./dra.exp "${ip}" "${user}" "${tmp_passwd}" "${check_cmd}" > tmp_file
sed -i '/password/d' tmp_file
grep -w "0" tmp_file > /dev/null
if [ $? -ne 0 ];then
echo "kill : ${ip} is failed."
return 1
fi
return 0
}
function stop
{
while read line
do
if [ "X${line}" != "" ];then
ip=$(echo ${line} | awk -F"," '{print $1}')
user=$(echo ${line} | awk -F"," '{print $2}')
passwd=$(echo ${line} | awk -F"," '{print $3}')
if [ "X${passwd}" == "Xuat_jboss_xhx7" ];then
tmp_passwd="jbossxhx7"
cmd="sh /opt/wildfly/bin/stop.sh"
check_cmd="ps -ef | grep java | grep jboss | grep -v grep | wc -l"
kill_cmd="ps -ef | grep java | grep jboss | grep -v grep | awk '{print $2}' | xargs -n1 -i kill -9 {}"
elif [ "X${passwd}" == "Xuat_jboss" ]; then
tmp_passwd="jbossuser"
cmd="sh /opt/wildfly/bin/stop.sh"
check_cmd="ps -ef | grep java | grep jboss | grep -v grep | wc -l"
kill_cmd="ps -ef | grep java | grep jboss | grep -v grep | awk '{print $2}' | xargs -n1 -i kill -9 {}"
elif [ "X${passwd}" == "Xuat_jboss_zf9" ]; then
tmp_passwd="jbosszf9"
cmd="sh /opt/wildfly/bin/stop.sh"
check_cmd="ps -ef | grep java | gep jboss | grep -v grep | wc -l"
kill_cmd="ps -ef | grep java | grep jboss | grep -v grep | awk '{print $2}' | xargs -n1 -i kill -9 {}"
elif [ "X${passwd}" == "Xtomcat" ]; then
tmp_passwd="tomcat"
cmd="sh /opt/snbtmct/bin/snbtmtd stop"
check_cmd="ps -ef | grep java | grep tomcat | grep -v grep | wc -l"
kill_cmd="ps -ef | grep java | grep tomcat | grep -v grep | awk '{print $2}' | xargs -n1 -i kill -9 {}"
else
tmp_passwd="tomcatuser"
cmd="sh /opt/tomcat/bin/shutdown.sh"
check_cmd="ps -ef | grep java | grep tomcat | grep -v grep | wc -l"
kill_cmd="ps -ef | grep java | grep tomcat | grep -v grep | awk '{print $2}' | xargs -n1 -i kill -9 {}"
fi
./dra.exp "${ip}" "${user}" "${tmp_passwd}" "netstat -an | grep -E \"8080|9990\" | wc -l" > tmp_file
sed -i '/spawn/d' tmp_file
sed -i '/password/d' tmp_file
grep -w "0" tmp_file > /dev/null
if [ $? -ne 0 ];then
./dra.exp "${ip}" "${user}" "${tmp_passwd}" "${cmd}" > tmp_file_excute
./dra.exp "${ip}" "${user}" "${tmp_passwd}" "${check_cmd}" > tmp_file
sed -i '/spawn/d' tmp_file sed -i '/password/d' tmp_file grep -w "0" tmp_file > /dev/null if [ $? -eq 0 ];then echo "${ip} stop failed." echo "Begin to kill : ${ip}" stop_kill "${ip}" "${user}" "${tmp_passwd}" "${check_cmd}" "${kill_cmd}" if [ $? -ne 0 ];then echo "${ip} stop fialed." return 1 else echo "${ip} stop success" fi fi fi fi
done<iplist
return 0
}
maiin $@
if [ $? -ne 0 ];then
echo "script_excute_error"
exit 1
else
echo "script_excute_success"
exit 0
fi
function main
{
stop
if [ $? -ne 0 ];then
echo "stop is