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,所以在此给大家展示一下。方便有需要的同学使用 需求明确后,我们开始编辑vbs脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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)toUBound(strLastArray) str2=255-strLastArray(i) strResult=strResult&str2& "." Next strResult=left(strResult,len(strResult)-1) For i=Lbound(strArray)toUBound(strArray)-1 strFinalResult=strFinalResult& "" &strArray(i) Next strFinalResult=strFinalResult& "" &strResult& "" & "106.34.168.2" &vbcrlf End If Loop fileObj2.WritestrFinalResult fileObj2.Close fileObj.Close Function RegExpTest(patternstr,str) Dim regEx,retVal 'Createvariable. Set regEx= New RegExp 'Createregularexpression. regEx.Pattern=patternstr 'Setpattern. regEx.IgnoreCase= False 'Setcasesensitivity. retVal=regEx.Test(str) 'Executethesearchtest. If retVal Then RegExpTest= True Else RegExpTest= False End If End Function msgbox "finish" 注:我们执行脚本的时候,需要将route.txt文件和脚本文件存放在同一个目录下才可以执行。 我们执行前的内容: 执行后,会在同一个目录下生成一个route2.txt文件,该文件是修改后生成的。 脚本执行完成 我们查看生成后的文件 本文转自 高文龙 51CTO博客,原文链接:http://blog.51cto.com/gaowenlong/1556815,如需转载请自行联系原作者