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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章