linux 下面程序自动安装脚本

         在此脚本中,主要用的是bash shell,需求为利用telnet或ssh把脚本写到服务器里面,让服务器执行脚本,从ftp服务器里面下载最新版本的程序,并解压执行。脚本内容如下:

#!/bin/bash
# install agent script

ftpIp="10.22.2.21"
ftpPort=2122
agentIp="10.22.2.22"
ftpUserName="test"
ftpUserPwd="123456"
ftpAgentPath="/main/v1.0"
ftpAgentFileName="agent.zip"
ftpJrePath="/java/linux/x86"
ftpJreName="jre.zip"
ftpErrorPath="/error"
agentSaveDir="/home/fan/agent"
agentContextName="hyperagent"
installAgentScriptPathName="agentscript"
installAgentScriptFileName="installAgent.sh"
startAgentScriptName="startup.sh"
uninstallAgentScriptName="shutdown.sh"
tempPortFileName="tempPortFileName.txt"
agentPort=4850
# $1 native absolutely file path
# $2 remote path 
# $3 new file name
function ftpUpload(){
ftp -n<<!
open $ftpIp $ftpPort
user $ftpUserName $ftpUserPwd
binary
prompt
cd $2
put $1  $3
close
bye
!
}
#ftpUpload "/home/boco/jre.zip" "/main" "jre2.zip"

# $1 native absolutely file path
# $2 remote path
# $3 remote file name
function ftpDownload(){
ftp -n<<!
open $ftpIp $ftpPort
user $ftpUserName $ftpUserPwd
binary
lcd $1
cd $2
prompt
mget $3
close
bye
!

}
#ftpDownload "/home/boco" "/main/v1.0" "hyperagent.zip"

agentScriptFullDir="$agentSaveDir/$installAgentScriptPathName"
# $1 is msg
function uploadAgentLog(){
	tempLogFullName="$agentScriptFullDir/$agentIp.log"
	echo $1 > $tempLogFullName
	ftpUpload $tempLogFullName $ftpErrorPath "$agentIp.log"
}
function uninstallAgent(){
echo "begin unistall agent"
echo "begin check port $agentPort status"
aport=`netstat -lntp | grep ":$agentPort "`

if [ -z "$aport" ]
then
        echo "port $agentPort free"
else
        echo "port $agentPort busy"
	uninstallScriptFullPath="$agentSaveDir/$agentContextName/bin"
        echo "begin invoke $uninstallScriptFullPath/$uninstallAgentScriptName to stop agent"
	if [ -e "$uninstallScriptFullPath/$uninstallAgentScriptName" ]
	then
		cd $uninstallScriptFullPath
		sh $uninstallAgentScriptName
		sleep 3
        	echo "end invoke $uninstallScriptFullPath/$uninstallAgentScriptName"
	else
        	echo "$uninstallScriptFullPath/$uninstallAgentScriptName not exist"
	fi
fi

echo "begin delete $agentSaveDir/$agentContextName"
rm -rf "$agentSaveDir/$agentContextName"
echo "end delete $agentSaveDir/$agentContextName"

# delete temp zipfile
rm -rf "$agentScriptFullDir/$ftpAgentFileName" 
rm -rf "$agentScriptFullDir/$ftpJreName" 
echo "end uninstall agent"
}

function installAgent2(){
#echo $agentScriptFullDir
echo "begin download main agent"
ftpDownload $agentScriptFullDir $ftpAgentPath $ftpAgentFileName
echo "end download main agent"
echo "begin download jre"
ftpDownload $agentScriptFullDir $ftpJrePath $ftpJreName
echo "end download jre"
echo "begin unzip main agent"
unzip -o -d $agentSaveDir "$agentScriptFullDir/$ftpAgentFileName" >>/dev/null
echo "end unzip main agent"
echo "begin unzip jre"
unzip -o -d "$agentSaveDir/$agentContextName" "$agentScriptFullDir/$ftpJreName" >>/dev/null
echo "end unzip jre"
# grant user x priviledge of jre and bin
echo "begin grant current user x priviledge of agent"
chmod -R u=rwx "$agentSaveDir/$agentContextName"
echo "end grant current user x priviledge of agent"

# start agent
echo "start agent"
cd "$agentSaveDir/$agentContextName/bin/"
sh $startAgentScriptName
#sh "$agentSaveDir/$agentContextName/$startAgentScriptName"
echo "start agent end"
sleep 3
echo "the third time to check port $agentPort status"
aport=`netstat -lntp | grep ":$agentPort "`
if [ -z "$aport" ]
then
        echo "agent installed but start error on port $agentPort"
	uploadAgentLog "agent installed but start error on port $agentPort"
else
        echo "agent start success on port $agentPort"
fi


#unzip 
#unzip -o -d /home/boco/agent/agentscript/ hyperagent.zip
#echo "end"
}
function installAgent(){
uninstallAgent

echo "again check port $agentPort status"
aport=`netstat -lntp | grep ":$agentPort "`

if [ -z "$aport" ]
then
        echo "port free"
	installAgent2
else
        echo "port $agentPort busy and cannot be removed,stop install agent"
	uploadAgentLog "port $agentPort busy and cannot be removed,stop install agent"
fi

echo "install complete!"
}
installAgent


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值