
我讀過很多關於如何用 Notepad++ 替換的文章,但我似乎一無所獲。
這是一個 Juniper 配置範例,我將其更改為另一種語言:
set policy id 3016 from "CSA" to "Untrust" "1.1.1.1/32" "2.2.2.2/32" "SSH" permit log
set policy id 3016
set dst-address "3.3.3.3/32"
exit
!
set policy id 3053 name "Footprints" from "Untrust" to "CSA" "Blocked Addresses" "Any" "ANY" deny log
set policy id 3053
exit
現在我想將其更改為 Fortinet 語言,但對於我來說,要開始此過程,我需要它來查找某些變數。我需要更改的幾位是:
set policy id 3016 from "CSA" to "Untrust" "1.1.1.1/32" "2.2.2.2/32" "SSH" permit log
set policy id 3016
set dst-address "3.3.3.3/32"
exit
!
set policy id 3053 name "Footprints" from "Untrust" to "CSA" "Blocked Addresses" "Any" "ANY" deny log
set policy id 3053
exit
到
edit 0
set srcintf "CSA"
set dstintf "Untrust"
set srcaddr "1.1.1.1/32"
set dstaddr "2.2.2.2/32" "3.3.3.3/32"
set action accept
set schedule "always"
set service "SSH"
set fsso disable
set nat enable
next
edit 0
set name "Footprints"
set srcintf "Untrust"
set dstintf "CSA"
set srcaddr "Blocked Addresses"
set dstaddr "all"
set action accept
set schedule "always"
set service "ALL"
set fsso disable
set nat enable
next
所以我使用以下方法來嘗試讓我先開始:
尋找(set policy id (.+))
並替換為edit 0
- 這似乎突出顯示了所有內容,並留下了空配置,只需編輯 0 即可。
然後我嘗試查找(set policy id *)
並替換為edit 0
- 這給我留下了所有內容,但對其進行了更改,使其看起來如下所示:(基本上在 XXXX 之前插入 0
edit 03016 from "CSA" to "Untrust" "1.1.1.1/32" "2.2.2.2/32" "SSH" permit log set policy id 3016 set dst-address "3.3.3.3/32" exit
任何幫助將不勝感激,因為這可以節省我的時間!
答案1
恕我直言,您應該用您最喜歡的腳本語言編寫腳本。
但如果您確實想使用 Ntepad++ 完成這項工作,這裡有一個正規表示式解決方案。正如您所看到的,正規表示式和替換有點複雜,需要良好的知識來維護。
Ctrl+H
找什麼:
set policy id \d+ (?:(?:(?!exit).)*?name (".+?"))?(?:(?!exit).)*?from (".+?")(?:(?!exit).)*?to (".+?")(?:(?!exit).)*?("\d{1,3}(?:\.\d{1,3}){3}/\d+"|"[\w ]+") ("\d{1,3}(?:\.\d{1,3}){3}/\d+"|"[\w ]+") (".+?")(?:(?:(?!exit).)*?set dst-address (".+?"))?(?:(?!exit).)+?exit
用。
edit 0\n\t(?1set name $1\n\t:)set srcintf $2\n\tset dstintf $3\n\tset srcaddr $4\n\tset dstaddr $5(?7 $7:)\n\tset action accept\n\tset schedule "always"\n\tset service $6\n\tset fsso disable\n\tset nat enable\nnext
查看 相符
查看 環繞
查看 正規表示式
查看
. matches newline
Replace all
Find All in Current Document
解釋:
set policy id \d+ # literally and digits for id
(?: # non capture group
(?:(?!exit).)*? # 0 or more any character but not the word "exit"
name (".+?") # name is captured in group 1
)? # end group, optional
(?:(?!exit).)*? # 0 or more any character but not the word "exit"
from (".+?") # from is captured in group 2
(?:(?!exit).)*? # 0 or more any character but not the word "exit"
to (".+?") # to is captured in group 3
(?:(?!exit).)*? # 0 or more any character but not the word "exit"
("\d{1,3}(?:\.\d{1,3}){3}/\d+"|"[\w ]+") # group 4, src IP or literal like "Blocked Addresses"
("\d{1,3}(?:\.\d{1,3}){3}/\d+"|"[\w ]+") # group 5, dst IP or literal like "Any"
(".+?") # group 6, service
(?: # non capture group
(?:(?!exit).)*? # 0 or more any character but not the word "exit"
set dst-address # literally
(".+?") # group 7, 1 or more any character, not greedy
)? # end group, optional
(?:(?!exit).)*? # 0 or more any character but not the word "exit"
exit # literally
替代品:
edit 0\n\t # edit 0 followed by linefeed and tabulation
(?1 # if group 1 exists, (name)
set name $1\n\t # print the name (group 1)
: # else
# nothing
) # end if
set srcintf $2\n\t # and so on ...
set dstintf $3\n\t
set srcaddr $4\n\t
set dstaddr $5
(?7 $7:)\n\t
set action accept\n\t
set schedule "always"\n\t
set service $6\n\t
set fsso disable\n\t
set nat enable\n
next
螢幕截圖(之前):
螢幕截圖(之後):
你會找到完整的解釋這裡