iptables 按时间限制访问

Man*_*a 7 4 iptables

如何控制每个IP的上网时间?

如何为该规则添加时间:iptables -I FORWARD -s 192.168.0.56 -j ACCEPT?

我试过这个

iptables -I FORWARD -s 192.168.0.56 -m time --timestart 13:00 --timestop 14:00 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

iptables -I FORWARD -s 192.168.0.56 --match time --weekdays Mon,Tue,Wed,Thu,Fri --timestart 09:00 --timestop 10:00 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

但它不起作用

也许有另一种方法可以做到?

Operating system: Ubuntu 18.04.4 LTS

root@router:/home/wlodek# iptables -L FORWARD
Chain FORWARD (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  192.168.0.56         anywhere             TIME from 09:00:00 to 10:00:00 on Mon,Tue,Wed,Thu,Fri UTC
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
Run Code Online (Sandbox Code Playgroud)

IP: 192.168.0.56 -有连接 iptables -I FORWARD -s 192.168.0.56 -j ACCEPT

IP:192.168.0.56 -无连接 iptables -I FORWARD -s 192.168.0.56 -j ACCEPT +时间

Jon*_*nas 6

iptables 正在使用 UTC 时间而不是您的本地时间。

试试下面的公式:

iptables -I FORWARD -s 192.168.0.56 --match time --weekdays Mon,Tue,Wed,Thu,Fri --timestart $(date -u -d @$(date "+%s" -d "09:00") +%H:%M) --timestop $(date -u -d @$(date "+%s" -d "10:00") +%H:%M) -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

这会将您的本地开始和结束时间转换为 UTC,然后再将其移交给iptables.

当然,您可以用任何其他时间替换开始时间09:00和结束10:00时间。