Skip to content

Instantly share code, notes, and snippets.

@kamermans
Created July 11, 2011 17:06
Show Gist options
  • Select an option

  • Save kamermans/1076290 to your computer and use it in GitHub Desktop.

Select an option

Save kamermans/1076290 to your computer and use it in GitHub Desktop.
Show status of all fail2ban jails at once
#!/bin/bash
JAILS=`fail2ban-client status | grep "Jail list" | sed -E 's/^[^:]+:[ \t]+//' | sed 's/,//g'`
for JAIL in $JAILS
do
fail2ban-client status $JAIL
done
@jmsfnch
Copy link

jmsfnch commented Oct 14, 2022

Ta very much!

@cchalifo999
Copy link

ty

@CDSoft
Copy link

CDSoft commented Dec 27, 2023

Thanks.

Since fail2ban banned lists all jails with their banned IP as a list of Python dictionaries, Python can also be used to get the list of jails:

$ fail2ban-client banned
[{'sshd': []}, {'apache-auth': []}, {'apache-badbots': []}, {'apache-noscript': ['AAA.BBB.CCC.DDD', 'AAA.BBB.CCC.DDD']}, {'apache-overflows': []}, {'apache-nohome': []}, {'apache-botsearch': []}, {'apache-fakegooglebot': []}, {'apache-modsecurity': []}, {'apache-shellshock': []}, {'php-url-fopen': []}, {'apache-pass': []}]
$ JAILS=$(python -c "print(' '.join(k for d in $(fail2ban-client banned) for k in d))")
$ echo $JAILS
sshd apache-auth apache-badbots apache-noscript apache-overflows apache-nohome apache-botsearch apache-fakegooglebot apache-modsecurity apache-shellshock php-url-fopen apache-pass

@abqcheeks
Copy link

abqcheeks commented Oct 13, 2025

Thanks for this, still relevant after all these years. I tweaked it with perl to print the totals in a quick table.

#!/bin/perl

open(J, "fail2ban-client status |") || die;
while (<J>) {
  next unless /Jail list/;
  s/^[^:]+:[ \t]+//;
  s/,//g;
  push(@jailnames, split);
}
close(J);

foreach $j (@jailnames) {
  open(S, "fail2ban-client status $j |") || die;
  while (<S>) {
    if (/Currently failed:\s+(\d+)/) {
      $jails->{$j}->{cf} = $1;
    } elsif (/Total failed:\s+(\d+)/) {
      $jails->{$j}->{tf} = $1;
    } elsif (/Currently banned:\s+(\d+)/) {
      $jails->{$j}->{cb} = $1;
    } elsif (/Total banned:\s+(\d+)/) {
      $jails->{$j}->{tb} = $1;
    }
  }
  close(S);
}

printf("%-22s  %12.12s  %12.12s  %12.12s  %12.12s\n",
       "JAIL", "Curr Failed", "Tot Failed", "Curr Banned", "Tot Banned");
print "-" x 78, "\n";
foreach $j (@jailnames) {
  printf("%-22s  %12.12s  %12.12s  %12.12s  %12.12s\n", $j,
         $jails->{$j}->{cf}, $jails->{$j}->{tf},
         $jails->{$j}->{cb}, $jails->{$j}->{tb});
}

@kamermans
Copy link
Author

Nice, thanks @abqcheeks, glad this is still useful after 14 years 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment