首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在foreach中的IP列表中跳转

问在foreach中的IP列表中跳转
EN

Stack Overflow用户
提问于 2019-03-21 18:10:33
回答 1查看 29关注 0票数 0

我正在编写一个小脚本,以便在一个脚本中收集一些智慧和选项。为了以后也为了我自己。它很简单,但包含了大量的信息和“可重用性”。

我现在正试图在列表的“运行”过程中实现一个跳跃。到目前为止,我所能想到的就是通过正则表达式来实现这一点。但我可以正确地使用大麦正则表达式。

代码语言:javascript
复制
#Custom Ping to identify the scenario
function Custom-Ping {
    Param(
        [string]$Address
    )
    $ping = ping $Address /w 1 /n 1
    if (![string]::IsNullOrEmpty($ping -Like "*(100% loss)*")) {
        $result = "Error"
    } elseif(![string]::IsNullOrEmpty($ping -Like "*expired*")) {
        $result = "Warning"
    } else {
        $result = "succeded"
    }
    return $result
}

$ErrorActionPreference = "SilentlyContinue"

$tstart = Get-Date
$counter = 0
$unreachable = 0

$IPlist = foreach ($Network in 1..29) {
    foreach ($Range in 1..254) {
        "10.10.$Network.$Range"
    }
}

foreach ($IP in $IPlist) {
    $counter ++
    try {
        if ($unreachable -lt 6) {
            #SwitchCase
            $case = Custom-Ping $IP

            switch ($case) {
                succeded {
                    Write-Host "Response from: $IP" -ForegroundColor Green
                    #$IP | Export-Csv -Path D:\somepath.csv -Append
                }
                Error {
                    Write-Host "No Response from: $IP" -ForegroundColor Red
                    #$IP | Export-Csv -Path D:\somepath.csv -Append
                }
                Warning {
                    Write-Host "Time Expired on: $IP" -ForegroundColor Yellow
                    #$IP | Export-CSV -path D:\somepath.csv -Append
                    $unreachable ++
                }
                default {
                    Write-Warning "An Error Occured while processing"
                }
            }
        } else {
            #Hop to the next range as this range isnt accesibble
            #$IPswap = $IP
            #newIP = $Ipswap "$Network from 2 to 3"
            #$IP=$newIP
            $unreachable = 0
            Write-Host "The Network xxxx cant be reached"
        }
    } catch {
        Write-Warning "Other Error"
    }
}

$tend = Get-Date
Write-Host "$counter Completed Ping requests"
New-TimeSpan -Start $tstart -End $tend | select Minutes, Seconds

这是目前为止的脚本..。直到现在,我还没有找到一种方法来实现“网络”的这一飞跃。

例如,它在10.10.2.0网络中得到5不可达,然后设置为10.10.3.0网络,并重新开始该过程。

我想知道在这种情况下这是否可能。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-21 19:02:22

改用嵌套循环和标签:

代码语言:javascript
复制
:Network # Now we can jump into the next iteration of this loop from an inner loop
foreach ($Network in 0..29)
{
    :Host
    foreach($Node in 0..254){
        $IP = "10.10.$Network.$Node"

        #Counter for "Statistic"
        $counter ++
        try
        {
            #Check error sum for Range Hop
            if($unreachable -lt 6)
            {
                #SwitchCase
                $case = Custom-Ping $IP

                switch ($case)
                {
                succeded
                {           
                    Write-Host "Response from: $IP" -ForegroundColor Green
                    #$IP | Export-Csv -Path D:\somepath.csv -Append
                }
                Error
                {     
                    Write-Host "No Response from: $IP" -ForegroundColor Red
                    #$IP | Export-Csv -Path D:\somepath.csv -Append
                }
                Warning
                {
                    Write-Host "Time Expired on: $IP" -ForegroundColor Yellow
                    #$IP | Export-CSV -path D:\somepath.csv -Append
                    $unreachable ++
                }
                default
                {
                    Write-Warning "An Error Occured while processing"
                }
                }
            }
            else
            {
                $unreachable = 0
                Write-Host "The Network 10.10.$Network.0/24 cant be reached, jumping to 10.10.$($Network + 1).0/24"

                # jump forward to the next iteration of the outer "Network" loop
                continue Network
            }
        }
        catch
        {
            Write-Warning "Other Error" 
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55278026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档