pythoniptables:允许在端口1234上传入TCP通信时出现一个神秘错误

2024-04-29 03:15:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用Python编写iptables脚本。我想使用python iptables包,而不是调用iptables本身。不过,我很难得到一些基本的规则设置。我想使用过滤器链来接受端口1234上的传入TCP流量。所以我写了这个:

import iptc
chain = iptc.Chain(iptc.TABLE_FILTER,"INPUT")
rule = iptc.Rule()
target =  iptc.Target(rule,"ACCEPT")
match = iptc.Match(rule,'tcp')
match.dport='1234'
rule.add_match(match)
rule.target = target
chain.insert_rule(rule)

但是,当我运行这个程序时,我得到的是:

^{pr2}$

有没有人有使用python iptables的经验,可以启发我犯了什么错?在


Tags: 端口import脚本过滤器iptableschaintarget规则
1条回答
网友
1楼 · 发布于 2024-04-29 03:15:04

哦,刚刚注意到这个。你能给我一个最新的人头吗?我已经修复了大量的bug,并更新了python iptables,以便与最新的iptables版本一起工作。如果您仍然遇到问题,请在github上开罚单。在

有一点肯定不是很正确,那就是你没有在规则中设置协议:

import iptc
chain = iptc.Chain(iptc.TABLE_FILTER,"INPUT")
rule = iptc.Rule()

设置协议,例如:

^{pr2}$

然后你应该没事了:

target =  iptc.Target(rule,"ACCEPT")
match = iptc.Match(rule,'tcp')
match.dport='1234'
rule.add_match(match)
rule.target = target
chain.insert_rule(rule)

相关问题 更多 >