# Ping ###### tags: `script`,`windows`, `linux` ## 1. Windows `[1739982536.602150] 64 bytes from 168.95.1.1: time=19 ms` `[1739982537.321024] no answer yet` ``` powershell $logFile = "x:\matsu\ping.txt" $targetIP = "168.95.1.1" $unixEpoch = [datetime]"1970-01-01T00:00:00Z" while ($true) { # $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" # 取得當前時間並轉換為 +8 時區 $now = (Get-Date).ToUniversalTime().AddHours(8) $unixTime = ($now - $unixEpoch).TotalSeconds $formattedTs = "[{0:F6}]" -f $unixTime # 執行 Ping $pingResult = Test-Connection -ComputerName $targetIP -Count 1 -ErrorAction SilentlyContinue if ($pingResult) { $message = "$formattedTs 64 bytes from $($targetIP): time=$($pingResult.ResponseTime) ms" } else { $message = "$formattedTs no answer yet" } # 輸出到螢幕與 ANSI 檔案 Write-Output $message $message | Out-File -FilePath $logFile -Encoding Default -Append Start-Sleep -Seconds 5 } ``` ## 2. Linux `[1739857066.329874] 64 bytes from 168.95.1.1: icmp_seq=2661 ttl=59 time=18.7 ms` ``` shell #linux ping -OD -i [sec] [ip] # O 顯示丟包 # D 顯示時間戳(Unix) # i 幾秒送出一次 ``` ## 1. Windows ```javascript= @echo off SETLOCAL ENABLEDELAYEDEXPANSION set lan=192.168.0 :: ######### for 1~254 ######### for /l %%i in (1,1,254) do ( ping -n 1 -w 1 !lan!.%%i > nul if !errorlevel! == 0 ( echo !lan!.%%i O echo !lan!.%%i >> logPing.txt ) else ( echo !lan!.%%i X ) ) :: ######### 根據檔案 ######### for /F "tokens=*" %%i in (list.txt) do ( ping -n 1 -w 1 %%i > nul if !errorlevel! == 0 ( echo %%i O echo %%i >> logPing.txt ) else ( echo %%i X ) ) :: ######### 陣列 ######### set list[0]=192.168.0.100 set list[1]=192.168.0.103 set list[2]=192.168.0.105 for /L %%i in (0,1,2) do ( echo !list[%%i]! ) ``` ## 2. linux ``` bash= lan=192.168.0. for i in $(seq 1 254); do ip=$lan$i ping -c 1 -W 1 $ip > /dev/null if [ $? -eq 0 ]; then echo "$ip O" else echo "$ip X" fi done ``` `nbtscan -v -s : 192.168.0.1/24` `nmap -sP 192.168.0.0-255`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up