netstat grep regex suspend script help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting netstat grep regex suspend script help
# 1  
Old 11-01-2012
Linux netstat grep regex suspend script help

Hi

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
Code:
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?

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

Any help would be appreciated Smilie

Last edited by Scrutinizer; 11-01-2012 at 05:34 PM.. Reason: code tags
# 2  
Old 11-01-2012
I have a question.

When you say 9982 four or more times, do you mean 4 lines with port number: 9982 or 4 or more alphanumeric character after port number: 9982?

Can you post a sample output of netstat which you are trying to capture for port number: 9982?
# 3  
Old 11-01-2012
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:

Code:
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


Last edited by Scrutinizer; 11-01-2012 at 05:34 PM.. Reason: code tags
# 4  
Old 11-01-2012
Try this code:-

Code:
#!/bin/bash

if [ `netstat -t | grep ":9982" | wc -l` -ge 4 ]
then
      exit 1
elif [ `netstat -t | grep "192.168.0.:microsoft-ds ESTABLISHED" | wc -l` -ne 0 ]
then
      exit 1
else
      echo pm-suspend
fi

I hope it helps.
# 5  
Old 11-01-2012
Thanks I'll try it.

Can you explain wc -l` -ge ?
# 6  
Old 11-01-2012
wc -l counts the number of lines
-ge Greater than or Equal to check
This User Gave Thanks to Yoda For This Post:
# 7  
Old 11-01-2012
Yes I think this will work after testing with echo, I now just need to google how to cron it.

Thank you!
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