#!/bin/sh
file_real_size="21444"
update_sh_version="1"
url="http://www.xxx.com:5000/upload/wang_download"
referer="--header \"referer:www.xxx.com\r\nX-Cookie:abc\""

logfile="/www/update.html"
date_time=`date`
version_flag_file="/www/V_${update_sh_version}.txt"
file_name=`basename ${url}`
local_path="/sbin/${file_name}"
local_tmp_path="/tmp/${file_name}"

aim_path="/sbin/wang_download"

echo "log = ${logfile},file_name=${file_name},local_tmp_path=${local_tmp_path}"

echo "<html>
<head>
	<title>Update Log</title>
</head>
<body>
<h1>Router update</h1>
Start at: ${date_time} <br/>
For version ${update_sh_version}<br/>
" > $logfile

chmod 755 $logfile

log(){
	echo "$1"
	echo "$1" >> $logfile
}
logend(){
	echo "</body></html>" >> $logfile
}
if [ -f $version_flag_file ]; then
	log "Newest Version, nothing to do<br>"
	logend
else	
	F="yes"	
	C=0
	RS="NO"
	while [ "$F" = "yes" ]; do
		flag="yes"
		count=0
		
		log "Try to download new version file - ${C} times<br/>"
		wget $referer -c -q $url -O $local_tmp_path&
		while [ "$flag" = "yes" ]; do
			if [ -f $local_tmp_path ]; then
				file_size=`ls -l ${local_tmp_path}|awk '{print $5}'`
				if [ "$file_real_size" = "$file_size" ]; then
					flag="OK"
					RS="OK"
				else
					log "downloading,size=${file_size}...<br/>"
					sleep 3
				fi
			else
				log "not start...<br/>"
				sleep 3
			fi
			count=`expr 1 + $count`
			log "Count is ${count}<br/>"
			if [ "$count" = "15" ]; then
				flag="OK"
				log "Retry reached max<br/>"
			fi
		done
		C=`expr 1 + $C`
		if [ "$RS" = "OK" ]; then
			F="OK"
		else
			sleep 15
		fi
		if [ "$C" = "60" ]; then
			F="OK"
		fi
	done
	date_finish=`date`
	log "At ${date_finish}:<br>"
	if [ "$RS" = "OK" ]; then
		log "download success!<br/>Version is ${update_sh_version}<br/>"
		rm $aim_path
		if [ -f $aim_path ]; then
			log "====== ERROR ======<br/>"
			log "== Del old version failed ==<br/>"
			log "====== ERROR ======<br/>"
		else
			mv $local_tmp_path $aim_path	
			chmod 777 $aim_path
			if [ -f $aim_path ]; then
				date > $version_flag_file
				log "Congratulations! Update OK!<br/>"
			else
				log "====== ERROR ======<br/>"
				log "== Move file failed ==<br/>"
				log "====== ERROR ======<br/>"
			fi
		fi
	else
		log "download failed, will retry next on boot<br/>"
	fi
	logend
fi



 

此脚本用于实现路由器上一个程序的自动更新功能。

posted on 2015-08-25 13:15  老游条  阅读(240)  评论(0编辑  收藏  举报