Problem with awk and script!

General support questions
Post Reply
julemtl
Posts: 2
Joined: 2018/03/06 14:24:04

Problem with awk and script!

Post by julemtl » 2018/03/06 14:47:36

Hi!
For the school, i have an exercice and even the teacher have difficulties to resolve this.
i have to write a script and i m supposed to use echo, grep, cut, sort, uniq, sed and awk.
and i ll be looking like:
---------------------------------------------------------------------------------------------
Date of execution of execution of the script : ---------------
Stats of failed connection on ssh by users
--------------------------------------------------------------------------------------------
|User account | Nbres of Failures | Last date of failure |
--------------------------------------------------------------------------------------------
|user$1 | X | Day xx HH:MM:SS |
|user$2 | X | Day xx HH:MM:SS |
--------------------------------------------------------------------------------------------

By now , i have :

# cat /var/log/secure* | grep "Failed password" | grep -v "invalid" > tmp1
# awk '{print $9} tmp1 | sort | uniq -c
#cat tmp1 | grep "user$1" | sort | tail -1 | awk ' {print $1,$2,$3}'
#cat tmp1 | grep "user$2" | sort | tail -1 | awk ' {print $1,$2,$3}'

I'm really lost now,
Thanks for your help
Jules

User avatar
TrevorH
Site Admin
Posts: 33216
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Problem with awk and script!

Post by TrevorH » 2018/03/06 16:24:45

# cat /var/log/secure* | grep "Failed password" | grep -v "invalid" > tmp1
So in this set of commands the first cat is completely unnecessary since you can tell grep or awk or whatever to read the file directly instead of reading standard input. Second, there's no real reason to create a temporary file since you can just pipe the output from teh first set of commands into the awk/sort/uniq that follows.

So your first set of commands cat /var/log/secure* | grep "Failed password" | grep -v "invalid" can be replaced with something like

awk '/Failed password/ { print $9" Day "$2" " $3 }' /var/log/secure | grep -v invalid | sort -r | uniq -c -w 8

My /var/log/secure doesn't contain a field with the Day of the week in it so if you need that (your description is unclear about what's needed) you're going to need to extract Month and day number then convert that to the DoW.
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

MartinR
Posts: 714
Joined: 2015/05/11 07:53:27
Location: UK

Re: Problem with awk and script!

Post by MartinR » 2018/03/06 16:39:41

If you really have to use all of the commands
i m supposed to use echo, grep, cut, sort, uniq, sed and awk
then it's a fairly pointless exercise in making life difficult. You only need AWK, it will do all that you want. I'm not about to do your homework for you, but basically the shell script is:

Code: Select all

#!/bin.sh
awk '
...
' /var/log/secure*
Read the awk man page a couple of times at least. Then you need to do the following:
  • Write section that matches with "Failed password". When it runs, ignore any "invalid" lines (hint: "next" is useful here). Next extract the information, format it and save in in an array indexed on the user. Also build up your running totals.
  • Write an END section which prints out your first 6 lines. Then use asorti() to sort the array. Finally use a counter to take each array element in turn and print the formatted string you saved earlier.

julemtl
Posts: 2
Joined: 2018/03/06 14:24:04

Re: Problem with awk and script!

Post by julemtl » 2018/03/06 16:58:33

Thanks for your help TrevorH and MartinR!

I m sorry for my unclear description of my problem , and i m not looking for someone to do my homework but just want to understand how to build a script.
I ll try to manage something with your hints. And i agree with you , i think this is pointless!

Jules

Post Reply