Last active
March 12, 2019 03:21
-
-
Save lorne-luo/36857e05c21f9ab0f40789b8ef4e932a to your computer and use it in GitHub Desktop.
update ssh banlist on centos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
bashCommand = ''' | |
last | awk '{ print $1"|"$3 }' | |
''' | |
process = subprocess.Popen(bashCommand,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT) | |
output, error = process.communicate() | |
lines=output.split('\n') | |
white_list=set() | |
for l in lines: | |
if not l: | |
continue | |
user,ip=l.split('|') | |
if not user or not ip: | |
continue | |
if ip.count('.')==3: | |
white_list.add(ip) | |
print(white_list) | |
bashCommand = ''' | |
lastb | awk '{ print $1"|"$3 }' | |
''' | |
process = subprocess.Popen(bashCommand,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT) | |
output, error = process.communicate() | |
lines=output.split('\n') | |
white_users=['luotao'] | |
ban_list=set() | |
for l in lines: | |
if not l: | |
continue | |
user,ip=l.split('|') | |
if not user or not ip or user in white_users: | |
continue | |
if ip.count('.')==3 and ip not in white_list: | |
ban_list.add(ip) | |
with open('/etc/hosts.deny', 'w+') as f: | |
for ip in ban_list: | |
f.write('sshd: %s\n'%ip) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment