找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 13116|回复: 0

[原创] IIS过滤IP管理VBS脚本

[复制链接]
发表于 2020-2-4 08:20:36 | 显示全部楼层 |阅读模式
为保证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
复制代码

评分

参与人数 1技术 +1 收起 理由
Batcher + 1 感谢分享

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 04:49 , Processed in 0.018305 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表