【问题标题】:How can I change iptable entries using bash script?如何使用 bash 脚本更改 iptable 条目?
【发布时间】:2016-12-14 14:20:23
【问题描述】:

我有一个在家用路由器上运行的嵌入式 Linux 固件。当我以 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 机器上运行良好。

【问题讨论】:

  • 你是如何以 root 权限运行 shell 脚本的?你能告诉我们你是如何调用它的吗?
  • 路由器出厂时默认使用默认凭据运行 telnet 服务。

标签: linux bash shell iptables


【解决方案1】:

您是在 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

【讨论】:

  • 非常感谢!这正是问题所在!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-24
  • 2014-11-01
  • 2017-04-03
  • 1970-01-01
相关资源
最近更新 更多