I am trying to create a suspend script which will suspend the system only if it cant find port 9982 four or more times OR 192.168.0.:microsoft-ds ESTABLISHED in the output of netstat -t.
I am struggling with the 9982 bit, googling etc I came up with
if netstat -t|grep -P "(9982){4,}"
but this does not return anything when there is four or more instances of 9982.
Also is this script okay?
#!/bin/bash
if netstat -t|grep -P "(9982){4,}" ; then
exit
elif
netstat -t|grep -P "192.168.0.:microsoft-ds ESTABLISHED" ; then
exit
elif
echo pm-suspend
fi
I mean four or more instances of port 9982 in the whole of the output so it would match the below and more instances of port 9982 appearing in the output:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 htpc.local:9982 192.168.0.4:62101 ESTABLISHED
tcp 0 0 htpc.local:50767 htpc.local:9982 ESTABLISHED
tcp 0 0 htpc.local:40573 192.168.0.2:mysql TIME_WAIT
tcp 0 420 htpc.local:9982 192.168.0.4:62124 ESTABLISHED
tcp 0 0 htpc.local:9982 htpc.local:50767 ESTABLISHED
tcp 0 0 htpc.local:40574 192.168.0.2:mysql TIME_WAIT
tcp 0 0 htpc.local:40572 192.168.0.2:mysql TIME_WAIT
tcp 0 52 htpc.local:ssh 192.168.0.4:60655 ESTABLISHED
This script is working great but there is another issue I need to add to it. I dont want the machine to suspend if there is an active recording and unfortunately the port rule doesn't catch this however the below script is supposed to take account of recordings. Is it possible to incorporate the relevant section into mine?
#!/bin/bash
#
#
# modyfy if different location for tvheadend dvr/log path
cd ~hts/.hts/tvheadend/dvr/log
######################
start_date=0
stop_date=0
recording=0
pingcount=0
# wait for at least 60 seconds to let the calling recording expire
sleep 60
current_date=`date +%s`
for i in $( ls ); do
tmp_start=`cat $i | grep '"start":' | cut -f 2 -d " " | cut -f 1 -d ","`
tmp_stop=`cat $i | grep '"stop":' | cut -f 2 -d " " | cut -f 1 -d ","`
# check for any current recording
if [ $((tmp_stop)) -gt $((current_date)) -a $((tmp_start)) -lt $((current_date)) ]; then
recording=1
fi
done
#if [ $((recording)) -ne 0 ]; then
# echo "Recording in progress"
#fi
# check to see if there's an active client
pingcount=$(ping -c1 192.168.1.41 | grep 'received' | awk -F ',' '{print $2}' | awk '{ print $1}')
#echo "Pingcount is $pingcount"
# do the final check, no pingcount and no recording means we can suspend
#
if [ $((pingcount)) -eq 0 -a $((recording)) -eq 0 ]; then
#echo "XBMC is NOT active and NO recording in progress, so going into SUSPEND"
sudo /usr/sbin/pm-suspend
fi
---------- Post updated at 07:29 AM ---------- Previous update was at 06:17 AM ----------
this should work if i get my if s in the right place
#!/bin/bash
sleep 30
if [ `netstat -t | grep -c ":9982"` -ge 3 ]
then
exit 1
elif [ `netstat -t | grep -c "192.168.0.:microsoft-ds ESTABLISHED"` -ne 0 ]
then
exit 1
elif
cd ~billy/.hts/tvheadend/dvr/log
current_date=`date +%s`
for i in $( ls ); do
tmp_start=`cat $i | grep '"start":' | cut -f 2 -d " " | cut -f 1 -d ","`
tmp_stop=`cat $i | grep '"stop":' | cut -f 2 -d " " | cut -f 1 -d ","`
# check for any current recording
if [ $((tmp_stop)) -gt $((current_date)) -a $((tmp_start)) -lt $((current_date)) ]; then
echo recording in progress
fi
done
else
echo pm-suspend
fi
for i in $( ls ); do
if [ -f "$i" ]; then # Work only on files not directories.
tmp_start=`cat $i | grep '"start":' | grep -v script_name | cut -f 2 -d " " | cut -f 1 -d ","`
tmp_stop=`cat $i | grep '"stop":' | grep -v script_name | cut -f 2 -d " " | cut -f 1 -d ","`
# check for any current recording
if [ $((tmp_stop)) -gt $((current_date)) -a $((tmp_start)) -lt $((current_date)) ]; then
echo recording in progress
fi
fi
done
Where script_name is the file name of same script. So replace it with the file name of your script.