etc/host 文件条目匹配或不匹配,如果条目不匹配,则发送一个触发器 [英] etc/host file entries match or not if not match entries then send a Triger

查看:41
本文介绍了etc/host 文件条目匹配或不匹配,如果条目不匹配,则发送一个触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我是 PowerShell 脚本的新手.我正在尝试做主机文件条目应该匹配来自 C:\Windows\System32\drivers\etc 的 IP 和 DNS 名称条目动态列表中的每个条目.任何不匹配都会导致审核失败.

Here, I am new to PowerShell scripting. I am trying to do Host file entries should match every entry in a dynamic list of IP and DNS name entries from C:\Windows\System32\drivers\etc. Any mismatch will fail the audit.

条目是

10.23.24.45           foo.com
10.24.45.34           domain.com

这是我的代码.

"$Pattern = '^(?<IP>\d{1,3}(\.\d{1,3}){3})\s+(?<Host>.+)$'
$File    = "$env:SystemDrive\Windows\System32\Drivers\etc\hosts"
$Entries = @()

(Get-Content -Path $File)  | ForEach-Object {
  If ($_ -match $Pattern) {
    $Entries += "$($Matches.IP)  $($Matches.Host)"
    Write-Host " the values are $Entries"
    $FailureMessage = "IP and host entries are existing"
  }
  else {
    $FailureMessage = "IP and host entries are doesn't existing"
  }
}"

但这对我不起作用.你能在这里帮忙吗

But this is not working for me. Can you help here

推荐答案

主机文件还可以在 IP 和主机名部分之后包含注释行或注释,以 # 字符开头.您目前的正则表达式没有考虑到这一点.

A hosts file can also contain comment lines or comments after the IP and Hostname parts, preceeded by a # character. Your regex at the moment does not account for that.

我将创建一个包含所有必需条目的查找哈希表,并使用它来查找主机文件是否包含其中任何一个,并因此创建一个 PSObject 数组以实现良好的格式设置和轻松过滤.

I would create a lookup hashtable with all required entries and use that to find whether the hosts file contains any of these or not and as result create an array of PSObjects for nice formatting and easy filtering.

类似于:

$file    = "$env:SystemDrive\Windows\System32\Drivers\etc\hosts"
$pattern = '^\s*(?<IP>[0-9a-f.:]+)\s+(?<HostName>[^\s#]+)(?<Comment>.*)$'
# create an array of Hashtables with required entries
$required = @{Ip = '10.23.24.45'; HostName = 'foo.com'},
            @{Ip = '10.24.45.34'; HostName = 'domain.com'}

# read the current content of the hosts file, filter only lines that match the pattern
$result = Get-Content -Path $file | Where-Object { $_ -match $pattern } | ForEach-Object {
    $ip = $matches.Ip
    $hostname = $matches.HostName
    # test if the entry is one of the required ones
    $exists = [bool]($required | Where-Object { $_.Ip -eq $ip -and $_.HostName -eq $hostname })
    # output an object
    [PsCustomObject]@{
        IP = $ip
        HostName = $hostname
        Exists = $exists
    }
}

# show results on screen
$result | Format-Table -AutoSize

接下来您可以使用发送电子邮件如果缺少任何必需的条目,则发送邮件消息

# select the entries where property Exists is False
$missing = $result | Where-Object { -not $_.Exists }
if ($missing) {
    # here is where you send your mail message
}

正则表达式详情:

^                   Assert position at the beginning of a line (at beginning of the string or after a line break character)
\s                  Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.)
   *                Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?<IP>              Match the regular expression below and capture its match into backreference with name "IP"
   [0-9a-f.:]       Match a single character present in the list below
                    A character in the range between "0" and "9"
                    A character in the range between "a" and "f"
                    One of the characters ".:"
      +             Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)                  
\s                  Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.)
   +                Between one and unlimited times, as many times as possible, giving back as needed (greedy)
(?<HostName>        Match the regular expression below and capture its match into backreference with name "HostName"
   [^\s#]           Match a single character NOT present in the list below
                    A whitespace character (spaces, tabs, line breaks, etc.)
                    The character "#"
      +             Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)                  
(?<Comment>         Match the regular expression below and capture its match into backreference with name "Comment"
   .                Match any single character that is not a line break character
      *             Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)                  
$                   Assert position at the end of a line (at the end of the string or before a line break character)

这篇关于etc/host 文件条目匹配或不匹配,如果条目不匹配,则发送一个触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