OpenWrt打開和關閉指定端口shell腳本

最近使用小米路由器插件Misstar Tools,因爲它在小米路由器3上使用的端口爲1024,而且沒有密碼,所以把這個端口暴露在公網上就非常不安全。但有時候我們確實就想在公網上使用這個插件,比如使用它的網絡喚醒功能,省了168塊向日葵開機棒的錢,免費的它不香嗎?

Misstar

所以寫了一個shell腳本,實際調用的是iptables命令,理論上OpenWrt上面應該都能用,可以在需要使用時打開它使用的端口,不使用時關閉,增加安全性。

因爲小米路由器默認的規則是不打開的端口全部禁止,所以只要添加允許指定端口訪問規則就好了,不允許刪除該規則即可。

腳本plugin_ports.sh如下:

#!/bin/sh
# author: yasin

SERVICE_TCP_PORTS="1024,12666"

enable_tcp_ports(){
	echo "Open ports $SERVICE_TCP_PORTS."
	iptables -I INPUT -p tcp  -m multiport --dport $SERVICE_TCP_PORTS -j ACCEPT
	iptables-save
	echo "enable tcp ports $SERVICE_TCP_PORTS success." 
}
			
disable_tcp_ports(){
	rule_number=`iptables -L -n --line-number | grep $SERVICE_TCP_PORTS | awk '{print $1}'`
	if  [ ! -n "$rule_number" ] ;then
		echo "Ports $SERVICE_TCP_PORTS dont't have iptables rules."
	else
		echo "Delete input rule $rule_number."
		iptables -D INPUT $rule_number
	fi
	echo "disable tcp ports $SERVICE_TCP_PORTS success." 
}
						
case $1 in
enable)
	enable_tcp_ports;;
disable)
	disable_tcp_ports;;
*)
	echo "Usage:`basename $0` {enable|disable}";;
esac
  • 打開端口:sh plugin_ports.sh enable
  • 關閉端口:sh plugin_ports.sh disable
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章