Windows

如何使用命令行或腳本為 Windows SMTP 添加允許的中繼 IP

  • May 19, 2016

我有一長串 IP 地址需要添加為允許中繼到 Windows Server 2008 R2 上的內置 SMTP 服務(IIS 7.5,但 SMTP 使用舊的 IIS 6.0 管理器)

我知道如何手動添加允許的繼電器,但是列表很長,所以我希望將其自動化。

有什麼方法可以從命令行、腳本或其他自動化中做到這一點?

IIS 6 SMTP 伺服器?此配置位於 IIS 元數據庫 XML 文件中的systemroot\system32\inetserv\metabase.xml Line: RelayIpList

VBS 設置中繼 IP:

Option Explicit
Dim objSMTP,objRelayIpList,objCurrentList,objIP,objFSO,objTextFile,count,newIpList(),inputOption
Set objSMTP = GetObject("IIS://localhost/smtpsvc/1")
Set objRelayIpList = objSMTP.Get("RelayIpList") 
'objRelayIpList is of type IIsIPSecuritySetting http://msdn.microsoft.com/en-us/library/ms525725.aspx
Wscript.Echo "============================================" 
Wscript.Echo "CURRENT SETTINGS"
Wscript.Echo "================"
Wscript.Echo " " 
Wscript.Echo "Computer(s) that may relay through this virtual server."
Wscript.Echo " " 
' GrantByDefault returns 0 when "only the list below" is set (false) and -1 when all except the list below is set(true)
If objRelayIpList.GrantByDefault = true Then
   Wscript.Echo "All except the list below :"
   objCurrentList = objRelayIpList.IPDeny
Else 
   Wscript.Echo "Only the list below :"
   objCurrentList = objRelayIpList.IPGrant
End If
count = 0
For Each objIP in objCurrentList
       Wscript.Echo objIP 
       count = count + 1
Next
If count = 0 Then
   Wscript.Echo "*NIL*"
End If
Wscript.Echo "============================================" 
Wscript.Echo " " 
Wscript.Echo "Replacing ReplayIpList with the IP address(es) from the ip.txt file."
Wscript.Echo " "
Do While Not((inputOption = "a") Or (inputOption = "d") Or (inputOption = "x") ) 
Wscript.Echo "ENTER " 
Wscript.Echo "A to add to Allow List (Only the list below)"
Wscript.Echo "D to add to Deny List (All except the list below)"
Wscript.Echo "X Exit without making changes"
Wscript.Echo " "
inputOption = lcase(trim(Wscript.StdIn.ReadLine))
Loop
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("ip.txt") Then
   Set objTextFile = objFSO.OpenTextFile("ip.txt",1)

   count = 0
   Do Until objTextFile.AtEndOfStream
       Redim Preserve newIpList(count)
       newIpList(count) = objTextFile.Readline
       count = count + 1
   Loop
   objTextFile.Close

   'For each objIP in newIpList
   '    Wscript.Echo objIP
   'Next
   Wscript.Echo " "
   Select Case inputOption
       Case "a"
           objRelayIpList.GrantByDefault = false
           objRelayIpList.IpGrant = newIpList
           Wscript.Echo "SET " & count &" address(es) to Allow List"        
       Case "d"
           objRelayIpList.GrantByDefault = true
           objRelayIpList.IpDeny = newIpList
           Wscript.Echo "SET " & count &" address(es) to Deny List"
       Case "x"
           Wscript.Echo "Exiting without making changes"
           Wscript.Echo "============================================" 
           Wscript.Quit
   End Select

   objSMTP.Put "RelayIpList",objRelayIpList
   objSMTP.SetInfo
   Wscript.Echo " "

   Wscript.Echo "============================================" 
Else
   Wscript.Echo "Please create a file ip.txt that contains the list of IP address(es)"
   Wscript.Echo "FORMAT : Each Line should be IP,MASK "
   Wscript.Echo "EX     : 127.0.0.1,255.255.255.255"
End If

學分:

Script to Import a bunch of IP addresses to the ReplayIpList             

                    (c)vijaysk@microsoft.com                             
                    blogs.msdn.com/vijaysk                               


USAGE : cscript ImportRelayList.vbs                                      
PREREQUISITE : This script needs ip.txt in the same folder.              
Store your IP addresses in ip.txt FORMAT: Each line should be IP,MASK  

來自:http: //blogs.msdn.com/b/vijaysk/archive/2009/05/07/setting-smtp-relayiplist-from-a-script.aspx

未經我測試(BlueCompute)

引用自:https://serverfault.com/questions/703565