Is there a BASH script allowing me to grep specifics from /var/log/messages?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is there a BASH script allowing me to grep specifics from /var/log/messages?
# 1  
Old 03-24-2017
Wrench Is there a BASH script allowing me to grep specifics from /var/log/messages?

I am wondering if there is a script (if one exists, not confident in my own scripting ability) that is able to bring up specified information from the /var/log/messages. I need to show logged traffic on specific dates and times and protocols (ie. Show all insecure FTP traffic (most likely via port number) on October 23 between 12:30pm and 12:35pm). Is there a script that can do this? No matter what I try, mine aren't working too well
# 2  
Old 03-24-2017
Almost sure: yes.

Had we a specification worth its name, we could help further.
And, post your attempts so they can be analysed, discussed, or even improved.
# 3  
Old 03-24-2017
Show some lines from your log file. Hopefully the dates look like YYYY MM DD HH MM SS, that kind of timestamp is really easy to compare, everything else will mean parsing every single line to compute its date.
# 4  
Old 03-27-2017
Sure, here is the code I have so far that is not working:

Code:
#!/bin/bash

read -p "Enter month (first 3 letters): " month

read -p "Enter day of month: " day

read -p "Enter starting time (HH:MM:SS): " stime

read -p "Enter ending time (HH:MM:SS): " ftime

read -p "Enter chain: " chain

read -p "Enter any other modifiers (TTL, SRC, DSP, SPT, DPT, IN, OUT, etc): " modifier

if [ -z "$month" ]; then
    month='IN='
fi

if [ -z "$chain" ]; then
    chain='IN='
fi

if [ -z "$modifier" ]; then
    modifier='IN='
fi

if [ -z "$stime" ] && [ -z "$ftime" ]; then

cat /var/log/messages | grep -i "$month $day" | grep -i $chain | grep -i $modifier

else

cat /var/log/messages | grep -i "$month $day" | grep -i $chain | grep -i $modifier | sed -n "/$stime/,/$ftime/p"

fi

And here is an example of the /var/log/messages traffic:

Code:
Mar 27 10:24:29 router kernel: [ 2298.775662] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1791 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:31 router kernel: [ 2301.516556] FORWARD-ACCEPTEDIN=eth1 OUT=eth3 MAC=00:0c:29:91:18:81:00:0c:29:02:2c:a0:08:00 SRC=195.165.11.5 DST=195.165.11.70 LEN=69 TOS=0x00 PREC=0x00 TTL=127 ID=5181 PROTO=UDP SPT=59696 DPT=53 LEN=49 
Mar 27 10:24:32 router kernel: [ 2301.619601] FORWARD-DROPPEDIN=eth3 OUT=eth0 MAC=00:0c:29:91:18:95:00:0c:29:46:29:d3:08:00 SRC=195.165.11.70 DST=199.7.83.42 LEN=80 TOS=0x00 PREC=0x00 TTL=127 ID=32098 PROTO=UDP SPT=57894 DPT=53 LEN=60 
Mar 27 10:24:32 router kernel: [ 2301.619641] FORWARD-DROPPEDIN=eth3 OUT=eth0 MAC=00:0c:29:91:18:95:00:0c:29:46:29:d3:08:00 SRC=195.165.11.70 DST=202.12.27.33 LEN=69 TOS=0x00 PREC=0x00 TTL=127 ID=26672 PROTO=UDP SPT=57894 DPT=53 LEN=49 
Mar 27 10:24:34 router kernel: [ 2304.527738] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1792 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:35 router kernel: [ 2305.243755] FORWARD-ACCEPTEDIN=eth3 OUT=eth1 MAC=00:0c:29:91:18:95:00:0c:29:46:29:d3:08:00 SRC=195.165.11.70 DST=195.165.11.5 LEN=69 TOS=0x00 PREC=0x00 TTL=127 ID=8386 DF PROTO=UDP SPT=53 DPT=59696 LEN=49 
Mar 27 10:24:35 router kernel: [ 2305.244995] FORWARD-ACCEPTEDIN=eth1 OUT=eth3 MAC=00:0c:29:91:18:81:00:0c:29:02:2c:a0:08:00 SRC=195.165.11.5 DST=195.165.11.70 LEN=69 TOS=0x00 PREC=0x00 TTL=127 ID=5182 PROTO=UDP SPT=57560 DPT=53 LEN=49 
Mar 27 10:24:35 router kernel: [ 2305.245255] FORWARD-DROPPEDIN=eth3 OUT=eth0 MAC=00:0c:29:91:18:95:00:0c:29:46:29:d3:08:00 SRC=195.165.11.70 DST=192.58.128.30 LEN=69 TOS=0x00 PREC=0x00 TTL=127 ID=7802 PROTO=UDP SPT=58259 DPT=53 LEN=49 
Mar 27 10:24:35 router kernel: [ 2305.277867] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1793 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:36 router kernel: [ 2306.030264] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1794 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:36 router kernel: [ 2306.249173] FORWARD-ACCEPTEDIN=eth1 OUT=eth3 MAC=00:0c:29:91:18:81:00:0c:29:02:2c:a0:08:00 SRC=195.165.11.5 DST=195.165.11.70 LEN=69 TOS=0x00 PREC=0x00 TTL=127 ID=5183 PROTO=UDP SPT=57560 DPT=53 LEN=49 
Mar 27 10:24:37 router kernel: [ 2306.885204] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1795 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:37 router kernel: [ 2307.264584] FORWARD-ACCEPTEDIN=eth1 OUT=eth3 MAC=00:0c:29:91:18:81:00:0c:29:02:2c:a0:08:00 SRC=195.165.11.5 DST=195.165.11.70 LEN=69 TOS=0x00 PREC=0x00 TTL=127 ID=5184 PROTO=UDP SPT=57560 DPT=53 LEN=49 
Mar 27 10:24:38 router kernel: [ 2307.634718] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1796 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:38 router kernel: [ 2308.384824] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1797 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:39 router kernel: [ 2309.279134] FORWARD-ACCEPTEDIN=eth1 OUT=eth3 MAC=00:0c:29:91:18:81:00:0c:29:02:2c:a0:08:00 SRC=195.165.11.5 DST=195.165.11.70 LEN=69 TOS=0x00 PREC=0x00 TTL=127 ID=5185 PROTO=UDP SPT=57560 DPT=53 LEN=49 
Mar 27 10:24:39 router kernel: [ 2309.339155] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1798 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:40 router kernel: [ 2309.773505] FORWARD-DROPPEDIN=eth3 OUT=eth0 MAC=00:0c:29:91:18:95:00:0c:29:46:29:d3:08:00 SRC=195.165.11.70 DST=198.41.0.4 LEN=69 TOS=0x00 PREC=0x00 TTL=127 ID=10024 PROTO=UDP SPT=58259 DPT=53 LEN=49 
Mar 27 10:24:40 router kernel: [ 2310.089359] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1799 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:41 router kernel: [ 2310.839629] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1800 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:42 router kernel: [ 2311.991505] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1801 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:43 router kernel: [ 2312.741286] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1802 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:43 router kernel: [ 2313.292604] FORWARD-ACCEPTEDIN=eth1 OUT=eth3 MAC=00:0c:29:91:18:81:00:0c:29:02:2c:a0:08:00 SRC=195.165.11.5 DST=195.165.11.70 LEN=69 TOS=0x00 PREC=0x00 TTL=127 ID=5186 PROTO=UDP SPT=57560 DPT=53 LEN=49 
Mar 27 10:24:43 router kernel: [ 2313.396563] FORWARD-DROPPEDIN=eth3 OUT=eth0 MAC=00:0c:29:91:18:95:00:0c:29:46:29:d3:08:00 SRC=195.165.11.70 DST=199.7.83.42 LEN=69 TOS=0x00 PREC=0x00 TTL=127 ID=32099 PROTO=UDP SPT=58259 DPT=53 LEN=49 
Mar 27 10:24:43 router kernel: [ 2313.396740] FORWARD-DROPPEDIN=eth3 OUT=eth0 MAC=00:0c:29:91:18:95:00:0c:29:46:29:d3:08:00 SRC=195.165.11.70 DST=193.0.14.129 LEN=69 TOS=0x00 PREC=0x00 TTL=127 ID=26639 PROTO=UDP SPT=58259 DPT=53 LEN=49 
Mar 27 10:24:43 router kernel: [ 2313.493634] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1803 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:45 router kernel: [ 2315.046166] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1804 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:46 router kernel: [ 2315.796555] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1805 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:46 router kernel: [ 2316.546721] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1806 PROTO=UDP SPT=137 DPT=137 LEN=58 
Mar 27 10:24:47 router kernel: [ 2317.292821] FORWARD-ACCEPTEDIN=eth1 OUT=eth3 MAC=00:0c:29:91:18:81:00:0c:29:02:2c:a0:08:00 SRC=195.165.11.5 DST=195.165.11.70 LEN=69 TOS=0x00 PREC=0x00 TTL=127 ID=5187 PROTO=UDP SPT=64648 DPT=53 LEN=49 
root@router:/home/ajfoncec/Checkpoint_4_Scripts#


Last edited by Corona688; 03-27-2017 at 12:57 PM..
# 5  
Old 03-27-2017
Code tags for code please.

OK, some sadist printed the dates in MON D HH:MM:SS order and we'll need to convert them before comparing.

Code:
$ awk -v FIRST='03 27 10:24:30' -v LAST='03 27 10:24:35'  'BEGIN {
        split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", A); # A[1]=Jan, etc
        for(X in A) A[A[X]]=sprintf("%02d",X) # Convert A[1]=Jan to A[Jan]=01
}
{ $1=A[$1] ; $2=sprintf("%02d", $2); } # Substitute two digit months and days into first two fields
($0 >= FIRST) && ($0 <= LAST)' logentryfile

03 27 10:24:31 router kernel: [ 2301.516556] FORWARD-ACCEPTEDIN=eth1 OUT=eth3 MAC=00:0c:29:91:18:81:00:0c:29:02:2c:a0:08:00 SRC=195.165.11.5 DST=195.165.11.70 LEN=69 TOS=0x00 PREC=0x00 TTL=127 ID=5181 PROTO=UDP SPT=59696 DPT=53 LEN=49
03 27 10:24:32 router kernel: [ 2301.619601] FORWARD-DROPPEDIN=eth3 OUT=eth0 MAC=00:0c:29:91:18:95:00:0c:29:46:29:d3:08:00 SRC=195.165.11.70 DST=199.7.83.42 LEN=80 TOS=0x00 PREC=0x00 TTL=127 ID=32098 PROTO=UDP SPT=57894 DPT=53 LEN=60
03 27 10:24:32 router kernel: [ 2301.619641] FORWARD-DROPPEDIN=eth3 OUT=eth0 MAC=00:0c:29:91:18:95:00:0c:29:46:29:d3:08:00 SRC=195.165.11.70 DST=202.12.27.33 LEN=69 TOS=0x00 PREC=0x00 TTL=127 ID=26672 PROTO=UDP SPT=57894 DPT=53 LEN=49
03 27 10:24:34 router kernel: [ 2304.527738] INPUT-DROPPEDIN=eth0 OUT= MAC=ff:ff:ff:ff:ff:ff:00:50:56:c0:00:08:08:00 SRC=192.168.48.1 DST=192.168.48.255 LEN=78 TOS=0x00 PREC=0x00 TTL=64 ID=1792 PROTO=UDP SPT=137 DPT=137 LEN=58

$

Code in red is where you change the date range to what you want. Dates are in MM DD HH:MM:SS order.

Code in blue is the code which does the actual work, everything else is putting the date in proper MM DD HH:MM:SS order.
# 6  
Old 03-29-2017
Okay so...do I add the red part to the top of the code I posted earlier? or is this a stand-alone script? Also, is it possible to allow me to input the ports/times/dates outside the script rather than editing the script each time I want to use it?

