如何将162端口重定向到1024以上的自定义端口用于snmp陷阱?

时间:2016-11-10 10:52:43

标签: linux permissions snmp iptables privileges

我写了一些代码来侦听162端口。它看起来像是

listenAddress = GenericAddress.parse(System.getProperty("snmp4j.listenAddress", "udp:0.0.0.0/162"));
TransportMapping transport;
if (listenAddress instanceof UdpAddress) {
    transport = new DefaultUdpTransportMapping((UdpAddress) listenAddress);
} else {
    transport = new DefaultTcpTransportMapping((TcpAddress) listenAddress);
}
snmp = new Snmp(dispatcher, transport);
snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());
snmp.listen();

但是代码只能由非root用户启动。所以当我运行程序时,我收到了一个错误。 java.net.bindexception permission denied.

我想将162端口重定向到16200.在我的代码中,我可以这样称呼它

listenAddress = GenericAddress.parse(System.getProperty("snmp4j.listenAddress", "udp:0.0.0.0/16200"));

然后我修改了/ etc / sysconfig / iptables,但它没有用。以下是iptables配置。

*nat
:PREROUTING ACCEPT [1379:235423]
:POSTROUTING ACCEPT [6:680]
:OUTPUT ACCEPT [6:680]
-A PREROUTING -p udp  -m udp --dport 162 -j REDIRECT --to-ports 16200
COMMIT
# Completed on Thu Nov 10 18:07:25 2016
# Generated by iptables-save v1.3.5 on Thu Nov 10 18:07:25 2016
*filter
:INPUT ACCEPT [38534:3129869]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [2052:284032]
-A INPUT -p tcp -m tcp --dport 161 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -p udp -m udp --dport 161 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 162 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -p udp -m udp --dport 162 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 161 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -p udp -m udp --dport 161 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 162 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -p udp -m udp --dport 162 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 16200 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -p udp -m udp --dport 16200 -m state --state NEW,ESTABLISHED -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 161 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -p udp -m udp --sport 161 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 162 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -p udp -m udp --sport 162 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 161 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -p udp -m udp --sport 161 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 162 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -p udp -m udp --sport 162 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 16200 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -p udp -m udp --sport 16200 -m state --state ESTABLISHED -j ACCEPT
COMMIT
# Completed on Thu Nov 10 18:07:25 2016
# Generated by iptables-save v1.3.5 on Thu Nov 10 18:07:25 2016
*mangle
:PREROUTING ACCEPT [39240:3206748]
:INPUT ACCEPT [38535:3129909]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [2052:284032]
:POSTROUTING ACCEPT [2052:284032]
COMMIT

如何配置iptables以便我可以在16200端口上获取陷阱消息?或者还有其他方法可以做到吗?

我很困惑。请给我一些建议。谢谢!

1 个答案:

答案 0 :(得分:0)

您必须正确配置iptables以将所有传入的UDP数据包从端口162转发到用户定义的端口(>1024)。

sudo iptables -t nat -A PREROUTING -i enp0s3 -p udp --dport 162 -j REDIRECT --to-port 5678

您应该使用正确的网络接口名称替换enp0s3。使用ifconfig查找以太网接口的名称。通常 - eth0。在此示例中,来自端口162的所有传入UDP数据包都将转发到UDP端口5678