我试图弄清楚为什么我的“ $ count--”每次循环到新IP时都将整数递减2。我希望每次检查新IP时将其倒数1。
function scrape {
$PortList = @(443, 4433, 444, 433, 4343, 4444, 4443)
$IPList = $text1.text.split("`r")
$IPList = $text1.text.split()
$count = ($IPList.Count - 1)/2
Write-Host $IPList
Write-Host 'Firewalls with open ports will be listed here as they are discovered. Please wait while the script' `n 'processes the list of IP addresses. There are'$count 'IP addresses to check'
foreach ($IP in $IPList) {
$count--
foreach ($Port in $PortList) {
$url = "https://${IP}:${Port}"
$verb = 'GET'
$SiteData = try{httpget $url $verb}Catch{Continue}
If ($SiteData.Contains("auth1.html")) {
Write-Host ('https://' + $IP + ':' + $Port + " MGMT " + $IP) -ForegroundColor Red
$text2.text += ('https://' + $IP + ':' + $Port + " MGMT " + $IP + "`n")
}
Else {
If ($SiteData.Contains("SSLVPN")) {
Write-Host ('https://' + $IP + ':' + $Port + " SSLVPN " + $IP)
$text2.text += ('https://' + $IP + ':' + $Port + " SSLVPN " + $IP + "`n")
}
Else {
Write-Host ('https://' + $IP + ':' + $Port + " OTHER " + $IP)
$text2.text += ('https://' + $IP + ':' + $Port + " OTHER " + $IP + "`n")
}
}
}
}
}
编辑/更新:好的,所以我弄清楚循环正在将IP地址之间的空白计数为数组的成员,这将导致双倍减量。现在我只需要弄清楚如何只计算地址即可。
答案 0 :(得分:0)
这看起来很可疑:
$count = (15 - 1)/2
您是否尝试过运行脚本并从foreach循环中输出$ count的值,以确认该值就是您认为的值?
我还认为您在完成工作后需要移动增量语句。
使用以下示例,计数输出将返回以下内容:
function scrape {
$PortList = @(443, 4433, 444, 433, 4343, 4444, 4443)
#$IPList = $text1.text.split("`r")
#$IPList = $text1.text.split()
$IPList = ("IP:A", "IP:B", "IP:C", "IP:D")
$count = ($IPList.Count - 1)
Write-Host $IPList
#Write-Host 'Firewalls with open ports will be listed here as they are discovered. Please wait while the script' `n 'processes the list of IP addresses. There are'$count 'IP addresses to check'
foreach ($IP in $IPList) {
Write-Host "count: " + $count
# go do some stuff
$count--
}
}
答案 1 :(得分:0)
从那时起,您发现问题是$IPList
数组在要检查的IP地址之间包含空元素。
原因是您尝试将多行字符串分割成几行都没有正确的尝试:
$text1.text.split("`r") # !! retains the `n after `r -> elements start with `n
$text1.text.split() # !! splits by *both* `r and `n -> empty extra elements
最简单的解决方案是使用-split
运算符的一元形式,该形式方便地通过 any 空格运行将字符串拆分为令牌,包括换行符(无论它们是Windows样式的CRLF换行符(`r`n
)还是Unix样式的仅LF换行符(`n
):
-split $text1.text # split by runs of whitespace, including newlines
但是,请注意,如果各行包含行内空格(空格,制表符),它们也将被分解为令牌。
如果您确实只想按换行符(换行符)进行拆分,请使用-split
operator的 binary 形式:
$text1.text -split '\r?\n' # split by CRLF or LF
请注意,-split
的RHS是正则表达式; \r?\n
是与CRLF和仅LF换行符都匹配的模式。
如果您知道换行符仅是适用于平台的换行符,则可以使用
$text1.text -split [Environment]::NewLine
。