Block all IP addresses and domains that have attempted brute force SSH login to computer

(bzcat BZIP2_FILES && cat TEXT_FILES) | grep -E "Invalid user|PAM" | grep -o -E "from .+" | awk '{print $2}' | sort | uniq >> /etc/hosts.deny
Searches all log files (including archived bzip2 files) for invalid user and PAM authentication errors, both of which are indicative of brute force attempts at logging into computer. A list of all unique IP addresses and domain names is appended to hosts.deny. The command (and grep error messages) will work on Mac OS X 10.6, small adjustments may be needed for other OSs.

3
2010-01-03 04:41:51

1 Alternatives + Submit Alt

What Others Think

What if I just incorrectly type my password a couple of times?
kaedenn · 497 weeks and 6 days ago
Would it be possible to do it for every IP that had more than 5 failed login attempts?
gromburger · 497 weeks and 6 days ago
install the denyhosts package or fail2ban to get this automated for you. Good++ use of the commandline though :)
linuxrawkstar · 497 weeks and 5 days ago
like linuxrawkstar said... "sudo apt-get install fail2ban"
DrAzraelTod · 497 weeks and 3 days ago
@linuxrawkstar @DrAzraelTod Already installed denyhosts. :) The Mac-specific corrections are wrong on the website. Above command helped me figure out regexes for denyhosts. Love python and regexes.
jayhawkbabe · 497 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: