Script - CCcam [start|stop|restart|check|cards|respond...

feissmaik
Entwickler Team
Beiträge: 2576
Registriert: So 17. Apr 2011, 11:39
Been thanked: 1 time
Kontaktdaten:

Script - CCcam [start|stop|restart|check|cards|respond...

Beitrag von feissmaik »

Eins meiner älteren CS Scripts für CCcam was nicht nur guckt ob der Prozess noch läuft, sondern sich auch versucht via TELNETINFO anzumelden und wenn das nicht funktioniert (respond) wird CCcam neu gestartet...
Ausserdem kann das Script auch prüfen ob zb 2 Karten des gleichen Typs (zb Betacrypt) noch von CCcam erkannt sind oder ob sich dort zb eine von verabschiedet hat (cards) und startet dann CCcam ggf neu (hatte früher mal Probleme das einer von 2 Smargos unregelmässig rausgeflogen wa)
...es kann auch nochn bissal mehr, siehe Changelog...


cccam [start|stop|restart|check|cards|respond|log|conf]

Code: Alles auswählen

#!/bin/sh
#
# 08.2010
#
## Changelog:
#
# v0.51  added feature to display CCcam's log in realtime...
#        code cleanups...
#
# v0.50  added editor mode for config files (it uses nano if installed else vi)
#        added support for rar -> if installed backuplogs gets packed with -m5
#        readded old and still supported cmds...
#        added /var/log/kern.log (or any other?) to get backuped on crash
#        fixed script-config-checks...
#        fixed connection failed handling of nc...
#        optimized code...
#
# v0.44: added $CCcamLOG as CCcam-output-log filename
#        added another script-config-checks...
#
# v0.43: added BACKUPCARDCHECK to make backups of logfiles on crash
#        removed RESPONDCHECK coz DETECTCHECK does the same (but its still useable)
#
# v0.42: added check for "detected and working cards" 
#        added /tmp/CCcamCheck.lock file so the script cant run twice
#
# v0.41: added '-i 1' to respond/detected checks as NCINTERVAL
#        changed script syntax for cronjob to: check auto
#        added misc stuff like script-config-checks etc...
#
# v0.40: added RESPONDCHECK
#        added ENABLECOLORS
#        fixed start_cam
#
#
## Recommended HowTo:
#
#
# ! ->  ITS VERY IMPORTEND THAT YOU   D O N T   FORWARD ANY OTHER PORTS
# ! ->  EXCEPT CCCAM-SERVER-LISTEN-PORT OUTSIDE YOUR LAN !!
# ! ->  YOU SHOULD ALSO CHANGE   A L L   DEFAULT PORTS !!
#
#
# Add a new shell user and install there your CCcam:
#
#  useradd -m -s/bin/bash CS
#  usermod -G dialout,adm -a CS
#  su - CS
#  mkdir -p CCcam/bin CCcam/logs CCcam/etc
#
# Copy your files as followed: (inc. this script as: cccam)
#
#  CCcam/
#  cccam
#
#  CCcam/etc:
#  CCcam.cfg  CCcam.channelinfo  CCcam.providers  SoftCam.Key  AutoRoll.Key  constant.cw
#
#  CCcam/bin:
#  CCcam.211.x86
#
# Make the CCcam binary and my script executable..
#
#  chmod 700 CCcam/cccam CCcam/bin/CCcam.211.x86
#
# Change the following in your CCcam.cfg:
#
#  TELNETINFO USERNAME : 
#  TELNETINFO PASSWORD : 
#  TELNETINFO LISTEN PORT : 16000
#  SOFTKEY FILE : /home/CS/CCcam/etc/SoftCam.Key
#  AUTOROLL FILE : /home/CS/CCcam/etc/AutoRoll.Key
#  STATIC CW FILE : /home/CS/CCcam/etc/constant.cw
#  CAID PRIO FILE : /home/CS/CCcam/etc/CCcam.prio
#  PROVIDERINFO FILE : /home/CS/CCcam/etc/CCcam.providers
#  CHANNELINFO FILE : /home/CS/CCcam/etc/CCcam.channelinfo
#  LOG WARNINGS : /home/CS/CCcam/logs/warnings.log
#
# Add some cronjob entry so CCcam gets started on reboot and gets checked every 2 minutes:
#
#  echo "@reboot	$HOME/CCcam/cccam start  >/dev/null 2>&1" >> $HOME/.cron
#  echo "*/2 * * * *	$HOME/CCcam/cccam check auto >/dev/null 2>&1" >> $HOME/.cron
#  crontab $HOME/.cron
#
#
# NOTES:
#
#  - i recommend to use */2 !
#    */2 means every 2 minutes (eg.: 12:00, 12:02, 12:04, 12:06, 12:08, 12:10, 12:12...)
#    set it only to * means every minute
#
#  - if you want to run more than one CCcam-server:
#    cp -r CCcam CCcam2 ... rename the binary (eg: CCcam.211.x86_2)
#    reconfigure this script and your new CCcam.cfg
#
#  - COMP is used to choose the compression type to be used, options are: tar or rar
#    rar will produce slightly smaller files but is more processor intensive so
#    may take longer to complete.
#
#
# ...Feel free to change the paths...
# ...which depends on your receiver/image/system...
#
#
### CONFIG - START


# Enable colored output-lines on shell? [yes/no]
ENABLECOLORS="yes"

# Path where the CCcam binary lies, no trailing / [CCcam-default: /var/emu]
CCcamPATH="/home/CS/CCcam/bin"

# Name of the CCcam binary [CCcam-default: cccam]
CCcamBIN="CCcam.211.x86"

# Path to save the CCcam logs to, no trailing / [CCcam-default: /tmp]
CCcamPLOG="/home/CS/CCcam/logs"

# Inside $CCcamPLOG the CCcam-output-log is saved to which file?
CCcamLOG="$CCcamPLOG/log.CCcam"

# Enable above CCcam-output-log? [yes/no]
# NOTE: its growing, and growing...UP in SIZE if you have much peers! but good to debug!
enableLOG="yes"

# Inside $CCcamPLOG we save the crontabed checks to which file?
CCcamCLOG="check.CCcam.log"

# Complete /path/to/file of your CCcam.cfg [CCcam-default: /var/etc/CCcam.cfg]
CCcamCFG="/home/CS/CCcam/etc/CCcam.cfg"

# Options to start CCcam with [CCcam-default: <none>]
# NOTE: DON'T edit unless you know what you are doing!
# NOTE: im using -d to save CCcam's output into a logfile so dont add it again please
CCcamOPTIONS="-t"

# CCCam-IP of your CCcam-Server for respond-checks (normaly: localhost/127.0.0.1)
# NOTE: you need to set this for DETECTCHECK
CCCAMIP="127.0.0.1"

# TELNETINFO-LISTEN-PORT of your CCcam-Server for respond-checks [CCcam-default: 16000]
# NOTE: you need to set this for DETECTCHECK
TELNETPORT="16000"

# Enable "detected and working cards"-check ? [yes/no]
DETECTCHECK="yes"

# For Card-Detected-Check, what is the FIRST word of your card? [Betacrypt/Viaccess/Nds]
# (currently only one identical cardname supported!)
CARDNAME="Betacrypt"

# How many identical cards your using with this CCcam?
CARDNUM="2"

# Create backups from logs of above card-check if it failed ? [yes/no]
BACKUPCARDCHECK="yes"

# Should we backup also another system-log-file for debugging?
# setting this to "" to disable or add more files seperated by space
BACKUPSYSLOGFILELIST="/var/log/kern.log"

# Choose Compression type. (tar or rar)
COMP="tar"


# delay interval for lines sent (for responding check) [default: <empty>]
# NOTE: DON'T edit unless you know what you are doing!
# NOTE: if your CCcam-server runs on an 1xChip dbox its possible that the
# respond-check tooks too long for this script. Than you need to set
# this to '-i 3' or '-i 4'... it waits longer befor quitting and fail...
NCINTERVAL="-i 2"

# default System's binary paths to locate nano and rar, no trailing /
# NOTE: DON'T edit unless you know what you are doing!
SYSTEMbinPATHS="/bin /usr/bin"



### CONFIG - END



# -------------------------------------------------------------- #
# >>> >> >  DO NOT MESS WiTH ANYTHiNG BELOW THiS LiNE!  < << <<< #
# -------------------------------------------------------------- #



DATE=`date`
DOW=`date +%A` # Day of the week e.g. Monday
DNOW=`date +%u` # Day number of the week 1 to 7 where 1 represents Monday
DOM=`date +%d` # Date of the Month e.g. 27
M=`date +%B` # Month e.g January
W=`date +%V` # Week Number e.g 37
VER="0.51"
CHECKFILE="/tmp/cccamtest.log"
tmpCARDNUM="0"


#fixme:
#LogRotate() {
# Weekly Backup
#if [ $DNOW = $DOWEEKLY ]; then
#echo Weekly Backup
#echo
#echo Rotating last weeks Backup...
#if [ "$W" -le 05 ];then
#REMW=`expr 48 + $W`
#elif [ "$W" -lt 15 ];then
#REMW=0`expr $W - 5`
#else
#REMW=`expr $W - 5`
#fi
#eval rm -fv "$REMW.*" 
#}

if [ -z "$ENABLECOLORS" ]; then
	## Read script-config
	if [ -z $scriptconfig ]; then
		scriptconfig="`dirname $0`/Checkcccam.conf"
	fi
	if [ ! -r $scriptconfig ]; then
		echo "ERROR: Cant read $scriptconfig"
		exit 0
	else
  	. $scriptconfig
	fi
fi

if [ -z "$CCcamCFG" ]; then
	CCcamCFG="/var/etc/CCcam.cfg"
fi

if [ "$SYSTEMbinPATHS" = "" ] && [ ! -z "$PATH" ]; then
	SYSTEMbinPATHS=$PATH
fi

if [ "$ENABLECOLORS" = "yes" ]; then
		ROT=`tput setaf 1`
		GRUEN=`tput setaf 2`
		BOLD=`tput bold`
		NORM='tput op && tput sgr0'
	else
		ROT="" ; GRUEN="" ; BOLD="" ; NORM=""
fi

# This methode deletes old and creates an new logfile for CCcam on re/start
create_new_log() {
	if [ ! -e $CCcamLOG ]; then
		touch $CCcamLOG
	else
		mv -f $CCcamLOG ${CCcamLOG}.old
		touch $CCcamLOG
	fi
}

# This method starts CCcam
start_cam() {
	  if ps x |grep -v grep |grep -c $CCcamBIN >/dev/null 2>&1
		then
			echo $ROT "ERROR: $CCcamBIN still runs with pid: $(pidof $CCcamBIN)" ; $NORM
		else
			if [ -f "/tmp/.CCcam.nodeid"]; then
				rm -f /tmp/.CCcam.nodeid
			fi
			cd $CCcamPATH
			if [ "$enableLOG" = "yes" ]; then
				create_new_log
				./$CCcamBIN $CCcamOPTIONS -d -C $CCcamCFG >>$CCcamLOG 2>&1 &
			else
				./$CCcamBIN $CCcamOPTIONS -q -C $CCcamCFG &
			fi
			echo $GRUEN "Successfully started CCcam with pid: $(pidof $CCcamBIN)" ; $NORM
		fi
}
		
# This method stops CCcam
stop_cam() {
		PiD=$(pidof $CCcamBIN)
		if [ -z "$PiD" ]; then
			echo $ROT "ERROR: Cant see $CCcamBIN to stop..." ; $NORM
		else
			echo $GRUEN "Stopping CCcam with pid: $PiD" ; $NORM
			kill -9 $PiD
		fi
}

# This method checks for running process, detected and working cards and restarts if not...
check_run() {
		if ps x |grep -v grep |grep -c $CCcamBIN >/dev/null
		then
			echo $GRUEN "Seems CCcam still runs with pid: $(pidof $CCcamBIN)" ; $NORM
			if [ "$DETECTCHECK" = "yes" ]; then
					sleep 1 ; check_cards
			fi
		else
			echo "["$DATE"] BAD - Couldn't see $CCcamBIN running. Starting!" >> $CCcamPLOG/$CCcamCLOG
			echo $ROT "Couldn't see $CCcamBIN running. Starting!" ; $NORM
			start_cam
		fi
}

# This method checks detected and working cards and restarts if not...
check_cards() {
		echo "...Checking if all CCcam '$CARDNAME' cards are presend"
		echo entitlements|nc $NCINTERVAL $CCCAMIP $TELNETPORT > $CHECKFILE
		if [ "$?" != "0" ]; then
			echo $ROT "ERROR while connecting to CCcam Server!" ; $NORM
			echo "["$DATE"] BAD - ERROR while connecting to CCcam Server!" >> $CCcamCLOG
			proc_exit
		fi
		CardDetect=$(grep -w $CARDNAME $CHECKFILE)
		tmpCARDNUM=$(grep -c $CARDNAME $CHECKFILE)
		if [ "$tmpCARDNUM" = "$CARDNUM" ]; then
			echo $GRUEN " OK - $tmpCARDNUM Cards are shown in entitlements!"; $NORM
			echo "$CardDetect"
		else
			echo "["$DATE"] BAD - Only $tmpCARDNUM/$CARDNUM Cards detected! Restarting CCcam: $CCcamBIN" >> $CCcamCLOG
			echo $ROT " BAD - Only $tmpCARDNUM/$CARDNUM Cards detected! Restarting CCcam: $CCcamBIN"; $NORM
			if [ "$BACKUPCARDCHECK" = "yes" ]; then
				echo " Creating backup from logs: $CCcamPLOG/$CBFILE"
				echo "["$DATE"] Created backup from logs: $CCcamPLOG/$CBFILE" >> $CCcamCLOG
				$PACKAGER $CHECKFILE $CCcamLOG $BACKUPSYSLOGFILELIST > $CCcamPLOG/$CBFILE
				sleep 1
			fi
			stop_cam
			sleep 10
			start_cam
		fi
		rm -f $CHECKFILE
}

# This method checks if CCcam still responding like it should
check_respond() {
		process=`ps auxwww | grep $CCcamBIN | grep -v grep | awk '{print $1}'`
		if [ -z "$process" ]; then
			echo $ROT " BAD - Couldn't find $CCcamBIN running. Restarting!"; $NORM
			echo "["$DATE"] BAD - Couldn't find $CCcamBIN running. Restarting!" >> $CCcamPLOG/$CCcamCLOG
			start_cam
		else
			echo $GRUEN "CCcam process is present, now checking if CCcam is frozen"; $NORM
			# minimize Logfile size
			LASTLINECHECK=`tail -n1 $CCcamPLOG/$CCcamCLOG | grep "now checking if CCcam is frozen"`
			if [ -z "$LASTLINECHECK" ]; then
				echo "CCcam process is present, now checking if CCcam is frozen" >> $CCcamPLOG/$CCcamCLOG
			fi
			echo info|nc $NCINTERVAL $CCCAMIP $TELNETPORT > $CHECKFILE
			if [ "$?" != "0" ]; then
				echo $ROT "ERROR while connecting to CCcam Server!" ; $NORM
				echo "["$DATE"] BAD - ERROR while connecting to CCcam Server!" >> $CCcamPLOG/$CCcamCLOG
				rm -f $CHECKFILE ; proc_exit
			fi
			CHECKED=$(ls -s $CHECKFILE)
			if [ "$CHECKED" = "0" ]; then
				echo $ROT " BAD - CCcam is frozen, restarting CCcam: $CCcamBIN"; $NORM
				echo "["$DATE"] BAD - CCcam is frozen, restarting CCcam" >> $CCcamPLOG/$CCcamCLOG
				stop_cam
				sleep 10
				start_cam
			else
				echo $GRUEN " OK - CCcam is responding like it should!"; $NORM
				# FixMe: output responding to shell if its NOT triggered: check auto
				if [ ! "$2" = "auto" ]; then
						echo "CCcam responding:"
						echo $(cat $CHECKFILE)
				fi
				# minimize Logfile size
				LASTLINECHECK=`tail -n1 $CCcamPLOG/$CCcamCLOG | grep "CCcam is responding like it should"`
				if [ -z "$LASTLINECHECK" ]; then
					echo "["$DATE"] OK - CCcam is responding like it should" >> $CCcamPLOG/$CCcamCLOG
				fi
			fi
			rm -f $CHECKFILE
		fi
}

# This method watches the $CCcamLOG in realtime...
monitor_log() {
		if [ ! -e "$CCcamLOG" ]; then
				echo $ROT "ERROR: Cant find CCcam log $CCcamLOG ... Is it enabled?" ; $NORM
		else
			echo " "
			echo $BOLD "Starting monitoring of $CCcamLOG in realtime. Hit [STRG] + [C] to abort!" ; $NORM
			echo " "
			sleep 1
			tail -f $CCcamLOG
			proc_exit
		fi
}

proc_Get_Packager() {
	unset PACKAGER
	CBFNOW=$(date +"%d.%m.%Y_%H%M")
	for Spath in $SYSTEMbinPATHS ; do
				if [ -x $Spath/rar ]; then
						PACKAGER="$Spath/rar a -inul -tk -m5 -y"
						CBFILE="backup_checklog__$CBFNOW.rar"
					fi
	done
	if [ -z "$PACKAGER" ]; then
			PACKAGER="tar -czp"
			CBFILE="backup_checklog__$CBFNOW.tgz"
	fi
}

proc_Get_Editor() {
	unset UseEDITOR
	for Spath in $SYSTEMbinPATHS ; do
		if [ -x "$Spath/nano" ]; then
			UseEDITOR="$Spath/nano -R"
		fi
	done
	if [ -z "$UseEDITOR" ]; then
		for Spath in $SYSTEMbinPATHS ; do
			if [ -x "$Spath/vi" ]; then
				UseEDITOR="$Spath/vi"
			fi
		done
	fi
	if [ -z "$UseEDITOR" ]; then
		echo $ROT "ERROR: Unable to locate Editor to open file - Be sure SYSTEMbinPATHS is set currectly!" ; $NORM
		proc_exit
	fi
}

# edit files given from $1
edit_file() {
	case "$1" in
		conf*)
			EFILE="$CCcamCFG"
			if test -f $EFILE ; then
				echo "Opening file: $EFILE ... with: $UseEDITOR"
				sleep 1 ; $UseEDITOR $EFILE
			else
				echo -e "\033[1;31mError opening file: $EFILE !\033[0m"
			fi
		;;
		*)
			echo "err... $*"
		;;
	esac
}


proc_Find_CCcamBIN() {
	CCCAMbinLIST=""
	if [ ! -z "$CCcamPATH" ]; then
			CPfilelist=$(ls $CCcamPATH)
			for Cfile in $CPfilelist ; do
				if [ "$Cfile" = "[cC][cC][cC][aA][mM]*" ]; then
					CCCAMbinLIST="$CCCAMbinLIST $Cfile"
				fi
			done
	fi
}


proc_exit() {
	rm -f "/tmp/CCcamCheck.lock"
	exit 0
}

if [ -e "/tmp/CCcamCheck.lock" ]; then
	if [ "`find \"/tmp/CCcamCheck.lock\" -type f -mmin -5`" ]; then
			echo "Lockfile /tmp/CCcamCheck.lock exists and is not 5 minutes old yet. Quitting!"
			exit 0
	else
			echo "Lockfile exists, but its older then 5 minutes. Removing lockfile."
			touch "/tmp/CCcamCheck.lock"
	fi
else
	touch "/tmp/CCcamCheck.lock"
fi


## script-config checks...
if [ ! -d "$CCcamPLOG" ]; then
	echo $ROT "ERROR: Directory $CCcamPLOG does not exist - Check paths and permissions!" ; $NORM
	proc_exit
fi

if [ ! -w "$CCcamPLOG/$CCcamCLOG" ]; then
	echo $ROT "ERROR: Can not write to $CCcamPLOG/$CCcamCLOG - Check paths and perms!" ; $NORM
	proc_exit
fi

if [ ! -e $CCcamPATH/$CCcamBIN ]; then
	echo $ROT "ERROR: Can not find CCcam binary: $CCcamPATH/$CCcamBIN - Check paths and perms!" ; $NORM
	proc_exit
fi

if [ ! -e $CCcamPLOG/$CCcamCLOG ]; then               
	touch $CCcamPLOG/$CCcamCLOG
fi


# read configs  ((c) SEG74)
proc_Get_CCcamcfg() {
  CCUSER=$(grep -i "WEBINFO USERNAME" $CCcamCFG | cut -d ":" -f2 | sed -e 's/ //g' | sed -e 's/\r//g')
  CCPASS=$(grep -i "WEBINFO PASSWORD" $CCcamCFG | cut -d ":" -f2 | sed -e 's/ //g' | sed -e 's/\r//g')
  CCPORT=$(grep -i "WEBINFO LISTEN PORT" $CCcamCFG | cut -d ":" -f2 | sed -e 's/ //g' | sed -e 's/\r//g')
  CCTPORT=$(grep -i "TELNETINFO LISTEN PORT" $CCcamCFG | cut -d ":" -f2 | sed -e 's/ //g' | sed -e 's/\r//g')      
  CCTUSER=$(grep -i "TELNETINFO USERNAME" $CCcamCFG | cut -d ":" -f2 | sed -e 's/ //g' | sed -e 's/\r//g')
  CCTPASS=$(grep -i "TELNETINFO PASSWORD" $CCcamCFG | cut -d ":" -f2 | sed -e 's/ //g' | sed -e 's/\r//g')
  #echo "CCUSER: $CCUSER"; echo "CCPASS: $CCPASS"; echo "CCPORT: $CCPORT"
  #echo "CCTUSER: $CCTUSER"; echo "CCTPASS: $CCTPASS"; echo "CCTPORT: $CCTPORT"           
}
proc_Get_CCcamcfg

if [ "$DETECTCHECK" = "yes" -a "$TELNETPORT" != "" -a "$CCTPORT" != "" ]; then
	if [ ! "$TELNETPORT" = "$CCTPORT" ]; then
		echo $ROT "ERROR: Your script-config isnt the same as your CCcam configuration!"
		echo "Your script setting: $TELNETPORT ... But in your CCcam.cfg is TELNETINFO-port: $CCTPORT"; $NORM
		proc_exit
	fi
	proc_Get_Packager
fi


case "$1" in
	start)
		echo "[SCRIPT] $1:"
		start_cam
	;;
	stop)
		echo "[SCRIPT] $1:"
		stop_cam
	;;
	restart)
		echo "[SCRIPT] $1:"
		stop_cam
		sleep 10
		start_cam
	;;
	check)
		echo "[SCRIPT] $1:"
		check_run
	;;
	card*)
		echo "[SCRIPT] $1:"
		check_cards
	;;
	respond*)
		echo "[SCRIPT] $1:"
		check_respond
	;;
	[lL][oO][gG])
		echo "[SCRIPT] $1:"
		monitor_log
	;;
	conf*)
		echo "[SCRIPT] $1:"
		proc_Get_Editor
		edit_file $1
	;;
	*)
		echo "Usage: $0 [start|stop|restart|check|cards|respond|log|conf]"
	;;
esac
proc_exit


Du musst nicht kämpfen um zu siegen

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 1 Gast