電訊茶室's Archiver

角色 發表於 2018-10-17 11:57

MikroTik router 怎样每天更换IP address?

[i=s] 本帖最後由 角色 於 2018-10-19 11:26 編輯 [/i]

=== gfx 提供 的方法更加精彩 [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&ptid=7243&pid=45568]Link[/url] ===
=== 在安装第一楼的script前,请看第二楼信息,和先安装第三楼的script,买个保险 ===

因为科学上网,导致梯子经常给发现(通过不同的大陆IP address接到国外的服务器),如果阿爷系统侦测到,先干扰后block IP,所以每天必须更换IP address才能尽量避开被block ip的命运。

之前我用过怎样更改mac address,但是比较复杂,还有没有必要,现在下面有个简单的方法。

下面的script,就是把ether1关(disabled=yes),然后等8分钟,再把ether1 开(disabled=no)。IP就会自动更换,一般系统5分钟就可以把之前分配的IP给忘了,现在我用8分钟,是比要求长一定。我是用PCCW,估计系统ISP,如HKBN,或者HGC也可以。

用winbox打开MikroTik router,然后在system->script,加上一个script叫ether1-down-up,如下图:

[attach]4201[/attach].

上图的code:[code]
/interface ethernet set ether1 disabled=yes
:log warning "Interface 1 is down"

delay "8m";

/interface ethernet set ether1 disabled=no
:log warning "Interface 1 is up"
[/code].

在sytem->scheduler,加入一个每天凌晨4点钟进行更换IP。
[attach]4200[/attach]。

Start Time: 04:00:00
Interval: 1d 00:00:00

1d = 1 day (每天)

如果你想进行快速测试(每15分钟),Interval可以修改为
Interval = 00:15:00

在大陆的DNS server,需要大约等10分钟,新的IP Address才能更新到大陆来,如果在ether1 up后10分钟,在大陆才能access到香港改了IP adress的Mikrotik router。

角色 發表於 2018-10-17 12:17

在使用上会遇到很多不知道的结果,例如我喜欢用command line打命令,还有script的“预计结果”有误,导致到WAN口ether1被关上,要等晚上家里人回来接上电脑才能把router ether1唤醒(打开ether1)。有的时候因为所有interface etherX都被关上,有电脑和LAN cable都没有用,要用serial cable才能唤醒,而香港也很好买到那种cable,需要在大陆淘宝,然后寄回香港,再把router用serial cable才能打开,所以大家一定要小心行事。

经历过N次,ether1被关上,本人想出另外一个方法,就是自动把WAN口ether1欢呼到打开。

角色 發表於 2018-10-17 12:18

[i=s] 本帖最後由 角色 於 2018-10-17 12:56 編輯 [/i]

为了防止在上面的ether1被错误理解指令被关上,大家可以用下面的script。


[attach]4203[/attach].[code]
/interface ethernet set ether1 disabled=no
:log warning "Interface 1 is forced to up"
/code].


[attach]4202[/attach].

[code]
/system script run ether1-up
[/code].

角色 發表於 2018-10-17 13:58

上面的script,有需要就开着或者关着,但是做实验必须开着ether1-wake-up

就在“+” enable,“-” disable

[attach]4205[/attach]

gfx86674 發表於 2018-10-17 21:19

我覺得script放在dhcp-client advanced欄應該會更好.
ether1不但不會閒置,還可以強制更新與上次不同的地址,並每天固定的時間定時重撥.

假設制定每日5點進行重撥:
[attach]4212[/attach][code]:local arm 5:0:0

:local time [/system clock get time]
:global past ; :local public
:if ($bound=1) do={
:set public [/ip dhcp-c get [find interface=ether1] address]
:set public [:pick $public 0 [:find $public "/"]]

:if ($public!=$past) do={
   :set past $public
   :if ($time>=$arm) do={:delay (1d-$time+$arm)} else={:delay ($arm-$time)}
}
/ip dhcp-client release [find interface=ether1] ; :delay 3s
}
[/code]

角色 發表於 2018-10-18 11:25

谢谢CHing,我把你上面的script exactly copied我的MikroTik Router,明天就知道结果。

角色 發表於 2018-10-19 07:54

经过测试确实可以用,谢谢CHing。

从[1]里的信息,都有类似师兄的script来源:[code]
/ip dhcp-client
add add-default-route=no dhcp-options=hostname,clientid disabled=no interface=ether2 script="{\r\
    \n    :local rmark \"WAN1\"\r\
    \n    :local count [/ip route print count-only where comment=\"WAN1\"]\r\
    \n    :if (\$bound=1) do={\r\
    \n        :if (\$count = 0) do={\r\
    \n            /ip route add gateway=\$\"gateway-address\" comment=\"WAN1\" routing-mark=\$rmark\r\
    \n        } else={\r\
    \n            :if (\$count = 1) do={\r\
    \n                :local test [/ip route find where comment=\"WAN1\"]\r\
    \n                :if ([/ip route get \$test gateway] != \$\"gateway-address\") do={\r\
    \n                    /ip route set \$test gateway=\$\"gateway-address\"\r\
    \n                }\r\
    \n            } else={\r\
    \n                :error \"Multiple routes found\"\r\
    \n            }\r\
    \n        }\r\
    \n    } else={\r\
    \n        /ip route remove [find comment=\"WAN1\"]\r\
    \n    }\r\
    \n}\r\
    \n"
[/code].

Ref:
[1] [url]https://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Client[/url]

角色 發表於 2018-10-19 11:42

[b]回復 [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&pid=45568&ptid=7243]5#[/url] [i]gfx86674[/i] [/b]

CHing, 从图上开的default route distance = 5,而我是 = 1,是否 = 5有别的用途呢?

gfx86674 發表於 2018-10-19 11:57

[quote]经过测试确实可以用,谢谢CHing。

从[1]里的信息,都有类似师兄的script来源:.

Ref:
[1]  ...
[size=2][color=#999999]角色 發表於 2018-10-19 07:54[/color] [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&pid=45573&ptid=7243][img]http://www.telecom-cafe.com/forum/images/common/back.gif[/img][/url][/size][/quote]
其實兩script邏輯差異是很大的...
小弟準備的script整個架構主在目前連線後,隨即準備下次取得新ip的動作.
但在取得下次新ip的時間,用delay這個參數做間隔.

若與上次的ip不同,就delay方式故意延遲到指定的凌晨時段再完成換ip的作業;
若與上次ip同 ,就沒有故意delay延遲的問題 ,直接讓script完成換新ip的工作.

gfx86674 發表於 2018-10-19 13:01

[quote]回復  gfx86674

CHing, 从图上开的default route distance = 5,而我是 = 1,是否 = 5有别的用途呢? ...
[size=2][color=#999999]角色 發表於 2018-10-19 11:42[/color] [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&pid=45574&ptid=7243][img]http://www.telecom-cafe.com/forum/images/common/back.gif[/img][/url][/size][/quote]
若您有勾選add default route ,distance才可以輸入數字.

若您沒有勾選,您就需在/ip route新增dst-address=0.0.0.0/0 gateway=ether1
若有勾選,就無需手動新增,讓系統在/ip route自動生成.
dst-address=0.0.0.0/0代表連接任何一個ip ,都要先往ether1(gateway)查詢.

而distance代表順位,數字越大代表排續越後面.

所以今日我有兩個wan ,我不希望1.1.1.1往ether1查詢 ,我只要在/ip route設:
dst-address=1.1.1.1/32 gateway=ether2 distance=2
distance 2>5 ,所以只要在路由表控制不需做policy route ,即可讓1.1.1.1往ether2 ;

但如果dst-address=0.0.0.0/0 gateway=ether1 distance=1
distance 1>2 ,即使路由表標示1.1.1.1往ether2 ,最終也是會往ether1去.

所以通常0.0.0.0/0的distance都盡量不要設太優先,也就是不要設0或1 ,
這樣我們才有操控地址gateway的空間...
像windows的路由表,dst-address=0.0.0.0/0 metric(與distance同義)都常不會太小,都是100起跳.
而route我只用到5,這已經算很保守的操作了.

頁: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.