| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 | #!/bin/bashDEFMODE=mainMODE=${2:-$DEFMODE}IAPPASS=rotekucnLOGIN=adminPASS=12345DB_FW=""#MAIN_FW=./output/RTPS_05502xx.binMAIN_FW=./output/IBP.binSERVICE_FW=./output/SERVICE_05502xx.binfunction usage {    echo -e "Usage: upload.sh {ip|mac} {full|main|db|service}\n\    \tmain - update main firmware\n\    \tdb - update doughter board firmware\n\    \service - upload service firmware\n\    \tfull - update both main and doughter boards firmware"}function wait_ip {	while ! ping -c1 -W1 $IP > /dev/null	do		echo "Waiting IP.."			sleep 1		((i++))		if [ $i == 10 ]		then			echo -e "\r\nError. IP not found!"			exit 1		fi	done}function need_login {	curl -s --compressed -b /tmp/cookies.txt -c /tmp/cookies.txt http://$IP/index.html | grep -q "Авторизация"}function need_iap {	curl -s --compressed -b /tmp/cookies.txt -c /tmp/cookies.txt http://$IP/index.html | grep -q "Параметры"}function log_out {	curl -s -b /tmp/cookies.txt -c /tmp/cookies.txt http://$IP/logout.cgi > /dev/null}function log_in {	curl -s -b /tmp/cookies.txt -c /tmp/cookies.txt -d "login=$LOGIN&password=$PASS" http://$IP/login.cgi > /dev/null}function boot_iap {	curl -s -X GET -b /tmp/cookies.txt -c /tmp/cookies.txt http://$IP/fw_update.cgi > /dev/null}function upload_db_fw {	echo $(curl --compressed http://$IP/upload.cgi -H 'Expect:' -F filedata=@$DB_FW --progress-bar)}function upload_main_fw {	echo $(curl --compressed http://$IP/upload.cgi -H 'Expect:' -F filedata=@$MAIN_FW --progress-bar)}function upload_service_fw {	TMP_FILE=$(mktemp -d /tmp/upload.XXXXX)/$(basename $MAIN_FW)	cp $SERVICE_FW $TMP_FILE	echo $(curl --compressed http://$IP/upload.cgi -H 'Expect:' -F filedata=@$TMP_FILE --progress-bar)}function goback {	curl -s http://$IP/goback.cgi }if [ $# -lt 1 ]then    echo "Error: Not enough parameters"     usage    exit 1fi#Check passed parameterif [[ "$1" =~ ^([a-fA-F0-9]{2}[:-]){5}[a-fA-F0-9]{2}$ ]]then    #Passed MAC    MAC=$(echo $1 | tr '[:upper:]' '[:lower:]' | tr '-' ':')    IP=$(sudo arp-scan -l | grep $MAC | cut -d$'\t' -f1)elif [[ "$1" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]then    #Passed IP    IP=$1else    echo "Error: Invalid parameter"    usage    exit 1fiping -c1 -W1 $IP 2>/dev/null 1>/dev/nullif [ "$?" = 0 ]then  echo "Host found ($IP)"else  echo "Error: Host not found ($IP)"  exit 1fiif need_login then	echo "Logging in.."	log_infi	if need_iapthen	echo "Reboot to IAP.."	boot_iap	sleep 10	wait_ipfi	if [[ $MODE = "full" || $MODE = "db" ]]then	echo "Prog db firmware.."	db_res=$(upload_db_fw)	if [[ $db_res == "1" ]]	then 		echo "OK"	else		echo "Error"		echo "Update failed!"		exit 1	fi	sleep 1fiif [[ $MODE = "db" ]]then	goback	echo "Successfully updated"	exit 0fiif [[ $MODE = "full" || $MODE = "main" ]]then	echo "Prog main firmware.."	main_res=$(upload_main_fw)	if [[ $main_res == "1" ]]	then 		echo "OK"	else 		echo "Error"		echo "Update failed!"		exit 1	fifiif [[ $MODE = "service" ]]then	echo "Prog service firmware.."	res=$(upload_service_fw)	if [[ $res == "1" ]]	then 		echo "OK"	else 		echo "Error"		echo "Update failed!"		exit 1	fi	rm -rf $TMP_FILEfiecho "Successfully updated"exit 0
 |