【问题标题】:python-iptables: Cryptic error when allowing incoming TCP traffic on port 1234python-iptables:允许端口 1234 上的传入 TCP 流量时出现神秘错误
【发布时间】:2012-11-27 04:37:52
【问题描述】:

我想用 Python 编写一个 iptables 脚本。而不是调用 iptables 本身,我想使用 python-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)

但是,当我运行它时,我又遇到了这个问题:

Traceback (most recent call last):
  File "testing.py", line 9, in <module>
    chain.insert_rule(rule)
  File "/usr/local/lib/python2.6/dist-packages/iptc/__init__.py", line 1133, in insert_rule
    self.table.insert_entry(self.name, rbuf, position)
  File "/usr/local/lib/python2.6/dist-packages/iptc/__init__.py", line 1166, in new
    obj.refresh()
  File "/usr/local/lib/python2.6/dist-packages/iptc/__init__.py", line 1230, in refresh
    self._free()
  File "/usr/local/lib/python2.6/dist-packages/iptc/__init__.py", line 1224, in _free
    self.commit()
  File "/usr/local/lib/python2.6/dist-packages/iptc/__init__.py", line 1219, in commit
    raise IPTCError("can't commit: %s" % (self.strerror()))
iptc.IPTCError: can't commit: Invalid argument
Exception AttributeError: "'NoneType' object has no attribute 'get_errno'" in <bound method Table.__del__ of <iptc.Table object at 0x7fcad56cc550>> ignored

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

【问题讨论】:

  • 您是否以 root 身份运行脚本?
  • 是的,我设法加入了其他规则,但这一条不起作用:(

标签: python iptables


【解决方案1】:

哦,刚刚注意到这个。可以试一下github的最新头像吗?我已经修复了大量的错误并更新了 python-iptables 以使用最新的 iptables 版本。如果您仍然遇到问题,请在github 上开票。

肯定不太对的一件事是你没有在规则中设置协议:

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

设置协议,例如这里:

rule.protocol = 'tcp'

然后你应该没事:

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

【讨论】:

  • 我一直在使用 python-iptables 并且喜欢它。只是想说感谢您创建/工作。
  • 谢谢,我很感激。 0.3.0 刚刚发布了一些修复 - 可从 github 或 PyPI 获得。 :)
猜你喜欢
  • 2013-06-12
  • 2017-02-10
  • 2012-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-06
  • 1970-01-01
相关资源
最近更新 更多