Board logo

标题: [原创] IIS过滤IP管理VBS脚本 [打印本页]

作者: ejzhang    时间: 2020-2-4 08:20     标题: IIS过滤IP管理VBS脚本

为保证IIS安全或限制某些IP访问站点,需要在IIS安全性里设置过滤IP地址,为了方便命令行操作,参照网上的脚本写了一个
  1. Dim IisW3Svc, SiteServers, SiteIdLists
  2. SiteServers = "WebSite Listing:" & vbCrLf
  3. SiteIdLists = "|"
  4. On Error Resume Next
  5. Set IisW3Svc = GetObject("IIS://LocalHost/W3SVC")
  6. If Err.Number = 0 Then
  7.     For Each WebSrvr in IisW3Svc
  8.         If IsNumeric(WebSrvr.Name) Then
  9.             SiteIdLists = SiteIdLists & WebSrvr.Name & "|"
  10.             SiteServers = SiteServers & WebSrvr.ServerComment & "(SiteId:" & WebSrvr.Name & ", IpBinding:" & Join(WebSrvr.ServerBindings(0),"") & ")" & vbCrLf
  11.         End If
  12.     Next
  13. Else
  14.     WScript.Echo "The IIS cannot access!"
  15.     WScript.Quit(-1)
  16. End If
  17. On Error Goto 0
  18. If WScript.Arguments.Count = 0 Then
  19.     WScript.Echo SiteServers
  20. End If
  21. If WScript.Arguments.Count <> 2 Then
  22.     WScript.Echo "Usage: cscript " & WScript.ScriptName & " <SiteId> [*|+|-]<NewIp>"
  23.     WScript.Quit(1)
  24. End If
  25. Dim SiteId, NewIp, Action, reIPv4
  26. SiteId = WScript.Arguments(0)
  27. NewIp = WScript.Arguments(1)
  28. If InStr(SiteIdLists, "|" & SiteId & "|") = 0 Then
  29.     WScript.Echo "The <SiteId> is NOT valid!"
  30.     WScript.Quit(2)
  31. End If
  32. If Left(NewIp, 1) = "*" Then
  33.     Action = "List"
  34. ElseIf Left(NewIp, 1) = "-" Then
  35.     Action = "Del"
  36. Else
  37.     Action = "Add"
  38. End If
  39. If Action <> "List" Then
  40.     If Left(NewIp, 1) = "+" Or Left(NewIp, 1) = "-" Then
  41.         NewIp = Mid(NewIp, 2)
  42.     End If
  43.     Set reIPv4 = New regExp
  44.     reIPv4.Pattern = "^((25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(25[0-5]|2[0-4]\d|1?\d?\d)$"
  45.     If reIPv4.Test(NewIp) = 0 Then
  46.         WScript.Echo "The <NewIp> is NOT valid!"
  47.         WScript.Quit(2)
  48.     Else
  49.         NewIp = NewIp & ", 255.255.255.255"
  50.     End If
  51. End If
  52. Dim IisSecObj, IisIpSec
  53. On Error Resume Next
  54. Set IisSecObj = GetObject("IIS://LocalHost/W3SVC/" & SiteId & "/ROOT")
  55. If Err.Number = 0 Then
  56.     Set IisIpSec = IisSecObj.IPSecurity
  57. Else
  58.     WScript.Echo "SiteId is WRONG!"
  59.     WScript.Quit(-1)
  60. End If
  61. On Error Goto 0
  62. Dim IpLists
  63. If IisIpSec.GrantByDefault Then
  64.     IpLists = IisIpSec.IPDeny
  65. Else
  66.     IpLists = IisIpSec.IPGrant
  67. End If
  68. If Action = "List" Then
  69.     If IisIpSec.GrantByDefault Then
  70.         WScript.Echo "Default Grant access" & vbCrLf & Replace(Join(IpLists, vbCrLf), ", 255.255.255.255", "")
  71.     Else
  72.         WScript.Echo "Default Deny access" & vbCrLf & Replace(Join(IpLists, vbCrLf), ", 255.255.255.255", "")
  73.     End If
  74.     WScript.Quit(0)
  75. End If
  76. IpLists = "|" & Join(IpLists, "|")
  77. If Action = "Add" Then
  78.     If InStr(IpLists, NewIp) = 0 Then
  79.         IpLists = IpLists & "|" & NewIp
  80.     Else
  81.         WScript.Echo "Ip address " & Replace(NewIp, ", 255.255.255.255", "") & " is exist!"
  82.         WScript.Quit(3)
  83.     End If
  84. End If
  85. If Action = "Del" Then
  86.     If InStr(IpLists, NewIp) > 0 Then
  87.         IpLists = Replace(IpLists, "|" & NewIp, "")
  88.     Else
  89.         WScript.Echo "Ip address " & Replace(NewIp, ", 255.255.255.255", "") & " is not exist!"
  90.         WScript.Quit(3)
  91.     End If
  92. End If
  93. IpLists = Split(Mid(IpLists, 2), "|")
  94. If IisIpSec.GrantByDefault Then
  95.     IisIpSec.IPDeny = IpLists
  96. Else
  97.     IisIpSec.IPGrant = IpLists
  98. End If
  99. On Error Resume Next
  100. IisSecObj.IPSecurity = IisIpSec
  101. If Err.Number = 0 Then
  102.     IisSecObj.SetInfo
  103.     If Action = "Add" Then
  104.         WScript.Echo "Ip address " & Replace(NewIp, ", 255.255.255.255", "") & " is added!"
  105.     Else
  106.         WScript.Echo "Ip address " & Replace(NewIp, ", 255.255.255.255", "") & " is deleted!"
  107.     End If
  108.     WScript.Quit(0)
  109. Else
  110.     WScript.Echo "NewIp is WRONG!"
  111.     WScript.Quit(-1)
  112. End If
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2