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

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 2명의 중간 판 3개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
[[Category: Network]]
;is_ping()
*test set
*test set
:2.2.2.2 → false  
:2.2.2.2 → false  
7번째 줄: 10번째 줄:


==Bash==
==Bash==
<source lang='bash'>
[[Category: Bash]]
<syntaxhighlight lang='bash'>
PING_TIMEOUT=2
PING_TIMEOUT=2
PING_HOST=8.8.8.8
PING_HOST=8.8.8.8
13번째 줄: 17번째 줄:
echo $IS_PING
echo $IS_PING
# 1
# 1
</source>
</syntaxhighlight>
 
== Windows Batch ==
ERRORLEVEL의 값이 0이면 참, 1이면 실패이다.
<syntaxhighlight lang='batch'>
@echo off
ping -n 1 8.8.8.8 1>nul 2>nul
echo %ERRORLEVEL%
REM 0
</syntaxhighlight>


==C#==
==C#==
<source lang='csharp'>
[[Category: csharp]]
<syntaxhighlight lang='csharp'>
public bool is_ping(string host) {
public bool is_ping(string host) {
try {
try {
33번째 줄: 47번째 줄:
}
}
}
}
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
*Linux
*Linux
<source lang='php'>
<syntaxhighlight lang='php'>
function is_ping($host, $timeout = 2) {
function is_ping($host, $timeout = 2) {
return exec("ping -c1 -W$timeout $host | grep received | awk '{print $4}'");
return exec("ping -c1 -W$timeout $host | grep received | awk '{print $4}'");
}
}
</source>
</syntaxhighlight>


==PowerShell==
==PowerShell==
[[category:powerShell]]
[[category:powerShell]]
<source lang='powershell'>
<syntaxhighlight lang='powershell'>
$pinghost="8.8.8.8"
$pinghost="8.8.8.8"
echo (!(get-wmiobject win32_pingstatus -Filter "Address='$pinghost'").statuscode)
echo (!(get-wmiobject win32_pingstatus -Filter "Address='$pinghost'").statuscode)
# True
# True
</source>
</syntaxhighlight>
<source lang='powershell'>
<syntaxhighlight lang='powershell'>
$pinghost="8.8.8.9"
$pinghost="8.8.8.9"
echo (!(get-wmiobject win32_pingstatus -Filter "Address='$pinghost'").statuscode)
echo (!(get-wmiobject win32_pingstatus -Filter "Address='$pinghost'").statuscode)
# False
# False
</source>
</syntaxhighlight>
<source lang='powershell'>
<syntaxhighlight lang='powershell'>
Function is_ping([string] $pinghost) {
Function is_ping([string] $pinghost) {
return (!(get-wmiobject win32_pingstatus -Filter "Address='$pinghost'").statuscode)
return (!(get-wmiobject win32_pingstatus -Filter "Address='$pinghost'").statuscode)
65번째 줄: 80번째 줄:
echo $result
echo $result
# False
# False
</source>
</syntaxhighlight>


==See also==
==같이 보기==
*[[is_port_open]]
*[[함수 is_port_open()]]
*[[is_ipmiping]]
*[[함수 is_ipmiping()]]
*[[ping]]
*[[ping]]
[[Category: Bash]]
[[Category: csharp]]
[[category: PHP]]

2020년 11월 2일 (월) 02:36 기준 최신판

1 개요[ | ]

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

2 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

3 Windows Batch[ | ]

ERRORLEVEL의 값이 0이면 참, 1이면 실패이다.

@echo off
ping -n 1 8.8.8.8 1>nul 2>nul
echo %ERRORLEVEL%
REM 0

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

5 PHP[ | ]

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

6 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

7 같이 보기[ | ]

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