【发布时间】:2019-08-20 02:00:25
【问题描述】:
fail2ban 的其中一个操作被配置为运行 ruby 脚本;但是,当尝试执行 ruby 脚本时,fail2ban 会失败,并出现“找不到命令”错误。我不明白这个错误,因为我提供了 ruby 脚本的完整路径并且它具有执行权限:
这是我的 fail2ban 操作:
[root:a17924e746f0:~]# cat /etc/fail2ban/action.d/404.conf
# Fail2Ban action configuration file for Subzero/Core
[Definition]
actionstart =
actionstop =
actioncheck =
actionban = /root/ban_modify.rb ban <ip>
actionunban = /root/ban_modify.rb unban <ip>
这是/root/ban_modify.rb 脚本的内容:
#!/usr/bin/env ruby
command = ARGV[0]
ip_address = ARGV[1]
blacklist = File.open("/root/blacklist.txt").read.split("\n")
if command == "unban"
if blacklist.include? "#{ip_address} deny"
blacklist.delete "#{ip_address} deny"
end
elsif command == "ban"
blacklist << "#{ip_address} deny"
end
File.open("/root/blacklist.txt", "w") {|f| f.write(blacklist.join("\n"))}
非常简单。当满足 fail2ban 条件时,Apache 使用此 blacklist.txt 文件永久禁止个人访问 Web 服务器。
但是,当我发出以下命令时:sudo /usr/bin/fail2ban-client set 404 unbanip <my ip>
我收到以下错误:
2019-08-19 20:56:43,508 fail2ban.utils [16176]: Level 39 7ff7395873f0 -- exec: ban_modify.rb ban <myip>
2019-08-19 20:56:43,509 fail2ban.utils [16176]: ERROR 7ff7395873f0 -- stderr: '/bin/sh: 1: ban_modify.rb: not found'
2019-08-19 20:56:43,509 fail2ban.utils [16176]: ERROR 7ff7395873f0 -- returned 127
2019-08-19 20:56:43,509 fail2ban.utils [16176]: INFO HINT on 127: "Command not found". Make sure that all commands in 'ban_modify.rb ban <myip>' are in the PATH of fail2ban-server process (grep -a PATH= /proc/`pidof -x fail2ban-server`/environ). You may want to start "fail2ban-server -f" separately, initiate it with "fail2ban-client reload" in another shell session and observe if additional informative error messages appear in the terminals.
2019-08-19 20:56:43,509 fail2ban.actions [16176]: ERROR Failed to execute ban jail '404' action '404' info 'ActionInfo({'ip': '<myip>', 'family': 'inet4', 'ip-rev': '<myip>.', 'ip-host': '<myip>', 'fid': '<myip>', 'failures': 1, 'time': 1566266203.3465006, 'matches': '', 'restored': 0, 'F-*': {'matches': [], 'failures': 1}, 'ipmatches': '', 'ipjailmatches': '', 'ipfailures': 1, 'ipjailfailures': 1})': Error banning <myip>
如果 actionban 指向 ruby 脚本的完整路径,我不确定为什么会发生此错误。
我什至尝试将/root/ban_modify.rb 的内容更改为简单的puts "Hello World"。尝试将禁令更改为 iptables-allports ,但仍然失败。似乎禁令根本不起作用。
【问题讨论】:
-
建议的命令 (
grep -a PATH= /proc/`pidof -x fail2ban-server`/environ) 返回什么?