1 개요[ | ]
- is_ping()
- test set
- 2.2.2.2 → false
- 8.8.8.8 → true
- naver.com → false
- yahoo.com → true
2 Bash[ | ]
Bash
Copy
PING_TIMEOUT=2
PING_HOST=8.8.8.8
IS_PING=`ping -c1 -W$PING_TIMEOUT $PING_HOST | grep received | awk '{print $4}'`
echo $IS_PING
# 1
3 Windows Batch[ | ]
ERRORLEVEL의 값이 0이면 참, 1이면 실패이다.
batch
Copy
@echo off
ping -n 1 8.8.8.8 1>nul 2>nul
echo %ERRORLEVEL%
REM 0
4 C#[ | ]
C#
Copy
public bool is_ping(string host) {
try {
int timeout = 120;
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "Ping_ABCDEFGHIJKLMNOP1234567890";
byte[] buffer = Encoding.ASCII.GetBytes(data);
PingReply reply = pingSender.Send(host, timeout, buffer, options);
if (reply.Status == IPStatus.Success) return true;
return false;
}
catch (Exception) {
return false;
}
}
5 PHP[ | ]
- Linux
PHP
Copy
function is_ping($host, $timeout = 2) {
return exec("ping -c1 -W$timeout $host | grep received | awk '{print $4}'");
}
6 PowerShell[ | ]
powershell
Copy
$pinghost="8.8.8.8"
echo (!(get-wmiobject win32_pingstatus -Filter "Address='$pinghost'").statuscode)
# True
powershell
Copy
$pinghost="8.8.8.9"
echo (!(get-wmiobject win32_pingstatus -Filter "Address='$pinghost'").statuscode)
# False
powershell
Copy
Function is_ping([string] $pinghost) {
return (!(get-wmiobject win32_pingstatus -Filter "Address='$pinghost'").statuscode)
}
$result = is_ping('8.8.8.8')
echo $result
# True
$result = is_ping('8.8.8.9')
echo $result
# False
7 같이 보기[ | ]
편집자 Jmnote Ykhwong Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.