0

Background: Although I think Fail2Ban is great way of securing subsystems like Apache, it takes a lot of work to get all the info together. So I'm writing a BASH script to do that for me.

I need to put the 'get' options of fail2ban-client in an array ..

So far I isolated these using: f2b_opts_cmd="$(sudo fail2ban-client --help | grep -i 'get <jail>' | grep -vw 'act')"

On the commandline, it looks like:

get <JAIL> logpath                       gets the list of the monitored
get <JAIL> logencoding                   gets the encoding of the log files
get <JAIL> journalmatch                  gets the journal filter match for
get <JAIL> ignoreself                    gets the current value of the
get <JAIL> ignoreip                      gets the list of ignored IP
get <JAIL> ignorecommand                 gets ignorecommand of <JAIL>
get <JAIL> failregex                     gets the list of regular
get <JAIL> ignoreregex                   gets the list of regular
get <JAIL> findtime                      gets the time for which the filter
get <JAIL> bantime                       gets the time a host is banned for
get <JAIL> datepattern                   gets the patern used to match
get <JAIL> usedns                        gets the usedns setting for <JAIL>
get <JAIL> maxretry                      gets the number of failures
get <JAIL> maxlines                      gets the number of lines to buffer
get <JAIL> actions                       gets a list of actions for <JAIL>

However, the variable looks like:

get <JAIL> logpath gets the list of the monitored get <JAIL> logencoding gets the encoding of the log files get <JAIL> journalmatch gets the journal filter match for get <JAIL> ignoreself gets the current value of the get <JAIL> ignoreip gets the list of ignored IP get <JAIL> ignorecommand gets ignorecommand of <JAIL> get <JAIL> failregex gets the list of regular get <JAIL> ignoreregex gets the list of regular get <JAIL> findtime gets the time for which the filter get <JAIL> bantime gets the time a host is banned for get datepattern gets the patern .. etc.etc.

But I need to have something like:

f2b_opts=(logpath logencoding journalmatch ignoreself ignoreip ignorecommand failregex ignoreregex findtime bantime datepattern usedns maxretry maxlines actions)  

How do I get the word after the <JAIL> part?

After days of searching this forum and other sites, still haven't found a solution to this problem. Complicating factors here are the "less" and "greater than" signs and the fact that there are multiple "JAIL" words in the raw string (see example).

1 Answer 1

0

You can use the following command to translate the text given (I do not know if it's the input text file or the the result of f2b_opts_cmd="$(sudo fail2ban-client --help | grep -i 'get <jail>' | grep -vw 'act')", but that doesn't really matter). In both cases you can use this sed and tr command combination:

sed -e "s/get\s<JAIL>\s\([^ ]*\).*/\1/" input.txt | tr '\n' ' '

to get the following output:

logpath logencoding journalmatch ignoreself ignoreip ignorecommand failregex ignoreregex findtime bantime datepattern usedns maxretry maxlines actions

To function as a part of your command in a pipeline, remove the 'input.txt' part.

If I guess correctly, this may be what you want:

f2b_opts_cmd="$(sudo fail2ban-client --help | grep -i 'get <jail>' | grep -vw 'act' | sed -e "s/get\s<JAIL>\s\([^ ]*\).*/\1/" input.txt | tr '\n' ' ')"

But that's just a guess.

5
  • In your example above you used () brackets instead of {} brackets. May this cause your troubles?
    – zx485
    Commented Mar 2, 2020 at 20:49
  • Sorry about that, I didn't see the whole line. But a copy paste of your code left me with an empty string
    – Supr0
    Commented Mar 2, 2020 at 20:51
  • OMG, it works (when leaving the **input.txt" part out. Thank you so much!
    – Supr0
    Commented Mar 2, 2020 at 20:53
  • Oh spoke to soon .. when I execute the code on the commandline, it works perfectly, but when executed in a script, it has several "action" words ?? I thought I filtered that out using the grep -vw 'act'`
    – Supr0
    Commented Mar 2, 2020 at 21:23
  • Sorry, but with the given information that is impossible to reproduce for me. It's probably another question. Try to create a new Minimal, Verifiable Example in another question.
    – zx485
    Commented Mar 2, 2020 at 21:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .