回車恐懼症?13個 JUNOS 技巧助你輕鬆無憂配置網絡

回車鍵,日常電腦使用中,扮演“確認”或“執行”事件的功效。


在網絡工程師的平日運維工作中,無論是計劃性的網絡配置修改,還是臨時性的故障處理,大家是否都存在同樣的感受:特別小心敲擊回車鍵,再三確認以後才執行


相信以下場景是很多做網絡的朋友曾經都遇到過的,在遠程配置 Cisco 或者其他廠商路由器或交換機時,一頓複製粘貼然後敲下回車結果就是這樣:

1. 配錯接口 IP 地址——Game Over!

2. 修改靜態路由條目或者動態路由協議,導致路由丟失----Game Over!

3. 配置錯 VLAN ID 或者二層接口 ACCESS/TRUNK 配置錯誤----Game Over!


這個時候大部分人都是一臉驚慌,腦子琢磨怎麼解決:給遠端人員打電話把路由器交換機重啓。反正配置沒保存呢,重啓就回來了。


但是如果遠端就是客戶的話,後面幾天日子恐怕就不太好過了。要是這是公司另外一個廠房的核心交換,這事兒就大了。重啓是不可能了,再去現場花費的時間成本無可估量。


總的來說,在做日常網絡配置中,回車恐懼症來源於如下幾個方面:

● 對自己準備的配置沒信心,擔心配置中有低級錯誤

● 害怕配置設備過程中遇到意料之外的問題

● 和對端聯調的時候,不信任對端,害怕遠端工程師犯錯導致聯調不成功


以上的問題也曾讓我在日常運維中頭疼不已。直到後來我入職某運營商後,無論從客戶的 CPE,到核心 PE,BNG 或者 P 路由器,絕大部分設備都是 Juniper。在慢慢從 Cisco 習慣過渡到 Juniper 的過程中,我發現這些擔憂和問題極大地減少了。而且準確來說你有很大的機會在敲回車執行之間,就已經發現錯誤並予以糾正。


那到底在 Juniper 環境下,有什麼行之有效而且簡單易用的工具能夠解決工程師的煩惱呢?


爲了回答上面的問題,我將用一個日常割接流程來給大家揭曉答案。


說到割接流程,無非就三點: 

● 割接前期配置準備

● 進行割接

● 割接後複查


在介紹這些技巧之前,先給較少接觸過 Juniper 設備的朋友們做個知識鋪墊:和 Cisco 的 IOS 系統配置模式不同,JUNOS 系統配置模式裏面,配置修改並不是立即生效。你可以隨意增添甚至刪除整個設備配置,只有你在通過 commit 提交配置後,纔會生效。


(PS:請勿在現網設備隨意模仿,可以在Juniper實驗室或者模擬器上測試。)



割接前期配置準備


在前期配置準備過程中,我們怎麼保證預配置沒有低級錯誤,如打錯字符,寫錯 IP 等?難道要每次都把預配置加載到實驗室設備或者模擬器上跑一遍看看?


另外,由於配置腳本行數多,配置量大。在割接期間逐個複製粘貼很耗費時間;同時,在複製粘貼過程中,如果人眼跟蹤不及時,容易忽略設備提示的錯誤。


那麼,下面讓我們用一個簡單的例子來說明 JUNOS 都有哪些工具能夠解決以上問題。

 

案例:

某部門計劃隔天割接幾臺關鍵服務器到另外一臺 Juniper 設備上,工程師計劃配置一個新的 vlan 及 vlan interface。並劃分兩個物理接口到這個新 vlan 內。以下爲該工程師做的預配置: 

set interfaces ge-0/0/6 unit 0 family ethernet-switching interface-mode access
set interfaces ge-0/0/6 unit 0 family ethernet-switching vlan members internal
set interfaces ge-0/0/7 unit 0 family ethernet-switching interface-mode access
set interfaces ge-0/0/7 unit 0 family ethernet-switching vlan members internal
 
set interfaces irb unit 5 family inet address 192.168.1.1/24
 
set vlans internal vlan-id 5
set vlans internal l3-interface irb.5


問題 1:一般情況,工程師會等到割接時才把所有準備的配置加載到設備上,那我們可否在準備配置前期提前加載到設備上?等到割接的時候,只需要執行幾條關鍵的命令,就完成整個割接?這樣既節省時間,也減少了出錯率。

 

答案是可以的,上工具!


工具 1: load set terminal (加載set命令的配置,如果有語法錯誤會立即提醒)

load set terminal 是一個批量添加配置的工具,相比逐個命令複製粘貼,批量添加的好處是配置清晰明瞭。可以一目瞭然地發現錯誤的參數,尤其是在配置行數特別多的情況下,非常方便工程師查錯。


它還有其他孿生兄弟,例如:load merge terminal,load patch terminal等。具體大家可以查閱Juniper文檔。


讓我們使用 load set terminal 把工程師準備的割接配置加載到設備上,方法如下: 

admin@Juniper>configure 
Entering configuration mode
 
admin@Juniper# load set terminal 
[Type ^D at a new line to end input]
set interfaces ge-0/0/6 unit 0 family ethernet-switching interface-mode access
set interfaces ge-0/0/6 unit 0 family ethernet-switching vlan members internal
set interfaces ge-0/0/7 unit 0 family ethernet-switching interface-mode access
set interfaces ge-0/0/7 unit 0 family ethernet-switching vlan members internal
 
set interfaces irbunit 5 family inet address 192.168.1.1/24
 
set vlans internal vlan-id 5
set vlans internal l3-interface irb.5
load complete
 
[edit]
admin@Juniper#

當所有命令加載完畢以後,鍵入 Ctrl+D 後就可以結束 load setterminal 的界面並回到了配置模式下。


工具 2: deactivate(告知 JUNOS 忽略某配置)

通過 deactivate 這一配置技巧,我們可以人爲地忽略某配置。從而讓 JUNOS 在讀取配置的時候,不讀取對應的配置參數。但是在通過 show configuration 的時候,仍然可見。


回到案例中,爲了避免割接前配置的 vlan 接口 irb.5 的 IP 地址被其他路由協議發佈到網絡中,我們先把它 deactivate 掉。方法如下: 

[edit]
admin@Juniper# deactivate interfaces irb unit 5 family inet address 192.168.1.1/24


工具 3: disable (admin shut down某接口)

disable 就等同於 Cisco 的接口 shutdown。與 Cisco 相似,它也只能應用於接口級別。


爲了讓接口在割接前不開啓,我們可以 shutdown ge-0/0/6 以及 ge-0/0/7 接口。方法如下:

admin@Juniper# set interfacesge-0/0/6 disable 
 
[edit]
admin@Juniper# setinterfaces ge-0/0/7 disable


最後,讓我們來看看通過以上工具配置後的最終結果:

admin@Juniper# show interfaces | display set 
setinterfaces ge-0/0/6 disable
set interfacesge-0/0/6 unit 0 family ethernet-switching interface-mode access
set interfacesge-0/0/6 unit 0 family ethernet-switching vlan members internal
setinterfaces ge-0/0/7 disable
set interfacesge-0/0/7 unit 0 family ethernet-switching interface-mode access
set interfacesge-0/0/7 unit 0 family ethernet-switching vlan members internal
set interfaces irb unit 5 family inet address 192.168.1.1/24
deactivate interfaces irb unit 5 family inet address 192.168.1.1/24
 
admin@Juniper# show vlans | display set 
set vlans internal vlan-id 5
set vlans internal l3-interface irb.5

這裏你可能會注意到在每個配置裏面,都有一個對應的 deactivate 或者 disable 配置。


Tips:deactivate 和 disable 有什麼區別?

disable 等同於 Cisco 的 shutdown,但是 JUNOS 系統會繼續讀取 ge-0/0/6 或者 ge-0/0/7 的接口配置,只是把它admin down了而已。

而 deactivate 直接讓 JUNOS 忽略此配置,雖然你通過 show config 仍然能看見 irb unit 5 的 IP 地址,但是對於設備本身來說,它會完全忽略此IP。


問題 2:預配置完成以後,如何通過JUNOS覈查配置錯誤,如何知道當前的配置與現有配置是否衝突呢?


