Vbs程序批量修改防火墙路由

近期朋友在外面做了一个项目,需要对网络架构做整改,明白需求后,准备第二天给客户做架构调整;据了解他们的有两条线路,一条电信、一条网通;两条线都是通过一个路由器进行出网的,他们有一个防火墙,需要将电信的网络切换到防火墙上,切换前,他就说需要添加一个路由表,我们知道电信的路由表比较多,所以需要手动去添加,所以在网上就下载了一个比较完整的电信路由表,但是网上下载的电信的路由表的子网是反向的,需要将反向的子网改成正向的才能使用,改的前提是是需要去计算。怎么去计算呢,用255减去反向子网的值,但后得到的结果作为准确值使用。所以就想到了vbs程序修改。

网上下载的路由表结构:

route outside 58.32.0.0 0.31.255.255

以下数据是我们从网上下载的完整的电信路由表。我们需要根据自己的需求更改为:

Route outside 58.32.0.0 255-0.255-31.255-255.255-255 +下一跳(106.34.168.2

根据以上算法我们需要得到的结果是:

route outside 58.32.0.0 255.224.0.0 106.34.168.2

我们下载下来的电信路由表总共有445条,如果用手动改的话,工作量相当大。所以就想到了vbs,所以在此给大家展示一下。方便有需要的同学使用

clip_p_w_picpath002

clip_p_w_picpath003

需求明确后,我们开始编辑vbs脚本

filename="route.txt"
filename2="route2.txt"
Set WshShell =CreateObject("WScript.Shell")
currentpath= WshShell.CurrentDirectory
Set fso = createobject("Scripting.FilesystemObject")
Set fileObj = fso.OpenTextFile(currentpath&"\"&filename,1)
Set fileObj2 = fso.OpenTextFile(currentpath&"\"&filename2,2,true)
Do while Not fileObj.AtEndOfStream
content = fileObj.ReadLine
strResult=""
If RegExpTest("^route.*",content) Then
strArray=split(content," ",-1)
strLast=strArray(UBound(strArray))
strLastArray=split(strLast,".",-1)
strLen=UBound(strLastArray)
For i=Lbound(strLastArray) to UBound(strLastArray)
str2=255-strLastArray(i)
strResult=strResult&str2&"."
Next
strResult=left(strResult,len(strResult)-1)
For i=Lbound(strArray) to UBound(strArray)-1
strFinalResult = strFinalResult &" "& strArray(i)
Next
strFinalResult = strFinalResult & " "&strResult &" " & "106.34.168.2 " & vbcrlf
End If
Loop
fileObj2.Write strFinalResult
fileObj2.Close
fileObj.Close
Function RegExpTest(patternstr,str)
Dim regEx, retVal ' Create variable.
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patternstr ' Set pattern.
regEx.IgnoreCase = False ' Set case sensitivity.
retVal = regEx.Test(str) ' Execute the search test.
If retVal Then
RegExpTest=True
Else
RegExpTest=False
End If
End Function
msgbox "finish"

clip_p_w_picpath005

注:我们执行脚本的时候,需要将route.txt文件和脚本文件存放在同一个目录下才可以执行。

我们执行前的内容:

clip_p_w_picpath007

执行后,会在同一个目录下生成一个route2.txt文件,该文件是修改后生成的。

脚本执行完成

clip_p_w_picpath009

我们查看生成后的文件

clip_p_w_picpath011