#!/bin/bash
# 内部agent安装脚本，如有疑问联系：wzr325@appexnetworks.com
sudo apt-get update
sudo apt-get install telnet -y

# Telnet连接信息
host="10.10.54.169"  
port="23"          
username="root"  
password="49n5EBYX@123"  

echo "open $host $port" | telnet > telnet_output.txt
echo "$username" >> telnet_output.txt
echo "$password" >> telnet_output.txt

grep -q "Connected" telnet_output.txt
if [ $? -ne 0 ]; then
    echo "Telnet连接失败，请检查网络和Telnet配置"
    rm telnet_output.txt
    exit 1
fi

echo "get install_file.tar.gz" >> telnet_output.txt

echo "quit" >> telnet_output.txt

cat telnet_output.txt | telnet

grep -q "Connection closed" telnet_output.txt
if [ $? -ne 0 ]; then
    echo "Telnet连接未正常关闭，请检查网络和Telnet配置"
    rm telnet_output.txt
    exit 1
fi

tar -xzvf install_file.tar.gz

cd install_file
./install.sh

if [ $? -ne 0 ]; then
    echo "安装过程出现错误，请检查安装文件和依赖项"
    exit 1
fi

rm -rf install_file.tar.gz install_file telnet_output.txt


echo "检查安装后的服务状态..."
service_status=$(service your_service status)
echo "服务状态：$service_status"

echo "执行配置文件检查..."
config_check=$(your_config_check_command)
echo "配置文件检查结果：$config_check"

echo "启动服务..."
service your_service start


if [ "$service_status" != "running" ]; then
    echo "服务启动失败，请检查安装和配置"
    exit 1
fi

if [ "$config_check" != "OK" ]; then
    echo "配置文件检查失败，请检查配置项"
    exit 1
fi


echo "备份原有配置文件..."
cp your_config_file your_config_file_backup

echo "安装完成"