Я пишу код, используя python. Этот код открывает файл auth.log
и подсчитывает, сколько раз IP не удалось войти в систему. Если один и тот же IP-адрес не может подключиться более 30 раз, он добавляется к blacklist.txt
.
Мой код вроде как работает. Он обнаруживает два IP-адреса, которые не удалось войти в систему несколько раз. Однако должно быть три IP-адреса, а не два. Почему третий IP не будет обнаружен?
with open('auth.log') as failed_authlog: # Open log file as f_authlog
authlog = failed_authlog.read() # for counting failed IPs
ip_addresses = Counter(re.findall(r'authentication failure.*?rhost=([0-9.]*)\s', authlog)) # read for failed log in attempts
with open('blacklist.txt', 'w') as failed_blocked: # create blacklist for IP addresses
for ip_address, count in ip_addresses.items(): # which after counting
if count >= 30: # occured 30 or more times in the log file
failed_blocked.write('\n' + '{}\n'.format(ip_address) +'-' + str(count))
Пример соответствующих auth.log
записей:
Feb 3 08:35:27 j4-be02 sshd[32744]: reverse mapping checking getaddrinfo for reserve.cableplus.com.cn [211.167.103.172] failed - POSSIBLE BREAK-IN ATTEMPT!
Feb 3 08:35:27 j4-be02 sshd[32744]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=211.167.103.172 user=root
Feb 3 08:35:29 j4-be02 sshd[32744]: Failed password for root from 211.167.103.172 port 36610 ssh2