找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 8224|回复: 4

[脚本] 好久没来了,送个礼:ROS动态域名+UPNP脚本

[复制链接]
发表于 2008-1-6 11:10:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×
  1. :if ([/system scheduler get DDNS run-count ]=1) do={
  2. :log info "DDNS: Creating Dynamic DNS update system."
  3. :beep frequency=500
  4. :global username "yourusername"
  5. :global password "yourpassword"
  6. :global domainname "yourdomainname"
  7. :global domainzone "yourdomainzone"
  8. }

  9. :if ([/interface find type=pppoe-out running=yes]="") do={
  10. :beep frequency=500 length=2s
  11. :delay 4
  12. :beep frequency=500 length=2s
  13. :log warning "DDNS: No PPPoE is running."
  14. } else={
  15. :set intLocal [ /interface find type=pppoe-out running=yes ]
  16. :set ipResolve ([:resolve ($domainname . "." . $domainzone)] . "/32")
  17. :set ipLocal [ /ip address get [ /ip address find interface=$intLocal ] address ]
  18. :if ($ipLocal != $ipResolve) do={
  19. :log info ("DDNS: Resolved IP address : " . $ipResolve )
  20. :log info ("DDNS: Local IP address : " . $ipLocal )
  21. :log info ("DDNS: Sending Dynamic DNS update to server")
  22. /tool dns-update zone=$domainzone name=$domainname key=$password key-name=$username address=[:pick $ipLocal 0 ([:len $ipLocal] - 3)]

  23. /ip upnp set enabled=no
  24. :delay 2
  25. /ip upnp interfaces remove [/ip upnp interfaces find type=external ]
  26. /ip upnp interfaces remove [/ip upnp interfaces find type=internal ]
  27. :delay 2
  28. /ip upnp interfaces add interface=[/interface get [/interface pppoe-client find running=yes ] name] type=external
  29. /ip upnp interfaces add interface=LAN type=internal
  30. :delay 2
  31. /ip upnp set enabled=yes
  32. /ip upnp set allow-disable-external-interface=no
  33. /ip upnp set show-dummy-rule=no
  34. :delay 2
  35. /ip fi man set [/ip fi man find comment="flowback"] dst-address=$ipLocal
  36. :beep frequency=500
  37. }
  38. }
复制代码
简单说明一下作用:
由于我使用的是包小时的宽带,但又想包月上网,所以就使用别人的宽带帐号了。这样我就不得不在ros里添加大量pppoe-client,说不定哪个就能播上去,也说不定哪个就会掉线了。掉线之后WAN IP也随着变化,接着UPNP就会失效,所以我编写了这个脚本。这个脚本能够发现外网IP变化之后,进行域名更新,并重新开启UPNP,pppoe失效蜂鸣提示;使用大概一年了,没什么问题,发上来与各位探讨一下。

注意,我是直接在scheduler里添加的,并没有在scheduler 添加个计划任务,然后计划任务的内容是运行xxx脚本,我觉得这样很是多次一举,为什么不直接新建个计划任务然后内容就是要执行的脚本呢?还有“:if ([/system scheduler get DDNS run-count ]=1) do={”这句话表明了,计划任务一定要命名为"DDNS"才能顺利执行。


简单说明下原理:
  1. :if ([/system scheduler get DDNS run-count ]=1) do={ //如果是第一次运行,则执行下面初始化,蜂鸣并记录日志
  2. :log info "DDNS: Creating Dynamic DNS update system."
  3. :beep frequency=500
  4. :global username "yourusername"
  5. :global password "yourpassword"
  6. :global domainname "yourdomainname"
  7. :global domainzone "yourdomainzone"
  8. }

  9. :if ([/interface find type=pppoe-out running=yes]="") do={ //如果没有拨号成功的pppoe-client,蜂鸣并记录日志
  10. :beep frequency=500 length=2s
  11. :delay 4
  12. :beep frequency=500 length=2s
  13. :log warning "DDNS: No PPPoE is running."
  14. } else={ //否则(就是有拨号成功的pppoe-client)执行下面代码
  15. :set intLocal [ /interface find type=pppoe-out running=yes ] //返回成功拨号的pppoe-client接口索引
  16. :set ipResolve ([:resolve ($domainname . "." . $domainzone)] . "/32") //解析域名对应IP
  17. :set ipLocal [ /ip address get [ /ip address find interface=$intLocal ] address ] //获得拨号成功的pppoe-client接口IP
  18. :if ($ipLocal != $ipResolve) do={ //如果获得的IP和解析的IP不一致
  19. :log info ("DDNS: Resolved IP address : " . $ipResolve )
  20. :log info ("DDNS: Local IP address : " . $ipLocal )
  21. :log info ("DDNS: Sending Dynamic DNS update to server") //记录日志
  22. /tool dns-update zone=$domainzone name=$domainname key=$password key-name=$username address=[:pick $ipLocal 0 ([:len $ipLocal] - 3)] //进行域名更新

  23. /*
  24. [:pick $ipLocal 0 ([:len $ipLocal] - 3)]
  25. 这句的作用是去掉$ipLocal变量中后面的3个字符,在程序里就是去掉例如"111.222.33.44/32"后面的"/32"
  26. 这个位置当时留下个小问题,如果获得的子网掩码是"/8"之类的(2位字符宽度),那么可能会连IP最后一位一同去掉,但在我这里不会发生这个问题,所以也没考虑。
  27. 用:find是不是可以解决这个问题我没有试验。
  28. */

  29. /ip upnp set enabled=no //关闭UPNP
  30. :delay 2
  31. /ip upnp interfaces remove [/ip upnp interfaces find type=external ]
  32. /ip upnp interfaces remove [/ip upnp interfaces find type=internal ] //删除UPNP内、外接口
  33. :delay 2
  34. /ip upnp interfaces add interface=[/interface get [/interface pppoe-client find running=yes ] name] type=external //添加拨号成功的接口为UPNP外接口
  35. /ip upnp interfaces add interface=LAN type=internal //添加LAN接口为UPNP内接口
  36. :delay 2
  37. /ip upnp set enabled=yes //重新启用UPNP
  38. /ip upnp set allow-disable-external-interface=no //设置“允许关闭外部接口”为no
  39. /ip upnp set show-dummy-rule=no //啊!这个?!不明白,望高人赐教。。。。。。
  40. :delay 2
  41. /ip fi man set [/ip fi man find comment="flowback"] dst-address=$ipLocal //这个,更改回流的外网IP,回流我使用manage做的,我想这样比较好,这个下次再说
  42. :beep frequency=500
  43. }
  44. }
复制代码
routeros
 楼主| 发表于 2008-1-6 11:11:13 | 显示全部楼层
忘了说了,我的ROS是2927,置顶那个。
routeros
回复

使用道具 举报

发表于 2008-1-6 12:43:05 | 显示全部楼层
怎么没有用。我前面的没有用。只有后面的UPNP没有反应
routeros
回复

使用道具 举报

发表于 2008-4-2 12:44:43 | 显示全部楼层
弄个3.0能用的动态域名啊。。。
routeros
回复

使用道具 举报

发表于 2013-7-30 23:42:36 | 显示全部楼层
看看怎么样.
routeros
回复

使用道具 举报

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

本版积分规则

QQ|Archiver|手机版|小黑屋|软路由 ( 渝ICP备15001194号-1|渝公网安备 50011602500124号 )

GMT+8, 2024-5-15 10:50 , Processed in 0.107433 second(s), 4 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2001-2023 Discuz! Team.

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