ç¶ãã¨ãããã以åã¯Unixç°å¢ã§ã®è©±ã ã£ãã®ã§ãä»åã¯Windowsã§pingã§çµ¨æ¯¯çæãã¦LANã«æ¥ç¶ãã¦ããæ©å¨ã®IPã¢ãã¬ã¹ãåæãã¦ã¿ããã
ã¾ãã¯ã·ã³ãã«ã«ãããããã¡ã¤ã«ã§é次å®è¡ããæ¹æ³ã
@echo off setlocal set ADDRBASE=192.0.2. for /L %%I in (1, 1, 254) do ( ping -n 1 -w 100 %ADDRBASE%%%I >nul if not errorlevel 1 ( echo %ADDRBASE%%%I ) ) endlocal
Windowsã®pingã¯ã¿ã¤ã ã¢ã¦ãå¤ãããªç§åä½ã§è¨å®ãããLANã«æ¥ç¶ãã¦ããæ©å¨ã®å¿çæ§è½ã¨ã®å ¼ãåãã¯ããããã¿ã¤ã ã¢ã¦ãå¤ãçããããã¨ã§å®è¡æéãç縮ãããã¨ãã§ããã
å®éããã®ããããã¡ã¤ã«ï¼ã¿ã¤ã ã¢ã¦ã100ããªç§ï¼ã¯æå ã®ç°å¢ã§ã¯2å10ç§å¼±ã§å®äºããã
ã¨ã¯ããé次å®è¡ã§ãã以ä¸ãã©ããã¦ãæéãããã£ã¦ãã¾ããä½ã¨ãã§ããªããã®ã
ï¼ãã³ãã³ãããã³ããã§ã¯ç¡çããã ããPowerShellã§ã¯ã©ãã ãããï¼
PowerShellã§ä¸¦åå¦çããæ¹æ³ã¯ããã¤ãããããç´ ã®PowerShell 5.1ã®å ´åã¯ãããã¯ã°ã©ã¦ã³ãã¸ã§ãã使ãæ¹æ³ã¨Workflowã使ãæ¹æ³ã®2ã¤ãèããããã
ããã¯ã°ã©ã¦ã³ãã¸ã§ãã使ãæ¹æ³ã§ãä½ãèããã«ã·ã³ãã«ã«ã³ã¼ããæ¸ãã¨ããããªæãã«ãªãã
Set-StrictMode -Version Latest 1..254 | %{ $addr = "192.0.2.$_" Start-Job -ScriptBlock { param($addr) $out = ping -n 1 -w 500 $addr if ("$out" -cnotmatch '100%') { $addr } } } | Wait-Job | Receive-Job
ãã®ã¹ã¯ãªãããæå ã®ç°å¢ã§ã¯5åå¾ ã£ã¦ãå¦çãçµãããªãã®ã§ãããã¿ã¹ã¯ããã¼ã¸ã£ã¼ã§ç¢ºèªããã¨ããã大éã®ããã»ã¹ãçæããã¦åä½ãã¦ãããããã»ã¹ã®çææ°ãå¶å¾¡ããæ¹ãããããã ã
ã¨ãããã¨ã§ãã¢ã¬ã³ã¬ã¨è©¦è¡é¯èª¤ãã¦ãã©ãçãããæå ã®ç°å¢ã§æãé«éãªã¹ã¯ãªããã¯ãããªæãã
Set-StrictMode -Version Latest Set-Variable -Name ADDRBASE -Value '192.0.2.' -Option Constant $jobs = @() for ($i = 1; $i -le 254; $i++) { $jobs += Start-Job -ArgumentList "$ADDRBASE$i" -ScriptBlock { param($addr) $out = ping -n 1 -w 500 $addr if ("$out" -cnotmatch '100%') { $addr } } $tmp = $jobs | ?{ $_.State -eq 'Running' } if (($tmp | Measure-Object).Count -ge 5) { Wait-Job $tmp -Any | Out-Null } } Wait-Job $jobs | Receive-Job | Write-Host Remove-Job $jobs
ä¿¡ããããªããããããªãããforæã使ã£ã¦ããã®ãã«ã¼ãä¸ã§Wait-Jobãå®è¡ããã¿ã¤ãã³ã°ãã«ã¼ãä¸ã®Wait-Jobã®ãªãã·ã§ã³-Anyããå ¨ã¦æå ã®ç°å¢ã§ã®é«éåã«çµã³ã¤ãã¦ãããç¾ç©åããã§ãããªã£ãã
ãã®ã¹ã¯ãªããã¯â¦â¦ããã»ã¹ã常æ5å以ä¸ç«ã¡ä¸ãã¦CPUãã¬ã³ã¬ã³å æãã¦å¦çãè¡ãå²ã«ãæå ã®ç°å¢ã§ã¯2å16ç§ã»ã©ããããããããã¡ã¤ã«ã«ããé次å®è¡çã®æ¹ãæ°ç§éãä¸ã«ãCPUãã»ã¨ãã©å æããªããã©ããããã¨ã ï¼
ããã¯ã°ã©ã¦ã³ãã¸ã§ãã«å¤±æããã®ã§ãPowerShell 3.0ã§å°å ¥ãããWorkflowã使ã£ã¦ã¿ãã
Set-StrictMode -Version Latest workflow Ping-All { ForEach -parallel ($i in @(1..254)) { $addr = "192.168.24.$i" $out = ping -n 1 -w 500 $addr if ("$out" -cnotmatch '100%') { $addr } } } Ping-All | Sort-Object { [Version] $_ }
ãªã½ã¼ã¹ã®å æå ·åã¯ãã·ã«ãªã£ããã®ã®ããã¯ãæå ã®ç°å¢ã§ã¯ãããªãã«æéãããããã¨ã¯ããããããã¡ã¤ã«ã«ããé次å®è¡çããã¯å°ãé«éã«ãªã£ãã
Windowsä»å±ã®PowerShellã§ã®ä¸¦åå¦çã®é ãã«ã¯ä¸æºããããæ¨æºã§ThreadJobå ¥ãã«ãªãã¨ããã®ã ãâ¦â¦ä»å¾ãPowerShell Coreãã¼ã¹ã®å®è£ ãæ¨æºæ·»ä»ãããããã«ãªãã®ãã©ãããå«ãã¦ãå°ãæ°ã«ãªãã¨ããã§ããã