sábado, 4 de abril de 2020

[EN] Regular Expressions

Hello everyone!

How do you carry the lockdown? At the moment nothing bad, taking advantage of the time to do things that before didn't have enough time to do.

One of these things is to study; I have finished with a very interesting 'sed' and 'awk' course, and I wanted to share some notes on regular expressions, to see what you think.

‘\b[Cc]olou?r\b’
\b :: boundaries, limits the word
? :: optional inclusion of this character
\. :: Exactly one single character
this example matches Color, color, Colour, colour

Anchors
'^' :: start of a string
'$' :: end of a string

Ranges
[] :: denote the ranges
'[A-Za-z]' :: any letter
'[0-9]' :: any number; it could be represented as \d
'[a-z_]' :: lower case character and underscore character
'[349]' :: matches number 3, number 4 and number 9; it would match 34, 49 or 349 because include those numbers
'[^4]' :: matches anything BUT 4
'[Ss]erver' :: matches Server and server

Boundaries and strings
\s :: any white space character (space, line return, tap) > \S :: NOT looking for a white space character
\b :: word boundary (may include hyphen as word separator) >  \B :: NOT looking for a word boundary
'\ssytem' :: Matches "file system"
'\bsystem' :: Matches "file system" and "file-system"
'\bpop[0-9]\b' :: Matches pop2 and pop3 but not pop3s (from /etc/services file)
'\bpop[0-9]\B' :: Not matches pop2 and pop3 but matches pop3s (from /etc/services file)

Quantifiers
'u*' :: Matches u zero or more times
'u?' :: Matches u zero or once only (optional)
'u+' :: Matches u once or more times
'u{3}' :: Matches uuu (u 3 times)


'^\s*#' :: Matches all kind of commented lines: with no space, with spaces and with tabs
'start\s*end' :: Matches all independently the spaces between 'start' and 'end'
'start\s?end' :: Matches 'start end' and 'startend'
'start\s{2}end' :: Matches the option with 2 spaces between 'start and 'end'
'[a-z]{2}[0-9]{1,2}' :: Matches a postcode [ab12 7af]; {1,2} means that it could happen once or twice

grep -E :: takes extended regular expressions :: egrep. RE are extended when they use {}, but not when they use []

 



sábado, 14 de marzo de 2020

[EN] Pills :: using grep and sed

Hello everyone!

Today I bring you a tiny grep pills, a couple of features that I did not know and I think they are very interesting



So let's go there:

Returns # of matches (similar to wc -l)
> grep -c pattern

Shows the line that matches and two lines after
> cat file | grep -A2 pattern

Shows the line that matches and two lines before
> cat file | grep -B2 pattern

Shows the line that matches, two lines before and and two lines after
> cat file | grep -C2 pattern


Extra pill: This reminds me that if you want to add a line to a file, for example, above or below a specific row, you can do it using sed

Imagine that you want to add a line to /etc/passwords file, one above and one below a user named 'esther'

Adds the new line just after the user that starts with 'esther'
> sed ' /^esther/ a newuser:x:1001:1000:New User:/home/user:/bin/sh' /etc/passwd

Adds the new line just before the user that starts with 'esther'
> sed ' /^esther/ i newuser:x:1001:1000:New User:/home/user:/bin/sh' /etc/passwd


domingo, 23 de febrero de 2020

[EN] Nmap cheat sheet

Hello again,

nmap is a wonderful tool, very powerful and with which you can do a lot of things. I love it, it is one of the first utilities that I install on a new system, and I have found this cheat sheet that you will love for sure!



I have it to consult it often, so you will tell me if it is also as useful to you as it is to me.

Source: Sans.org

viernes, 24 de enero de 2020

[EN] [OOC] My first tattoo

Exactly a month ago I did a little crazy thing that I wanted to do for a long time and I got my first tattoo. Now healed and perfect, it looks like this


Just wanted to share it with you, I hope you like it!

martes, 24 de diciembre de 2019

[EN] How IT people see each other

Have you ever thought about it? Like SysAdmin or DevOps, how do you see the other colleagues in the IT department? And best of all, how do you see them?


I have to confess that as SysAdmin, sometimes I find myself thinking the same thing ... doesn't it happen to you?

Anyway, just wanted to wish you have a Merry Christmas and a really Happy 2020!

viernes, 29 de noviembre de 2019

[EN] Cleaning old systemd journal logs

Have you found that your folder is too big?

esther@Raton:~# du -hs /var/log/journal/
6,5G    /var/log/journal/

or

esther@Raton:~# du -hs /run/log/journal/
2,5G    /run/log/journal/


And have you found that once there you can't read any log messages?

esther@Raton:/var/log/journal/xxxxxxxxxxxxxxxxxxxxxxx# tail system.journal
L�8M�ތp�+2uW����.��+0OÈ�
                         �+B�ةO\@H�+ւ���|�X��+.�{EPр�+��)���ב�+o�
                                                                 �PX���+q���r�� �+"��)F���+�N��6���h�+l@����+n��Bm�B���+�{۳O�z�h�+�Z-9a���+"_�����+*(W4W8,FP�2���*�땠�OT� %$�����.fV�9P�| �J�8��8������3.��p�MESSAGE=XXXX [...]


Don't worry, that's pretty normal.

First of all, you can read your systemd journal log files just using

>  journalctl

and explore its options; some pretty useful may be
     --system                Show the system journal
     --user                  Show the user journal for the current user
  -r --reverse               Show the newest entries first
  -o --output=STRING         Change journal output mode (short, short-precise,
  -a --all                   Show all fields, including long and unprintable
     --vacuum-size=BYTES     Reduce disk usage below specified size
     --vacuum-files=INT      Leave only the specified number of journal files
     --vacuum-time=TIME      Remove journal files older than specified time


For example, for cleaning old files we have different ways using the last three options

> journalctl --vacuum-time=15d
> journalctl --vacuum-size=1G

Or you can check the live logs from a systemctl service using

> journalctl -xef -u systemctl.service