工具 4: show | compare (對比原配置和加載的新配置之間的區別)

show | compare能夠對比配置前後的差別,從而看出配置是否有誤。


在本案例中,使用 show | compare 可以對比配置加載前後的區別,方法如下:

admin@Juniper# show |compare 
[edit interfaces]
+   ge-0/0/6 {
+       disable;
+       unit 0 {
+           family ethernet-switching {
+               interface-mode access;
+               vlan {
+                   members internal;
+               }
+           }
+       }
+   }
+   ge-0/0/7 {
+       disable;
+       unit 0 {
+           family ethernet-switching {
+               interface-mode access;
+               vlan {
+                   members internal;
+               }
+           }
+       }
+   }
[edit interfaces irb]
+    unit 5 {
+        family inet {
+            inactive: address 192.168.1.1/24;
+        }
+    }
[edit vlans]
+   internal {
+       vlan-id 5;
+       l3-interface irb.5;
+   }
 
[edit]
admin@Juniper#

你可以看到,左邊爲加號(+),證明此配置是新增配置。如果是減號(-),那麼代表刪除配置。


爲了加強對這個工具的理解,下面再加一個示例:


### 額外示例 ###

某個工程師準備把接口 ge-0/0/5 從 access 模式變爲 trunk 模式,並新增一個 Internet 的 vlan 在此接口下。完成配置並 show | compare 後,通過加減號就可以知道哪個配置刪除了,哪個配置是新增的。示例如下:

admin@Juniper# show |compare 
[edit interfaces ge-0/0/5 unit 0 family ethernet-switching]
-      interface-mode access;
+      interface-mode trunk;
[edit interfaces ge-0/0/5 unit 0 family ethernet-switching vlan]
-       members MGMT;
+       members [ MGMT Internet ];

注意,show | compare 在日常 Juniper 運維中,佔據了非常重要的角色,通過 show| compare 你可以清楚知道自己的配置是否符合預期。


工具5: commit check (設備配置自動覈查)

通過 commit check,能夠在提交配置之前,做最後一步的系統配置自檢。


回到我們開頭的案例。完成 show | compare 的檢查以後,我們繼續讓 JUNOS 自行檢查配置: 

[edit]
admin@Juniper# commit check 

configuration check succeeds
 
[edit]
admin@Juniper#

commit check 會告知你配置是否有誤,是否與其他配置有衝突等。上面輸出可以看出我們的配置沒有問題。接下來就可以放心去提交配置了。

 

問題 3:在配置中如果需要修正錯誤配置或需要新增其他配置時,如何簡潔高效地完成任務?


有些時候,大家載入配置過程中,發現腳本里面接口配置錯誤,或者IP地址配置錯誤。按照一般思路來的話,肯定是刪除掉此命令,然後再重配置一遍。其實還有比這個更簡單的方法,我們來看看對比:


上面的案例裏,我們配置了 ge-0/0/6 和 ge-0/0/7 兩個接口。假設服務器部門告訴你,正確接口應該是 ge-0/0/5,你需要把 ge-0/0/7 的配置挪到 ge-0/0/5 上。


傳統方法:

delete interfaces ge-0/0/7 
 
set interfaces ge-0/0/5 disable
set interfaces ge-0/0/5 unit 0 family ethernet-switching interface-mode access
set interfaces ge-0/0/5 unit 0 family ethernet-switching vlan members internal


而更簡單的方法可以用到以下工具:

 

工具 6: rename (重命名某一個變量)

顧名思義,rename 可以把你在 JUNOS 內配置的任何參數變量重命名。在這裏我們把接口 ge-0/0/7 重命名爲 ge-0/0/5,所有 ge-0/0/7 相關的配置被挪到 ge-0/0/5 層級之下。

admin@Juniper# rename interfaces ge-0/0/7 to ge-0/0/5 
 
[edit]
admin@Juniper# show |compare 
[edit interfaces]
+   ge-0/0/5 {
+       unit 0 {
+           family ethernet-switching {
+               interface-mode access;
+               vlan {
+                   members internal;
+               }
+           }
+       }
+   }
-   ge-0/0/7 {
-       unit 0 {
-           family ethernet-switching {
-               interface-mode access;
-               vlan {
-                   members internal;
-               }
-           }
-       }
-   }
 
[edit]
admin@Juniper#

 

工具 7: replace pattern(替換字符串)

相比 renamereplace pattern 的功能更爲強大,他可以替換整個 JUNOS 配置裏面的參數。假設 ge-0/0/7 被某一個 routing-instance VRF)調用,在執行 replace pattern ge-0/0/7 with ge-0/0/5 以後,接口配置和 routing-instance 配置都會同時改變。這在大批量修改某一個參數的時候尤其方便。


在此案例中,ge-0/0/7 僅有接口配置,並未被其他協議等調用。所以 show | compare 僅僅替換接口相關參數。

admin@Juniper# replace pattern ge-0/0/7 with ge-0/0/5


替換完成後,使用前面講到的 show | compare 查看結果,方法如下:

admin@Juniper# show |compare 
[edit interfaces]
+   ge-0/0/5 {
+       unit 0 {
+           family ethernet-switching {
+               interface-mode access;
+               vlan {
+                   members internal;
+               }
+           }
+       }
+   }
-   ge-0/0/7 {
-       unit 0 {
-           family ethernet-switching {
-               interface-mode access;
-               vlan {
-                   members internal;
-               }
-           }
-       }
-   }
 
[edit]


可以看出,運用此工具後,所有的 ge-0/07 都被替換成了 ge-0/0/5。

 

工具 8: copy (複製某一個變量到其他變量)

如果這個時候,服務器部門又發郵件告知你,索性把ge-0/0/5和ge-0/0/7都配置了吧,很簡單,我們把ge-0/0/7的配置複製到ge-0/0/5就行了。


下面我們以ge-0/0/7爲模板複製配置到ge-0/0/5。

admin@Juniper# copy interfaces ge-0/0/7 to ge-0/0/5 
 
[edit]
admin@Juniper# show |compare 
[edit interfaces]
+   ge-0/0/5 {
+       unit 0 {
+           family ethernet-switching {
+               interface-mode access;
+               vlan {
+                   members internal;
+               }
+           }
+       }
+   }
 
[edit]
admin@Juniper#

完成以上的預配置之後我們需要讓其生效。正如我之前提到過,Juniper 的配置是需要 commit 提交以後才能生效的,但是爲了避免提交配置以後,出現意外情況導致工程師丟失與路由器的管理連接,JUNOS 引入了自動倒回功能。


工具9: commit confirm xxx comment “xxx”  (提交配置,若沒有工程師確認,系統會在xx分鐘之內倒回,並附上配置備註)


這是 JUNOS 最讓人愛不釋手的一個功能。先看例子:使用 commit confirm comment 來提交配置

admin@Juniper# commit confirmed 2 comment "add new interfaces as well as vlan configuration" 
 
commit confirmed will be automatically rolled back in 2 minutes unless confirmed
commit complete
 
# commit confirmed will be rolled back in 2 minutes
[edit]
admin@Juniper#

如上所示,confirm 2 是告知路由器,兩分鐘之內如果工程師沒有確認此配置,那麼之前配置的內容將被刪除並還原回配置之前的狀態。如果在 confirm 之後不加數字,默認是 10 分鐘。最低可以設置一分鐘。


更貼心的是,你還可以使用 comment 關鍵字添加備註,這樣大大方便了後續覈查的時候瞭解每次操作的大概內容。以上面的配置爲例,通過備註可以知道此次配置內容爲新增兩個接口和一個 vlan。


下面再來演示一下 Juniper 設備在 2 分鐘後無人確認配置的情況下,自動倒回的輸出:

[edit]
                                                                              
Broadcast Message from root@Juniper                                            
        (no tty) at 17:35 NZST...                                              
                                                                              
Commit was not confirmed; automatic rollback complete.                                                                              
 
[edit]
admin@Juniper#


那麼在 commit 之後,一切工作正常,我如何避免配置被自動倒回呢?


我們只需要在倒回時間超時之前再做一個核查配置的 commit check 就行了。如下所示:

# commit confirmed will be rolled back in 2 minute
[edit]

admin@Juniper# commit check

configuration check succeeds
 
[edit]
admin@Juniper#


是不是很貼心?



割接中


因爲有前期的預配置,割接就顯得很輕鬆了。我們只需要刪除 disable 以及 deactivate 的命令行就完成了。這裏會用到如下工具:

 

工具 10: activate(激活忽略的預配置)

通過刪除之前的disable 配置,並activate之前的deactivate的配置,就可以激活之前加載的所有預配置。

admin@Juniper# load set terminal 
[Type ^D at a new line to end input]
 
delete interfaces ge-0/0/6 disable
delete interfaces ge-0/0/7 disable
activate interfaces irb unit 5 family inet address192.168.1.1/24
 
load complete
[edit]


用 show | compare 確認激活行爲:

admin@Juniper# show |compare 
[edit interfacesge-0/0/6]
-   disable;
[edit interfacesge-0/0/7]
-   disable;
[edit interfaces irbunit 5 family inet]
!        active: address 192.168.1.1/24 { ... }


工具 11: commit at(定點提交配置)

激活配置以後,同樣我們也需要向 JUNOS 提交配置,相比之前的 commit confirm,這次我們換一個方法,使用 commitat。如下所示,它會告知路由器,請在晚上 10 點整的時候提交這個配置。(你可以根據自己的時間修改,但是需要比當前時間晚。)

admin@Juniper# commitcomment "cut over the vlan and ge-0/0/5,ge-0/0/6" at 22:00    

configuration checksucceeds

commit at will beexecuted at 2017-09-14 22:00:00 NZST

The configuration hasbeen changed but not committed

Exiting configurationmode

如上所示,系統提示配置會在晚上10點被執行。


需要說明的是,爲了給大家展現各個工具的使用方法,特地挑選了一個及其簡單的案例。但是帶來的副作用就是沒有完全體現出預先加載割接配置的優勢。但是試想一下如果此次割接需要大量的配置,例如大量的 VRF 配置,大量的 BGP 鄰居配置以及接口配置等。通過在割接前加載所有配置並 deactivate 掉,割接時只需要少量命令行來 activate 激活預配置,從而大大簡化了割接的步驟和複雜度,進而減小了犯錯誤的可能性。



割接完成


割接完成以後,最重要的就是覈查網絡運行情況,確認提交的配置是否按照預期運行,並且沒有引入任何其他網絡故障。爲了達到這樣的目的,我們可以採用以下介紹的工具:


工具11: monitor interface(設備接口流量監控)

完成接口配置以後,我們需要覈查接口是否有流量。如果用傳統的 show interface xxx 看到的都是靜態數據,那有沒有更好的方法?例如動態地觀測數據。


藉助 monitor interface,我們就可以輕鬆實現這個功能,如下實例:

admin@Juniper>monitor interface ge-0/0/5
 
Interface: ge-0/0/5,Enabled, Link is Up
Encapsulation:Ethernet, Speed: 1000mbps
Trafficstatistics:                                             Current delta
<以下爲接口實時動態輸入輸出流量,每秒都在刷新,我只截取了某一秒的輸出內容>
  Input bytes:                  17411220 (200 bps)                        [52]
  Output bytes:                 24971481 (947704 bps)                 [438150]
  Input packets:                  324059 (0 pps)                           [1]
  Output packets:                 319355 (789 pps)                      [2921]
Errorstatistics:
  Input errors:                        0                                   [0]
  Input drops:                         0                                   [0]
  Input framing errors:                0                                   [0]
  Policed discards:                    0                                   [0]
  L3 incompletes:                      0                                   [0]
  L2 channel errors:                 232                                   [0]
  L2 mismatch timeouts:                0                                   [0]
  Carrier transitions:                 1                                   [0]
  Output errors:                       0                                   [0]
  Output drops:                        0                                   [0]
  Aged packets:                        0                                   [0]
 
<爲了便於閱讀,此處省略了其他無關的輸出內容>

此命令是每秒鐘都在刷新,如果大家有條件可以在設備上嘗試一下,博客上無法動態展現給大家。


工具 12: monitor traffic interface (cli 層面的tcpdump 數據抓包)

有些時候配置了路由協議,但是發現其沒有正常工作,在開啓 debug 模式之前,我們還有一個最簡單的辦法來排錯,那就是抓包。


想到抓包,大家是不是第一印象就是做鏡像端口,然後把 PC 接入設備用 Wireshark 抓取。


在 Juniper 設備上,如果你只是想抓取到達路由器本身的流量,例如像上面所述的路由協議排查,我們可以很簡單地在命令行模式下抓取到達路由器的數據包。


下面演示一個命令行模式下用 monitor trafficinterface 的抓包:

admin@Juniper>monitor traffic interface ge-1/1/4.100 no-resolve    
verbose outputsuppressed, use <detail> or <extensive> for full protocol decode
Address resolution isOFF.
Listening onge-1/1/4.100, capture size 96 bytes
 
s00:46:38.271636  In IP 1.2.3.4.179 > 1.2.3.5.61855: P662902184:662902203(19) ack 2900796289 win 16384 <nop,nop,timestamp3271803181 643396392>: BGP, length: 19
00:46:38.371433 Out IPtruncated-ip - 2 bytes missing! 1.2.3.5.61855 > 1.2.3.4.179: . ack 19 win16384 <nop,nop,timestamp 643418995[|tcp]>
00:46:38.724938 OutIS-IS, L2 Lan IIH, src-id 0011.0000.0001, lan-id 0011.0000.0001.03, prio 64,length 47
00:46:39.174455 OutES-IS, ISH, length 26
00:46:41.199436 OutIS-IS, L2 Lan IIH, src-id 0011.0000.0001, lan-id 0011.0000.0001.03, prio 64,length 47
00:46:42.161045  In IS-IS, L2 Lan IIH, src-id 0011.0000.0254,lan-id 0011.0000.0001.03, prio 64, length 47
00:46:42.481016 OutIS-IS, L2 CSNP, src-id 0011.0000.0001.00, length 47
00:46:43.797914 OutIS-IS, L2 Lan IIH, src-id 0011.0000.0001, lan-id 0011.0000.0001.03, prio 64,length 47
00:46:43.864907 Out IPtruncated-ip - 21 bytes missing! 1.2.3.5.61855 > 1.2.3.4.179: P 1:20(19) ack19 win 16384 <nop,nop,timestamp 643424488[|tcp]>
00:46:43.964852  In IP 1.2.3.4.179 > 1.2.3.5.61855: . ack20 win 16384 <nop,nop,timestamp 3271808875 643424488>
00:46:46.147386 OutIS-IS, L2 Lan IIH, src-id 0011.0000.0001, lan-id 0011.0000.0001.03, prio 64,length 47
00:46:48.400363 OutIS-IS, L2 Lan IIH, src-id 0011.0000.0001, lan-id 0011.0000.0001.03, prio 64,length 47
00:46:49.340934  In IS-IS, L2 Lan IIH, src-id 0011.0000.0254,lan-id 0011.0000.0001.03, prio 64, length 47
^C
15 packets received byfilter
0 packets dropped bykernel

從上面輸出可以看出,兩臺路由器之間有 BGP 通信流量,同時還有 IS-IS IGP 路由協議流量。


當然,monitor traffic interface 還有更多選項。例如 extensive 可以看到更多的數據包內容,通過 match 可以像 tcpdump 一樣,設定源、目標地址,端口等信息來過濾數據包。

 

工具 13: show interface terse | refresh xxx (定期自動刷新)

此工具是用於定時自動刷新某些顯示的命令行內容。打個比方,有時候我們難免遇到需要同遠端聯調設備端口。在完成相應配置後,雙方需要確認接口是否 up。一般方法是按向上的方向鍵調出命令不斷手工刷新狀態,其實更簡單的辦法就是使用 refresh。


Refresh 可以用於任何狀態顯示的自動定期刷新,例如定期接口狀態,定期刷新 OSPF,ISIS,BGP 鄰居狀態等。


