"긴 핑"만 출력 / 연결 중단 로그

"긴 핑"만 출력 / 연결 중단 로그

가끔 연결이 잠시 중단되는 경우가 있는데 를 사용할 때 정확히 ping다음과 같은 출력이 생성되는지 확인하려고 합니다.

64 bytes from 192.168.0.1: icmp_req=1196 ttl=64 time=4.64 ms
64 bytes from 192.168.0.1: icmp_req=1197 ttl=64 time=5.14 ms
64 bytes from 192.168.0.1: icmp_req=1198 ttl=64 time=4.90 ms
64 bytes from 192.168.0.1: icmp_req=1199 ttl=64 time=25293 ms < -- interruption
64 bytes from 192.168.0.1: icmp_req=1200 ttl=64 time=24286 ms      starts here
64 bytes from 192.168.0.1: icmp_req=1201 ttl=64 time=23278 ms
64 bytes from 192.168.0.1: icmp_req=1202 ttl=64 time=22270 ms
64 bytes from 192.168.0.1: icmp_req=1203 ttl=64 time=21262 ms
64 bytes from 192.168.0.1: icmp_req=1204 ttl=64 time=20254 ms
64 bytes from 192.168.0.1: icmp_req=1224 ttl=64 time=142 ms
64 bytes from 192.168.0.1: icmp_req=1225 ttl=64 time=4.87 ms
64 bytes from 192.168.0.1: icmp_req=1226 ttl=64 time=4.54 ms

내가 어떻게 할 수있는?

  • time. 할 수 grep있어?
  • 타임스탬프를 추가하세요( -D옵션에 ​​대해 알고 있지만 ping사람이 읽을 수 있는 스탬프를 선호합니다).

답변1

이 솔루션은 grepawk(이 질문에서 찢어졌습니다., 어쩌면 과잉일 수도 있지만 작동합니다)

ping 192.168.0.1 \
| grep -E "time=[0-9]{2}" --line-buffered \
| gawk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }'

답변2

과잉 해결책에 관해 말하자면, 여전히 첫 번째 질문에 대한 답변을 원할 경우(X보다 긴 시간의 줄만 가져옴) 다음과 같은 추악한 bash-python 하이브리드 스크립트를 사용해 보십시오.

#!/bin/bash

IFS='\n' read -r -d '' check_ping <<EOF
from sys import stdin
data = reduce(lambda x, y: str(x) + str(y), stdin.readlines())
time = int(float(data.split("time=")[-1].split(" ")[0].strip()))
if time > $2: print(data)
EOF

ping -c 1 $1 | python -c "${check_ping}"

이를 스크립트 파일(예: ping)에 넣으면 test.sh두 개의 매개변수, 즉 ping하려는 호스트 이름과 ping 출력을 에코하지 않아야 하는 최소 TTL을 전달할 수 있습니다. 예를 들어, 실행하면 tst.sh www.google.com 100100ms 이상 걸리는 www.google.com에 대한 핑만 반환합니다.

답변3

나는 유닉스 환경에서 하는 일이지만 이런 종류의 텍스트를 처리하는 아이디어를 좋아하지 않습니다. Perl & Net::Ping을 살펴볼 수도 있습니다. 이를 통해 자신만의 시간 제한을 정의하고 핑이 실패할 때만 조치를 취할 수 있습니다. 자세한 내용은 여기를 참조하세요:

http://perldoc.perl.org/Net/Ping.html

관련 정보