Also, the bottom lines of your code appear to be copied from my /var/log/messages example. I'm not sure if that would work as I would be monitoring traffic from a variety of machines, varying MAC addresses, etc. So I don't think that would work within the code if it's looking for the same machine each time it's run (unless that wasn't meant to be within the code?)
# 7  
Old 03-29-2017
No. The bottom lines in the Corona688's post were the output produced by running the script he provided with an input file named logentryfile containing the sample data you showed us in post #4. If you want to parameterize his suggestion and read data from standard input (instead of from a file named logentryfile), change your script to something like:
Code:
#!/bin/bash
IAm=${0##*/}
if [ $# -ne 2 ]
then	printf 'Usage: %s start end
	where start and end are starting and ending dates and times in the format
		"MM DD hh:mm:ss"
	representing the start and end times to be selected from the logfile found
	on standard input.
'	"$IAm" >&2
	exit 1
fi
awk -v FIRST="$1" -v LAST="$2"  '
BEGIN {
	split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", A); # A[1]=Jan, etc
	for(X in A) A[A[X]]=sprintf("%02d",X) # Convert A[1]=Jan to A[Jan]=01
}
{	$1=A[$1]
	$2=sprintf("%02d", $2)
} # Substitute two digit months and days into first two fields
($0 >= FIRST) && ($0 <= LAST) # Select and print entries in range.'

When you invoke this script, give it two quoted operands containing your desired start and end dates and times and pipe the logfile you want it to process into it or, if the data is in a file, redirect the input to the script from that file.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[bash] script is filling up my /var/log

I am trying to create a script that checks if my VPN connection is up and running... Everything seems to work as except but for some reason, the script fills up my /var/log/auth.log with the below information Dec 13 01:07:44 debian sudo: soichiro : TTY=pts/0 ; PWD=/home/soichiro/Desktop ;... (5 Replies)
Discussion started by: soichiro
5 Replies

2. Shell Programming and Scripting

Transfer the logs being thrown into /var/log/messages into another file example /var/log/volumelog

I have been searching and reading about syslog. I would like to know how to Transfer the logs being thrown into /var/log/messages into another file example /var/log/volumelog. tail -f /var/log/messages dblogger: msg_to_dbrow: no logtype using missing dblogger: msg_to_dbrow_str: val ==... (2 Replies)
Discussion started by: kenshinhimura
2 Replies

3. Shell Programming and Scripting

Log all the commands input by user at real time in /var/log/messages

Below is my script to log all the command input by any user to /var/log/messages. But I cant achieve the desired output that i want. PLease see below. function log2syslog { declare COMMAND COMMAND=$(fc -ln -0) logger -p local1.notice -t bash -i -- "$USER:$COMMAND" } trap... (12 Replies)
Discussion started by: invinzin21
12 Replies

4. Shell Programming and Scripting

Script to monitor /var/log/messages

Hello All, I want to write a script to monitor my product logs from /var/log/messages and send notifications without using "tail -f" command.Please suggest alternatives and any other tools for monitoring and alerting. Thank You (1 Reply)
Discussion started by: Cva2568
1 Replies

5. UNIX for Dummies Questions & Answers

fprintd messages in /var/log/messages

Whenever a user uses su I get the following error messages in /var/log/messages: Nov 23 04:24:55 <REMOVED> abrt: saved core dump of pid 26141 (/usr/libexec/fprintd) to /var/spool/abrt/ccpp-1322018695-26141.new/coredump (753664 bytes) Nov 23 04:24:55 <REMOVED> abrtd: Directory... (3 Replies)
Discussion started by: JakesHat
3 Replies

6. UNIX for Dummies Questions & Answers

/etc/sudoers for allowing oracle user to /var/log/messages

So I want the DBA to access /var/log/messages and so I logged in as root and then edited the sudoers file as follows "oracle ALL= (root) /bin/view, /var/log/messages" However when I login as oracle and try "sudo more /var/log/messages" I get Sorry, user oracle is not allowed to... (1 Reply)
Discussion started by: gubbu
1 Replies

7. Shell Programming and Scripting

How can view log messages between two time frame from /var/log/message or any type of log files

How can view log messages between two time frame from /var/log/message or any type of log files. when logfiles are very big and especially many messages with in few minutes, I would like to display log messages between 5 minute interval. Could you pls give me the command? (1 Reply)
Discussion started by: johnveslin
1 Replies

8. Solaris

Difference between /var/log/syslog and /var/adm/messages

Hi, Is the contents in /var/log/syslog and /var/adm/messages are same?? Regards (3 Replies)
Discussion started by: vks47
3 Replies

9. UNIX for Advanced & Expert Users

/var/adm/messages vs /var/log/messages

The /var/adm/messages in Solaris seem to log more system messages/errors compared to /var/log/messages in Linux. I checked the log level in Linux and they seem OK. Is there any other log file that contains the messages or is it just that Linux doesn't log great many things? (2 Replies)
Discussion started by: gomes1333
2 Replies

10. Solaris

diff b/w /var/log/syslog and /var/adm/messages

hi sirs can u tell the difference between /var/log/syslogs and /var/adm/messages in my working place i am having two servers. in one servers messages file is empty and syslog file is going on increasing.. and in another servers message file is going on increasing but syslog file is... (2 Replies)
Discussion started by: tv.praveenkumar
2 Replies
Login or Register to Ask a Question