2016-12-14 18 views
0

我有在家庭路由器上運行的嵌入式Linux固件。當我以root身份從終端一個接一個地運行以下命令時,它的工作原理沒有任何錯誤,並符合我的目的。我知道這不是一個安全的政策。這只是爲了測試一些東西。如何使用bash腳本更改iptable條目?

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機器上工作正常。

+0

您是如何以root特權運行shell腳本的?你能讓我們知道你是如何引用它的嗎? – Inian

+0

該路由器附帶默認憑證默認運行的telnet服務。 –

回答

2

您是在Windows中創建腳本還是以某種其他方式爲Windows線路結尾(CRLF)設置路由器期望的Unix線路結尾(LF)?

這將導致解釋器在每個命令的末尾讀取額外的不可打印字符,這會導致顯示的錯誤。

您可以通過運行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 
+0

非常感謝你!這正是問題所在! –