How to go about Using a "Validate an IP script" in conjunction with Logfile?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to go about Using a "Validate an IP script" in conjunction with Logfile?
# 1  
Old 03-21-2011
How to go about Using a "Validate an IP script" in conjunction with Logfile?

Hi, so I have been trying to write a shell script to go through a log file and through that, generate another file with all the Valid IP addresses it finds. So there's the complication that there could be incomplete or invalid data which would disqualify it from making my "Valid IPs" file I need to generate.

So I already have working code/script which can check if a chunk of stuff with no spaces in it is one Valid IP or not. That seems to work fine.

Problem is, I don't know specifically what else do I need to do and/or code and how to use my function in conjunction with a log file and how to make things work together.

Though I have ways of reducing the data set of useless lines , (i.e. by calling grep), I'm not completely sure if that's best to do and how to best pass in the reduced data set where I can still traverse it and/or how to traverse it so that my function/script can work on Validating if each potential chunk is a real IP or not.
I am willing to edit this function to traverse several lines of potential chunks, but don't know how to traverse nor get each chunk within the lines.
Or what should I do?

For example, let's say log file has following format:

maryjane from 10.23.17.11
johnston4 from 20..70.81
mail server exception, port 3209
controller from 321.24.70.18
brady from 110.113.270.
carriemoon from 111.123.199.291
error type 209.134 handled
barkleyjohnson from 0.43.205.34
bishopqueen from 2.100.999.8
billyj from 71.253.82.114
firewall and security handler invoked Mar 15 05:24:33
freewill from 28.49.130.
marklouis from .111.250.230

Anyways, I don't know how to go about all this.
Code and suggestions would help and be appreciated

Thanks in advance.

Last edited by shellcow; 03-21-2011 at 05:11 PM..
# 2  
Old 03-21-2011
If you have grep that supports -o and -w, this should do the trick:
Code:
IPD='(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])'
grep -woE "$IPD\.$IPD\.$IPD\.$IPD" infile


Or using awk:
Code:
awk -v IPD='(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])' '{
 while(match($0,W IPD"\\."IPD"\\."IPD"\\."IPD W)) {
     print substr($0, RSTART+1, RLENGTH-2);
         $0=substr($0,RSTART+RLENGTH);
 }} ' W='[^[:digit:][:alpha:]_]' infile


Last edited by Chubler_XL; 03-21-2011 at 07:23 PM.. Reason: Added awk solution
# 3  
Old 03-21-2011
Thanks, I'll definitely try that out later. It might be easier.
I actually did find out on my own how to do what I needed. Trivial, but cryptic to see sometimes.
# 4  
Old 03-21-2011
If you want to get each string of numbers and dots from a file to pass to your function you could do something like this:

Code:
grep -oE "[.[:digit:]]*" infile | while read word
do
      your_function $word
done

or again if you don't have -o in your grep:

Code:
 sed 's/[^0-9.]/\
/g' infile | sed '/^$/d' | while read word
do
    your_function $word
done


Last edited by Chubler_XL; 03-21-2011 at 07:47 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

7. Shell Programming and Scripting

HowTo: reg expr doing grep "timestamp>$DesiredTime" logfile ?

I know I asked a similar question but I want to know if there is a regular expression existing that with a korn shell cmd, finds any timestamp data records in a file where it is greater then a timestamp in a shell variable ? something like : grep all records where it has a timestamp >... (5 Replies)
Discussion started by: Browser_ice
5 Replies

8. HP-UX

script running with "ksh" dumping core but not with "sh"

Hi, I have small script written in korn shell. When it is called from different script, its dumping core, but no core dump when we run it standalone. And its not dumping core if we run the script using "/bin/sh" instead of "ksh" Can some body please help me how to resolve this issue. ... (9 Replies)
Discussion started by: simhe02
9 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question