linux 下面程序自动安装脚本

来源:互联网 发布:化学方程式书写软件 编辑:程序博客网 时间:2024/06/09 17:33

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

#!/bin/bash# install agent scriptftpIp="10.22.2.21"ftpPort=2122agentIp="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 namefunction ftpUpload(){ftp -n<<!open $ftpIp $ftpPortuser $ftpUserName $ftpUserPwdbinarypromptcd $2put $1  $3closebye!}#ftpUpload "/home/boco/jre.zip" "/main" "jre2.zip"# $1 native absolutely file path# $2 remote path# $3 remote file namefunction ftpDownload(){ftp -n<<!open $ftpIp $ftpPortuser $ftpUserName $ftpUserPwdbinarylcd $1cd $2promptmget $3closebye!}#ftpDownload "/home/boco" "/main/v1.0" "hyperagent.zip"agentScriptFullDir="$agentSaveDir/$installAgentScriptPathName"# $1 is msgfunction uploadAgentLog(){tempLogFullName="$agentScriptFullDir/$agentIp.log"echo $1 > $tempLogFullNameftpUpload $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" ]thencd $uninstallScriptFullPathsh $uninstallAgentScriptNamesleep 3        echo "end invoke $uninstallScriptFullPath/$uninstallAgentScriptName"else        echo "$uninstallScriptFullPath/$uninstallAgentScriptName not exist"fifiecho "begin delete $agentSaveDir/$agentContextName"rm -rf "$agentSaveDir/$agentContextName"echo "end delete $agentSaveDir/$agentContextName"# delete temp zipfilerm -rf "$agentScriptFullDir/$ftpAgentFileName" rm -rf "$agentScriptFullDir/$ftpJreName" echo "end uninstall agent"}function installAgent2(){#echo $agentScriptFullDirecho "begin download main agent"ftpDownload $agentScriptFullDir $ftpAgentPath $ftpAgentFileNameecho "end download main agent"echo "begin download jre"ftpDownload $agentScriptFullDir $ftpJrePath $ftpJreNameecho "end download jre"echo "begin unzip main agent"unzip -o -d $agentSaveDir "$agentScriptFullDir/$ftpAgentFileName" >>/dev/nullecho "end unzip main agent"echo "begin unzip jre"unzip -o -d "$agentSaveDir/$agentContextName" "$agentScriptFullDir/$ftpJreName" >>/dev/nullecho "end unzip jre"# grant user x priviledge of jre and binecho "begin grant current user x priviledge of agent"chmod -R u=rwx "$agentSaveDir/$agentContextName"echo "end grant current user x priviledge of agent"# start agentecho "start agent"cd "$agentSaveDir/$agentContextName/bin/"sh $startAgentScriptName#sh "$agentSaveDir/$agentContextName/$startAgentScriptName"echo "start agent end"sleep 3echo "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(){uninstallAgentecho "again check port $agentPort status"aport=`netstat -lntp | grep ":$agentPort "`if [ -z "$aport" ]then        echo "port free"installAgent2else        echo "port $agentPort busy and cannot be removed,stop install agent"uploadAgentLog "port $agentPort busy and cannot be removed,stop install agent"fiecho "install complete!"}installAgent


 

原创粉丝点击