"함수 is ping()"의 두 판 사이의 차이

69번째 줄: 69번째 줄:
==See also==
==See also==
*[[is_port_open]]
*[[is_port_open]]
*[[is_ipmiping]]
*[[ping]]
*[[ping]]
*[[is_ipmiping]]


[[Category: Bash]]
[[Category: Bash]]
[[Category: csharp]]
[[Category: csharp]]
[[category: PHP]]
[[category: PHP]]

2014년 1월 17일 (금) 10:52 판

  • test set
2.2.2.2 → false
8.8.8.8 → true
naver.com → false
yahoo.com → true

1 Bash

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

2 C#

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;
	}
}

3 PHP

  • Linux
function is_ping($host, $timeout = 2) {
	return exec("ping -c1 -W$timeout $host | grep received | awk '{print $4}'");
}

4 PowerShell

$pinghost="8.8.8.8"
echo (!(get-wmiobject win32_pingstatus -Filter "Address='$pinghost'").statuscode)
# True
$pinghost="8.8.8.9"
echo (!(get-wmiobject win32_pingstatus -Filter "Address='$pinghost'").statuscode)
# False
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

5 See also

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}