如何使用 bash 脚本更改 iptable 条目?
How can I change iptable entries using bash script?
我在家用路由器上嵌入了 Linux 固件 运行ning。当我以 root 身份从终端 运行 一条一条地执行以下命令时,它没有任何错误地工作并且达到了我的目的。我知道这不是一个安全的策略。这只是为了测试一些东西。
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
iptables -X
iptables -A INPUT -p tcp -i eth1 --dport 4444 -j ACCEPT
但是,当这是 运行 在 bash 脚本中作为 root 时,
#!/bin/bash
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
iptables -X
iptables -A INPUT -p tcp -i eth1 --dport 4444 -j ACCEPT
它给出了以下错误:
iptables: Bad policy name. Run `dmesg' for more information.
iptables: Bad policy name. Run `dmesg' for more information.
iptables: Bad policy name. Run `dmesg' for more information.
iptables: No chain/target/match by that name.
iptables: No chain/target/match by that name.
我已经确认 bash 脚本的最后一行没有错误地执行并且可以在 iptables 中看到该条目。但是,所有其他行都会引发错误。我究竟做错了什么?令人惊讶的是,同一个批处理脚本在我的 Ubuntu 机器上运行良好。
您是在 Windows 中创建脚本,还是以其他方式在路由器期望 Unix 行结尾 (LF) 的地方提供 Windows 行结尾 (CRLF)?
这将导致解释器在每个命令的末尾读取一个额外的不可打印字符,这将导致显示的错误。
您可以通过运行cat -v myScript.sh
查看。不正确的 Windows 行结尾将显示为:
iptables -P INPUT ACCEPT^M
iptables -P FORWARD ACCEPT^M
iptables -P OUTPUT ACCEPT^M
iptables -F^M
iptables -X^M
iptables -A INPUT -p tcp -i eth1 --dport 4444 -j ACCEPT
我在家用路由器上嵌入了 Linux 固件 运行ning。当我以 root 身份从终端 运行 一条一条地执行以下命令时,它没有任何错误地工作并且达到了我的目的。我知道这不是一个安全的策略。这只是为了测试一些东西。
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
iptables -X
iptables -A INPUT -p tcp -i eth1 --dport 4444 -j ACCEPT
但是,当这是 运行 在 bash 脚本中作为 root 时,
#!/bin/bash
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
iptables -X
iptables -A INPUT -p tcp -i eth1 --dport 4444 -j ACCEPT
它给出了以下错误:
iptables: Bad policy name. Run `dmesg' for more information.
iptables: Bad policy name. Run `dmesg' for more information.
iptables: Bad policy name. Run `dmesg' for more information.
iptables: No chain/target/match by that name.
iptables: No chain/target/match by that name.
我已经确认 bash 脚本的最后一行没有错误地执行并且可以在 iptables 中看到该条目。但是,所有其他行都会引发错误。我究竟做错了什么?令人惊讶的是,同一个批处理脚本在我的 Ubuntu 机器上运行良好。
您是在 Windows 中创建脚本,还是以其他方式在路由器期望 Unix 行结尾 (LF) 的地方提供 Windows 行结尾 (CRLF)?
这将导致解释器在每个命令的末尾读取一个额外的不可打印字符,这将导致显示的错误。
您可以通过运行cat -v myScript.sh
查看。不正确的 Windows 行结尾将显示为:
iptables -P INPUT ACCEPT^M
iptables -P FORWARD ACCEPT^M
iptables -P OUTPUT ACCEPT^M
iptables -F^M
iptables -X^M
iptables -A INPUT -p tcp -i eth1 --dport 4444 -j ACCEPT