如下案例,設定路由器每 秒鐘自動刷新一次接口狀態。

admin@Juniper> showinterfaces terse ge-0/0/2 | refresh 1 | no-more 
---(refreshed at2017-09-14 22:00:55 NZST)---
Interface               Admin Link Proto    Local                 Remote
ge-0/0/2                up    up
ge-0/0/2.10             up    up  
ge-0/0/2.32767          up   up  
---(refreshed at2017-09-14 22:00:56 NZST)---
Interface               Admin Link Proto    Local                 Remote
ge-0/0/2                up    up
ge-0/0/2.10             up    up  
ge-0/0/2.32767          up   up  
---(refreshed at2017-09-14 22:00:58 NZST)---
Interface               Admin Link Proto    Local                 Remote
ge-0/0/2                up    up
ge-0/0/2.10             up    up  
ge-0/0/2.32767          up   up  
---(refreshed at2017-09-14 22:00:59 NZST)---
Interface               Admin Link Proto    Local                 Remote
ge-0/0/2                up    up
ge-0/0/2.10             up    up  
ge-0/0/2.32767          up   up  
---(refreshed at2017-09-14 22:01:00 NZST)---
Interface               Admin Link Proto    Local                 Remote
ge-0/0/2                up    up
ge-0/0/2.10             up    up  
ge-0/0/2.32767          up   up  
---(refreshed at2017-09-14 22:01:01 NZST)---
Interface               Admin Link Proto    Local                 Remote
ge-0/0/2                up    up
ge-0/0/2.10             up    up  
ge-0/0/2.32767          up   up

### 割接結束 ####


以上就是在使用 Juniper 設備過程中常用的運維技巧和方法,相信有了這麼多工具和思路的幫助下,對於大家的日常工作相比原來是否就不那麼恐慌了。至少可以對自己的配置胸有成竹,自信的敲下 commit 的回車鍵了!




附錄:以下爲額外贈送的福利,拿走不謝。


福利一:apply-group

你是不是在配置 Juniper SRX 的時候希望在每一個 zone 層級下配置 host-inbound-traffic 允許基本的 ping 和 traceroute。


例如有三個 zone:internal,external,dmz。普通的方法就是針對每一個 zone,配置對應的 host-inbound-policy,其實還有更簡單的方法,那就是利用 apply-group。


示例:

set groups ALLOW_PING_TRACEsecurity zones security-zone <*> interfaces <*>host-inbound-traffic system-services ping
set groupsALLOW_PING_TRACE security zones security-zone <*> interfaces <*>host-inbound-traffic system-services traceroute


你會發現這裏使用了通配符。這就是祕訣所在,通過通配符的方式,你可以匹配任意的 security zone 的任意接口。並在其底下允許 ping 和 traceroute


應用 groups:

set security zonesapply-groups ALLOW_PING_TRACE


查看配置,以 internal zone 爲例:

admin@JuniperSRX>show configuration security zones security-zone internal |display set            
set security zones security-zoneinternal interfaces irb.5


貌似沒看見配置?別急,讓我們使用 inheritance 的查看方式,此命令用於查看從其他地方繼承下來的配置,例如上文的 groups 配置。 

lg@JuniperSRX>show configuration security zones security-zone internal |display inheritance 
interfaces {
    irb.5 {
        ##
        ## 'host-inbound-traffic' was inheritedfrom group ALLOW_PING_TRACE
        ##
        host-inbound-traffic {
            system-services {
                ##
                ## 'ping' was inherited fromgroup ALLOW_PING_TRACE
                ##
                ping;
                ##
                ## 'traceroute' was inheritedfrom group ALLOW_PING_TRACE
                ##
                traceroute;
            }
        }
    }
}


福利二:apply-path

大家在配置 Juniper 路由器自身防護的時候,希望只允許設備配置的 BGP 鄰居與設備通過 TCP 端口 179 通訊,對於其他未配置的 BGP 鄰居丟棄目標爲 TCP 端口 179 的數據。


通常方法爲,先蒐集設備配置的所有 BGP 鄰居 IP 地址,在 firewall filter 裏面逐個添加。


費時費力不說,最不方便的是它是靜態配置,如果後續有新的 BGP 鄰居添加的時候,還得同步更新此 firewall filter。


那有什麼既省時,又省力的方法呢?讓我們來看看神奇的 apply-path


示例:

在這一臺 MX 路由器上,有如下的 BGP 鄰居配置:

admin@MX480> show configuration | grep bgp | display set | except "firewall|policy" |grep local-add 

set protocols bgp group TO_RR local-address 1.1.0.1

set routing-instances Internet protocols bgp group UpStream_Peering neighbor 2.2.2.1 local-address2.2.2.2

set routing-instances Internet protocols bgp group UpStream_Peering-IPv6 neighbor 2:2:37::2local-address 2:2:37::1

set routing-instances Internet protocols bgp group CUSTOMER_InternetA neighbor 1.1.4.4 local-address1.1.4.5

set routing-instances Internet protocols bgp group CUSTOMER_InternetA-IPv6 neighbor 1:1:4::4local-address 1:1:4::5

set routing-instances VRF_A protocols bgp group customer_A neighbor 10.10.2.100 local-address10.10.2.1

set routing-instances VRF_A protocols bgp group customer_A_v6 neighbor 10:10:2::100 local-address10:10:2::1

set routing-instances VRF_A protocols bgp group customer_B_v6 neighbor 10:10:20::100 local-address10:10:20::1


通過 apply-path 自動挑出鄰居 IP


先在 prefix-list 裏面定義 apply-path:

set policy-optionsprefix-list bgp-neighbors apply-path "protocols bgp group <*>neighbor <*>"

set policy-optionsprefix-list bgp-neighbors-logical-systems apply-path "logical-systems<*> protocols bgp group <*> neighbor <*>"

set policy-optionsprefix-list bgp-neighbors-routing-instance apply-path "routing-instances<*> protocols bgp group <*> neighbor <*>"

set policy-optionsprefix-list bgp-neighbors-routing-instance-logical-systems apply-path"logical-systems <*> routing-instances <*> protocols bgp group<*> neighbor <*>"


這個命令會挑出符合以上路徑的最後一個通配符的數值,在這裏就是 neighbor 後面的 IP 地址。一般情況在 Juniper 裏面 bgp 會存在於三個地方,全局路由表下,VRF 下,以及邏輯路由器下。所以上面就定義了三個 apply-path,並分別放置在不同的 prefix-list 內部。


讓我們來查看一下匹配是否成功,還是通過 inheritance 查看全局路由表的 BGP 鄰居匹配情況:

admin@MX480# showpolicy-options prefix-list bgp-neighbors | display inheritance 
##
## apply-path wasexpanded to:
##     1.1.0.1/32; 
##
apply-path"protocols bgp group <*> neighbor <*>";


最後就是引用prefix-list :

set firewall family inet filter router-protect term 1-accept-bgp from source-prefix-list bgp-neighbors

set firewall family inet filter router-protect term 1-accept-bgp from source-prefix-list bgp-neighbors-routing-instance

set firewall family inet filter router-protect term 1-accept-bgp from source-prefix-list bgp-neighbors-logical-systems

set firewall family inet filter router-protect term 1-accept-bgp from source-prefix-list bgp-neighbors-routing-instance-logical-systems

set firewall family inet6 filter router-protect6 term 1-accept-bgp from source-prefix-list bgp-neighbors

set firewall family inet6 filter router-protect6 term 1-accept-bgp from source-prefix-list bgp-neighbors-routing-instance

set firewall family inet6 filter router-protect6 term 1-accept-bgp from source-prefix-list bgp-neighbors-logical-systems

set firewall family inet6 filter router-protect6 term 1-accept-bgp from source-prefix-list bgp-neighbors-routing-instance-logical-systems


並把 firewall filter 應用到 lo0 接口上。(注:在 Juniper 的 JUNOS 內,路由器防護一般方法就是添加 ACL 並綁定在 lo0 接口上)

set interfaces lo0 unit 0 family inet filter input router-protect
set interfaces lo0 unit 0 family inet6 filter input router-protect6


大功告成!碼字很辛苦,贊一個吧?



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