netstat grep regex suspend script help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting netstat grep regex suspend script help
# 8  
Old 11-01-2012
Note: grep has a flag for counting:
Code:
grep ":9982" | wc -l

could be replaced by:
Code:
grep -c ":9982"



--
To prevent false positives, better use a delimiter at the end as well, a space in this case:
Code:
grep -c ":9982 "


Last edited by Scrutinizer; 11-01-2012 at 05:58 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 11-01-2012
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?

web address lonelycoder.com/redmine/boards/4/topics/906

I can't post urls yet.
# 10  
Old 11-01-2012
That URL is obsolete. Please post valid one.
# 11  
Old 11-02-2012
strange, here is what is on the page:

Code:
#!/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

Code:
#!/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

# 12  
Old 11-02-2012
Ok, you got the script & this can be added to your script. Now what kind of assistance you need?
# 13  
Old 11-02-2012
Quote:
Originally Posted by bipinajith
That URL is obsolete. Please post valid one.
Quote:
Originally Posted by bilboNIX
strange, here is what is on the page:
[..]
FWIW lonelycoder.com does not work, www.lonelycoder.com does. But it is good practice to post the code here anyway...
# 14  
Old 11-03-2012
I have slot in the bit of the code from the lonely coder page that I need as below but not sure what to do with the extra if and where to put it?

---------- Post updated at 01:12 PM ---------- Previous update was at 11:34 AM ----------

Code:
#!/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

---------- Post updated 03-11-12 at 09:14 AM ---------- Previous update was 02-11-12 at 01:12 PM ----------

I have tried:

Code:
#!/bin/bash
sleep 30


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
if [ `netstat -t | grep -c ":9982"` -ge 3 ]
then
      echo live tv
elif [ `netstat -t | grep -c "192.168.0.:microsoft-ds ESTABLISHED"` -ne 0 ]
then
      echo media playing
else
      echo pm-suspend
fi

but get error

Code:
/home/billy/bin/grep.sh: line 12: $i: syntax error: operand expected (error toke                    n is "$i")


Last edited by bilboNIX; 11-02-2012 at 03:10 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep regex

Hi everyone, I'm looking for a grep command to match the following pattern from a file: <EGS>10234567<EGS> I used this following command to do this: grep -E '^<EGS>{8}<EGS>' test.txt In output I got: <EGS>10234567<EGS> Till now it work, but if I add something at the end of the line... (2 Replies)
Discussion started by: Arnaudh78
2 Replies

2. UNIX for Beginners Questions & Answers

Grep in regex

Hello guys, Here i am writing a script in bash to check for a valid URL from a file using regex This is my input file http://www.yahoo.commmmmm http://www.google.com https://www.gooogle.co www.test6.co.in www.gmail.com www.google.co htt://www.money.com http://eeeess.google.com... (2 Replies)
Discussion started by: Meeran Rizvi
2 Replies

3. Shell Programming and Scripting

Expect script to suspend expecting for a time period.

I have a simple Expect script to power a system on and off in an endless loop looking for an ERROR message at which point the script should exit. But I need to skip the first 60 seconds after each power on or off and not exit if there are ERROR messages during that time. I thought I could use... (0 Replies)
Discussion started by: David_Gilhooly
0 Replies

4. AIX

How to grep PID and program name from netstat in AIX?

Hi All, I am using netstat on AIX to grep info on all open connections. However, unlike on Linux(Centos), I do not get the PID and program name using netstat on AIX. I need this info to be clubbed along with the information retrieved using netstat version of AIX. Is there a way this can be... (1 Reply)
Discussion started by: Vipin Batra
1 Replies

5. UNIX for Advanced & Expert Users

Regex to match Exact port number (netstat command)

Hi All, We have this regex:\\*.*?(.600).*?.(LISTEN|ESTABLISHED) OS = Solaris 10 The purpose of this regex is to match the ports in output of "netstat -an" and report if any ports between 6000-6009 are getting used. The only problem is if I have something like this (sample output as... (6 Replies)
Discussion started by: sk2code
6 Replies

6. Shell Programming and Scripting

shell or perl script using grep and regex

Hi, I have file stored in a directory containing information about subnet mask and next hop address in the following format 10.1.1.0/16, 255.255.0.0, 10.1.1.1 10.1.2.0/16, 255.255.0.0,10.1.2.1 here 10.1.1.0/16 represent range of ip address 10.1.1.1-10.1.1.16 given say an IP address... (1 Reply)
Discussion started by: termeric
1 Replies

7. Shell Programming and Scripting

How set filter netstat -an | grep -P '\:'38''

Hi, I can write sh script for Linux platform I run: netstat -an | grep -P '\:'38''| grep ESTABLISHED but result: # netstat -an | grep -P '\:'38''| grep ESTABLISHED tcp 0 0 172.16.1.107:383 172.16.1.81:49981 ESTABLISHED tcp 0 0... (8 Replies)
Discussion started by: ostapv
8 Replies

8. UNIX for Dummies Questions & Answers

| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello, I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide. ls -aLl /bin | grep "\(x\)" Which works, just highlights 'x' where ever, when ever. I'm trying to to get (?:) to work but... (4 Replies)
Discussion started by: MykC
4 Replies

9. Shell Programming and Scripting

suspend/restart a process in shell script

Hi, I have a task that Im stuck on. I have an elementary script named 'myscript' that prints "the script is running" once a second. It runs for 27 seconds. I need to write a 2nd script that starts 'myscript' and takes a parameter '$1' for a number. my 2nd script then needs to pause myscript... (1 Reply)
Discussion started by: daneensign
1 Replies

10. UNIX for Dummies Questions & Answers

need help on netstat -na |grep

Hi, I use the following cmd to view list of clients connected to my HPUX server netstat -na |grep Can anyone help me with the UNIX cmds to find list of clients connected to my HPUX server including idle time. Any help will be highly appreciated. Thanks MH (2 Replies)
Discussion started by: mhbd
2 Replies
Login or Register to Ask a Question