是否可以配置 IIS,使其在下载某个目录中的文件时发送电子邮件通知?例如: 中的任何文件www.example.com/download/
?
答案1
最好的办法可能是以合理的小间隔轮询 IIS 日志文件以查找您要查找的请求。
以下 powershell 脚本应该可以执行您想要的操作。显然,请更改变量以满足您的需求。
# Directory of IIS log files
$Path = "C:\inetpub\logs\LogFiles\W3SVC1"
#Get the most recent log file from the directory
$File = Get-ChildItem $Path | sort LastWriteTime | select -last 1
# Get-Content gets the file, pipe to Where-Object and skip the first 3 lines.
$Log = Get-Content $File.FullName | where {$_ -notLike "#[D,S-V]*" }
# Replace unwanted text in the line containing the columns.
$Columns = (($Log[0].TrimEnd()) -replace "#Fields: ", "" -replace "-","" -replace "\(","" -replace "\)","").Split(" ")
# Count available Columns, used later
$Count = $Columns.Length
# Get all Rows that i Want to retrieve
$QueryString = "*GET*"
$Rows = $Log | where {$_ -like $QueryString}
# Create an instance of a System.Data.DataTable
$IISLog = New-Object System.Data.DataTable "IISLog"
# Loop through each Column, create a new column through Data.DataColumn and add it to the DataTable
foreach ($Column in $Columns) {
$NewColumn = New-Object System.Data.DataColumn $Column, ([string])
$IISLog.Columns.Add($NewColumn)
}
# Loop Through each Row and add the Rows.
foreach ($Row in $Rows) {
$Row = $Row.Split(" ")
$AddRow = $IISLog.newrow()
for($i=0;$i -lt $Count; $i++) {
$ColumnName = $Columns[$i]
$AddRow.$ColumnName = $Row[$i]
}
$IISLog.Rows.Add($AddRow)
}
#Format Log data into string for sending
$BodyString = ""
foreach( $Row in $IISLog.Rows ){
$BodyString = $BodyString + $Row.date + " " + $Row.time + " " + $Row.sip + " " + $Row.csuriquery + "`n"
}
# Variables for sending email
$MailServer = "NAME OF YOUR MAIL SERVER"
$FromAddress = ""
$ToAddress = ""
$Subject = ""
$SMTP = New-Object Net.Mail.SmtpClient($MailServer)
$SMTP.Send($FromAddress,$ToAddress,$Subject,$BodyString)
我使用了以下页面作为参考。
答案2
有一种方法可以使用 IIS Url Rewrite 模块和自定义 Rewrite 提供程序。这意味着您必须编写一些代码,但我已为您完成了此操作:
首先,你需要Url Rewrite 模块,无论如何,有它总是好的。安装它并确保它能正常工作。
接下来按照文章中的步骤操作为 URL 重写模块开发自定义重写提供程序
该文章中有一个代码部分,请将其替换为以下内容:
using System;
using System.Collections.Generic;
using System.Net.Mail;
using Microsoft.Web.Iis.Rewrite;
public class DownloadAlerter : IRewriteProvider, IProviderDescriptor
{
private string recipient, sender, server;
public void Initialize(IDictionary<string, string> settings, IRewriteContext rewriteContext)
{
if (!settings.TryGetValue("Recipient", out recipient) || string.IsNullOrEmpty(recipient))
throw new ArgumentException("Recipient provider setting is required and cannot be empty");
if (!settings.TryGetValue("Sender", out sender) || string.IsNullOrEmpty(sender))
throw new ArgumentException("Sender provider setting is required and cannot be empty");
if (!settings.TryGetValue("Server", out server) || string.IsNullOrEmpty(server))
throw new ArgumentException("Server provider setting is required and cannot be empty");
}
public string Rewrite(string value)
{
MailMessage message = new MailMessage();
message.From = new MailAddress(sender);
message.To.Add(new MailAddress(recipient));
message.Subject = "Download Alert";
message.Body = string.Format("The following URL was downloaded: '{0}'" + Environment.NewLine + "For details check your IIS logs", value);
SmtpClient client = new SmtpClient();
client.Host = server;
client.Send(message);
return value;
}
public IEnumerable<SettingDescriptor> GetSettings()
{
yield return new SettingDescriptor("Recipient", "Email address of the recipient");
yield return new SettingDescriptor("Sender", "Email address of the sender");
yield return new SettingDescriptor("Server", "The SMTP server to be used");
}
}
本文接下来介绍了如何更改您的 web-config,请使用如下方法:
<rewrite>
<providers>
<provider name="DownloadAlerter" type="DownloadAlerter, Hahndorf.IIS.DownloadAlertProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx">
<settings>
<add key="Recipient" value="peter@******.****" />
<add key="Sender" value="iis@******.****" />
<add key="Server" value="localhost" />
</settings>
</provider>
</providers>
<rules>
<rule name="Test">
<match url="specific-page\.html" />
<action type="Rewrite" url="{DownloadAlerter:{URL}}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
是provider type
您按照本文创建的提供程序集的强名称。允许settings
您指定服务器和收件人。match url
定义您接收电子邮件的 URL。
这只是一个概念验证,但对我来说效果很好。我遇到了一个问题,因为我在设置为“无托管代码”的 IIS 站点上测试了这一点,您需要有一个与.NET CLR Version
您编写提供程序的版本相匹配的设置。
另外,我无法访问有关该请求的更多信息,例如远程 IP 地址,但我并没有对此进行深入研究。
我上传了项目提交到 GitHub。
答案3
不知道有没有一个一体化的解决方案,但是可以寻找这个:
1- 使用软件读取 iis 日志,当某些内容与监控文件匹配时,使用一些“代码”通过电子邮件发送结果。在 logstash 中查找带有电子邮件输出的 elk stack。不要忘记先启用日志记录。
2-当有人访问此文件时,使用您编写的 Web 应用程序来生成邮件。