![OS X와 Linux/기타 운영 체제 간의 Ping 시간 초과 차이](https://linux55.com/image/30604/OS%20X%EC%99%80%20%E2%80%8B%E2%80%8BLinux%2F%EA%B8%B0%ED%83%80%20%EC%9A%B4%EC%98%81%20%EC%B2%B4%EC%A0%9C%20%EA%B0%84%EC%9D%98%20Ping%20%EC%8B%9C%EA%B0%84%20%EC%B4%88%EA%B3%BC%20%EC%B0%A8%EC%9D%B4.png)
제가 작성한 이 함수는 OSX와 Linux 명령 간의 특성 ping
, 특히 -W
시간 초과 옵션을 반영해야 합니다.
# Checks if a host is up-and-running and responding to pings
function my_function_is_network_host_up() {
local -ri N_ARGUMENTS=1
if ! check_n_arguments "${FUNCNAME}" "${N_ARGUMENTS}" "${@}"; then return; fi
local -r NETWORK_HOST=$1
local -ri n_requests=1
local -i wait_for_reply=1 # Seconds
if [[ $my_global_os_type == 'OSX' ]]; then
((wait_for_reply *= 1000)) # Milliseconds
fi
ping -c "$n_requests" \
-W "$wait_for_reply" \
-q \
"$NETWORK_HOST" \
&> /dev/null
return $?
}
실제 시스템 관리자는 방금 뛰어든 시스템과 별도로 호스트를 확인하기 위해 더 나은 Bash 기능을 사용합니까?
나의 일반적인 사용법:
hs=(...);
for h in "${hs[@]}"; do
if my_function_is_network_host_up $h; then
do_stuff
fi
done
그렇지 않으면:
my_function_is_network_host_up $h && do_stuff
배경
OSX
Time in milliseconds to wait for a reply for each packet sent. If a reply arrives later, the packet is not printed as replied, but considered as replied when calculating statistics.
리눅스
Time to wait for a response, in seconds. The option affects only timeout in absense of any responses, otherwise ping waits for two RTTs.
답변1
귀하의 기능이 무엇인지 이해하지 못합니다. 단순 달리기에 비해 어떤 이점이 있나요?
for host in host1 host2 host3; do ping -c 1 host >/dev/null && do_stuff; done
호스트가 작동 중인지 확인하고 호스트가 시작될 때 명령을 실행하려면 위의 내용으로 충분합니다.