找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 1570|回复: 17

[Router OS] 恳请大佬帮忙修改ROS自动获取修改端口映射命令

[复制链接]
本人ROS小白。得到一个脚本,意思是每次ROS重启后,WAN口得到的外网IP地址都会改变,这个脚本的意思就是把内网192.168.1.8这个IP映射出去。在每次ROS路由重启后自动检测到WAN口的公网IP改变时,就更改NAT的映射规则。并且每分钟检测一次。而且我用的是V6.49.10的版本,好像粘帖后不会执行。感谢!

具体的肢本如下:



#!/bin/bash# 定义变量external_port=8080  # 外部端口internal_ip=192.168.1.8  # 内部IP地址internal_port=80  # 内部端口current_external_ip=""  # 当前外部IP地址# 获取当前外部IP地址get_external_ip() {  current_external_ip=$(wget -qO- https://ipecho.net/plain)}# 更新端口映射配置update_port_mapping() {  # 删除原有NAT规则  /ip firewall nat remove [find comment="port-map"]  # 创建NAT规则  /ip firewall nat add chain=dstnat action=dst-nat protocol=tcp dst-address=0.0.0.0/0 dst-port="$external_port" to-addresses="$internal_ip" to-ports="$internal_port" comment="port-map"  # 创建防火墙接受规则  /ip firewall filter add chain=input action=accept protocol=tcp dst-port="$external_port" comment="port-map"}# 检查外部IP地址是否有变化check_ip_change() {  get_external_ip  if [[ "$current_external_ip" != "$stored_external_ip" ]]; then    stored_external_ip="$current_external_ip"        # 更新端口映射配置    update_port_mapping  fi}# 主循环while true; do  check_ip_change  sleep 60  # 每隔60秒检查一次done


我的恩山、我的无线 The best wifi forum is right here.
啥玩意啊整的这么复杂。直接映射不就可以了吗
/ip firewall nat

add action=dst-nat chain=dstnat comment="12345" \
    dst-port=12345 in-interface=pppoe-out1 protocol=tcp to-addresses=\
    10.10.10.9 to-ports=12345
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| | 显示全部楼层
oh_Jhon 发表于 2023-9-8 16:29
啥玩意啊整的这么复杂。直接映射不就可以了吗
/ip firewall nat

说明一下脚本意思啊大佬。12345代表什么?而10.10.10.9又是什么?
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

50439488 发表于 2023-9-8 17:05
说明一下脚本意思啊大佬。12345代表什么?而10.10.10.9又是什么?

/ip firewall nat
add action=dst-nat chain=dstnat comment="你要备注的内容" \
    dst-port=你的外网端口 in-interface=你的外网接口 protocol=tcp to-addresses=\
    你的内网IP地址 to-ports=你的内网端口
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| | 显示全部楼层
oh_Jhon 发表于 2023-9-8 17:14
/ip firewall nat
add action=dst-nat chain=dstnat comment="你要备注的内容" \
    dst-port=你的外网 ...

我没有固定的公网IP,路由每次重启后都得到一个不一样公网IP。你这个脚本会自动的更改映射NAT规则里的外网IP吗?谢谢
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

一般路由器都有的功能,怎么这么复杂呢?难道没有虚拟服务器(端口映射)的插件?任意外网映射到内网固定地址,但外网地址变化了,你也是要用ddns动态域名才好访问吧
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

aming.ou 发表于 2023-9-8 18:49
一般路由器都有的功能,怎么这么复杂呢?难道没有虚拟服务器(端口映射)的插件?任意外网映射到内网固定地 ...

他就是这个意思
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| | 显示全部楼层
aming.ou 发表于 2023-9-8 18:49
一般路由器都有的功能,怎么这么复杂呢?难道没有虚拟服务器(端口映射)的插件?任意外网映射到内网固定地 ...

我看懂你说的意思了,但你没有看懂我说的意思。我说的不是一般的硬路由,而是Routeros软路由系统。里面什么都没有设置的,100%手搓的。端口映射是不难,难的是在Routeros系统里设置WAN口分配到的新公网IP要自动填入映射规则里。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| | 显示全部楼层

为什么一定要这样。说白了吧。内网里挂了点心,还不是为什么赚点电费。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

50439488 发表于 2023-9-9 11:16
我看懂你说的意思了,但你没有看懂我说的意思。我说的不是一般的硬路由,而是Routeros软路由系统。里面什 ...

不难,写一个脚本挂在pppoe的profile里即可。

你一楼那个脚本也是可笑,RouterOS的脚本第一行不用写什么bash,这又不是普通Linux
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| | 显示全部楼层
mantouboji 发表于 2023-9-9 14:56
不难,写一个脚本挂在pppoe的profile里即可。

你一楼那个脚本也是可笑,RouterOS的脚本第一行不用写什么 ...

感谢提示!搞好了,就是新建一个profile。然后再给一个命令一分钟检测一次,出现公网IP改变时自动更改端口映射规则里的IP。现在重启路由就可以自动更改了。哈哈真的可以,ROS玩懂了真的是好东西。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| | 显示全部楼层
思路就是这样的
添加 动作=目标网络地址转换 链=目标网络地址转换 注释=nat-235 目标地址=得到的公网IP \
    目标端口=xx 协议=TCP 目标地址=本地要映射的端口 目标端口=21
添加 动作=源地址伪装 链=源地址伪装 源地址=IP段/24


添加 动作=添加源地址到地址列表 地址列表=online 地址列表超时=非静态 链=prerouting 注释="ip calc" 目标地址类型="" \
    目标限制=1,5,目标地址/1m40s 限制=1,5:数据包 源地址=IP段/24 源地址类型="" 时间=0s-1d,sun,mon,tue,wed,thu,fri,sat

添加 不需要权限验证=否 名称=set-nat-global-ip 所有者=mmc 权限=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon 源代码=":\
    全局 ipaddr [/ip address get [/ip address find interface=pppoe-out1] add\
    ress]\r\
    \n\r\
    \n:set ipaddr [:pick \$ipaddr 0 ([len \$ipaddr] -3)]\r\
    \n:全局 oldip [/ip firewall nat get [/ip firewall nat find comment=\"nat\
    -235\"] dst-address]\r\
    \n\r\
    \n:如果 (\$ipaddr != \$oldip) do={\r\
    \n  记录信息 消息=[/ip firewall nat set [/ip firewall nat find comment=\
    \"nat-235\"] dst-address=\$ipaddr]\r\
    \n}"
添加 不需要权限验证=否 名称=ip-number 所有者=mmc 权限=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon 源代码=":\
    本地 ipNumber 0\r\
    \n:本地 onlineIPList online\r\
    \n\r\
    \n:foreach i in=[/ip firewall address-list find list=\$onlineIPList] do={\
    \r\
    \n  :set ipNumber (\$ipNumber+1)\r\
    \n  }\r\
    \n记录警告 消息=(\"当前 \" . \$ipNumber . \" 个在线IP地址\")"

再加一个定时器
添加 间隔=1m 名称=global-ip-sync 事件=":执行 \"set-nat-global-ip\"" \
    权限=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
    起始日期=oct/16/2018 起始时间=11:07:59
添加 间隔=1m 名称=ip-statistic 事件=":执行 \"ip-number\"" 权限=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
    起始时间=startup

搞定
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

冰雪聪明。不过不要搞这么蹩脚的汉语翻译,把代码放在论坛的“代码”块里
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

50439488 发表于 2023-9-8 17:27
我没有固定的公网IP,路由每次重启后都得到一个不一样公网IP。你这个脚本会自动的更改映射NAT规则里的外 ...

他就是告诉你“in-interface=你的外网接口”,这个设置就解决动态ip的问题,无需再填变动的WAN的IP地址,定义in-interface就行了。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

本帖最后由 Jnwei1202 于 2023-9-22 23:48 编辑

那有那么复杂要什么每分钟运行一次判断IP是否改变,只要拔号就改IP
1、建NAT规则,dstnat 的 dst. address,和 src-nat的 To Addresses 可以先随意填,主要是把 规则的 Comment 写上对就PPPOE的拔号名或自己指定。
比如 src-nat就用"SRCNAT-PPPOE-OUT1",dstnat就用"DSTNAT-PPPOE-OUT1"

2、在PPP里建一个PPPOE用的Profiles,在Profiles的Scripts 里写脚本。只要拔号就运行脚本修改,相关的NAT规则的IP。
#启用PPPoE线路SRCNAT
/ip firewall nat set [ find comment="SRCNAT-PPPOE-OUT1" ] to-addresses=$"local-address"
#启用PPPoE线路DSTNAT
/ip firewall nat set [ find comment="DSTNAT-PPPOE-OUT1" ] dst-address=$"local-address"


我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

欢迎大家光临恩山无线论坛上一条 /1 下一条

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-5-6 00:37

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

快速回复 返回顶部 返回列表