簡體   English   中英

Powershell-在所有文件和子目錄中搜索除127.0.0.1之外的所有IP地址

[英]Powershell - Search for all IP addresses except 127.0.0.1 in all files and subdirectories

我想在給定路徑的所有文件和子目錄中搜索除127.0.0.1之外的所有IP地址。

我努力了:

function searchstr($string)
{
    ls -Recurse | Select-String $string -List | Select Path
}

這使我可以搜索特定的IP

searchstr "192.153.133.2"

但並非所有ip ...

我通常使用Eclipse中的正則表達式來執行此操作,如下所示:1) ctrl + h => 在此處輸入圖片說明

但是這個項目是如此之大,並且有太多對ips的引用,當我嘗試在其上運行該正則表達式時,蝕就消失了。

我希望我的輸出看起來像:

C:\full\path\to\file.php
C:\full\path\to\ipFile.txt

並排除帶有127.0.0.1條目

試試看。 可能可以修改正則表達式以在Select-String命令中執行127.0.0.1排除,但我不確定。 我只對正則表達式足夠了解。

Get-ChildItem -Recurse `
| Select-String -Pattern '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' `
| ? { $_.Line -ne '127.0.0.1' } `
| Select-Object -ExpandProperty Path -Unique

更新資料

試試看。

Get-ChildItem -Recurse `
| Select-String -Pattern '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' -AllMatches `
| ? { 
    $matches = ($_.Matches | Select-Object -Unique)
    return $matches.Count -gt 1 -or $matches[0].Value -ne '127.0.0.1' 
} `
| Select-Object -ExpandProperty Path -Unique

這應該排除包含OP在注釋中顯示的行的文件:

IP地址= 127.0.0.1

但是,它不會排除具有以下行的文件(建議的修復將排除具有此行的文件):

IP地址= 127.0.0.1; IP地址= 8.8.8.8

嘗試這個 ;)

 $dirroot="C:\temp\root"
 $IPregex=[regex]‘(?<Address>((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))’
 gci -Recurse $dirroot -File | foreach {$res=(Get-Content $_.fullname); if ($res -Match $IPregex -and $Matches.Address -ne "127.0.0.1"  ) {New-Object -TypeName psobject -Property @{IP=$IPregex.Match($res).Value; File=$_}} }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM