Swich statement based on a grep?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Swich statement based on a grep?
# 1  
Old 07-14-2008
Swich statement based on a grep?

I'm piping the results of a tcpdump into a shell script and want certain commands executed (specifically the firing of an SNMP alert) based on a grep for certain contents in the output of tcpdump.

For example, I have

Code:
#!/bin/bash
while read str; do
grep 'ttl 64' -q && sudo snmptrap -v 1 -c private 127.0.0.1 1.3.6.1.4.1.2011.0.6.4 "" 6 23 ""
grep 'ttl 128' -q && sudo snmptrap -v 1 -c private 127.0.0.1 1.3.6.1.4.1.2011.1.2.8 "" 6 23 ""
done

However, this simply waits for a string that matches ttl64 and then waits for one matching ttl128, I need it to continually spot matches and fire the packet based on which is matched. Any ideas for a better script?

Cheers.
# 2  
Old 07-14-2008
This is how it can be done using perl:
Code:
#!/usr/bin/perl

use File::Tail;
  $file=File::Tail->new("file");
    while (defined($line=$file->read)) {
       if ($line =~ /ttl 64|ttl 128/) { system('YOUR COMMAND'); };
}

# 3  
Old 07-14-2008
If you have ksh93 something like the following should work.
Code:
#!/bin/ksh93t

while read str
do
   [[ $str == *+(ttl 64)* ]] && sudo ......
   [[ $str == *+(ttl 128)* ]] && sudo .....
done < file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Solaris OS having issue accessing LUN from Qlogic swich

Hi Community, I am facing one issue related storage accessing from solaris 10 machine(M5000). it was working fine then we shifted to another site. the only change on that site is FC switch. The issue i am facing is from solaris machine i can able to see the LUN, it;s only a test lun of 30... (17 Replies)
Discussion started by: bentech4u
17 Replies

2. Shell Programming and Scripting

How to separate a statement based on some delimiter and store each field in a variable?

Hi, Variable1 = MKT1,MKT2,MKT3,MKT4 Now i want to store each of these value seperated by comma to a array and access each of the values. Also find out number of such values seperated by comma. Variable1 can have any number of values seperated by comma. Thanks :) (3 Replies)
Discussion started by: arghadeep adity
3 Replies

3. Shell Programming and Scripting

Using GREP in IF Statement

Hello All, I have 2 different pieces of code, I am confused why the Code1 is giving me the correct result where as the Code2 is not giving me correct result. It gives me always result as "Failure" irrespective of the "ERROR" word exists in logfile or not. may I know the reason why? I am using Bash... (17 Replies)
Discussion started by: Ariean
17 Replies

4. Shell Programming and Scripting

"if" statement based off "grep"

Hello, I am somewhat new to Linux/Unix. I am currently working on a shell script that is suppose to cat a file, grep the same file for a certain line, if that line is found save the file in a different location, else remove the file. This is a rough example of what I want. $Dating = False... (13 Replies)
Discussion started by: Amzerik
13 Replies

5. Shell Programming and Scripting

combine lines from two files based on an if statement

I'm rather new to programming, and am attempting to combine lines from 2 files in a way that is way beyond my expertise - any help would be appreciated! I need to take a file (file1) and add columns to it from another file (file2). However, a line from file2 should only be added to a given line... (3 Replies)
Discussion started by: Cheri
3 Replies

6. Shell Programming and Scripting

Grep statement

Hi All, Please can somebody advise that if I want to search a pattern xyz the grep command should only select xyz and not any other pattern containing xyz (ex abxyzcd) Regards (1 Reply)
Discussion started by: Shazin
1 Replies

7. Shell Programming and Scripting

Using grep in a test/if statement

Okay, well this is more or less my first attempt at writing a shell script. Anyways, here's my code: cd ${PATH} if then rm ${FILE} ./anotherScript else exit 1 fi exit 1 Anyways, it's a pretty simple script that is supposed to search for the... (4 Replies)
Discussion started by: cbo0485
4 Replies

8. Shell Programming and Scripting

Using grep in if statement

Can somebody please guide me towards right syntax: #!/bin/ksh if i = $(grep $NAME filename) echo "Name Found" else echo " Name not Found" fi I need to grep for $NAME in the file, and if it returns false, execute a series of commands and if true, exit out. The above is not the right... (3 Replies)
Discussion started by: chiru_h
3 Replies

9. Shell Programming and Scripting

case statement based on file availability

I need to make a select menu that gives options dynamically based on whether certain files are available. For instance: PS3="Open which readme? " select readme in This That do case "$readme" in This) open -a /Applications/TextEdit.app This.txt;; That) open -a... (6 Replies)
Discussion started by: Loriel
6 Replies

10. Shell Programming and Scripting

How can I get an if statement to execute based on number of lines in a file?

I need to have an if statement in a script to run if there are certain processes running. Easiest way I can see to do this is to run a ps and grep the results based on what I am looking for: $ ps -ef | grep wtrs --- webtrend 5046 1 0 May 12 ? 0:28 /webtrends/versions/6.1/wtrs_ui... (6 Replies)
Discussion started by: LordJezo
6 Replies
Login or Register to Ask a Question