WGET에서 HTTP 응답 헤더를 구문 분석합니다
문제
나는 wget의 결과에서 라인을 추출하려고하지만 문제가 있습니다. 이것은 내 wget 전화입니다.
$ wget -SO- -T 1 -t 1 http://myurl.com:15000/myhtml.html
산출:
--18:24:12-- http://xxx.xxxx.xxxx:15000/myhtml.html => `-' Resolving xxx.xxxx.xxxx... xxx.xxxx.xxxx Connecting to xxx.xxxx.xxxx|xxx.xxxx.xxxx|:15000... connected. HTTP request sent, awaiting response... HTTP/1.1 302 Found Date: Tue, 18 Nov 2008 23:24:12 GMT Server: IBM_HTTP_Server Expires: Thu, 01 Dec 1994 16:00:00 GMT Location: https://xxx.xxxx.xxxx/siteminderagent/... Content-Length: 508 Keep-Alive: timeout=10, max=100 Connection: Keep-Alive Content-Type: text/html; charset=iso-8859-1 Location: https://xxx.xxxx.xxxx//siteminderagent/... --18:24:13-- https://xxx.xxxx.xxxx/siteminderagent/... => `-' Resolving xxx.xxxx.xxxx... failed: Name or service not known.
내가 이것을한다면 :
$ wget -SO- -T 1 -t 1 http://myurl.com:15000/myhtml.html | egrep -i "302" <br/>
문자열이 포함 된 줄을 반환하지 않습니다. 사이트 나 사이트 마인더가 올라 있는지 확인하고 싶습니다.
해결책
당신이 찾고있는 wget의 출력은 stderr에 작성되었습니다. 리디렉션해야합니다.
$ wget -SO- -T 1 -t 1 http://myurl.com:15000/myhtml.html 2>&1 | egrep -i "302"
다른 팁
wget
헤더를 Stdout이 아닌 Stderr에 인쇄합니다. 다음과 같이 stderr를 stderr로 리디렉션 할 수 있습니다.
wget -SO- -T 1 -t 1 http://myurl.com:15000/myhtml.html 2>&1 | egrep -i "302"
"2> & 1"부분은 파일 디스크립터 1 (STDOUT)으로 파일 디스크립터 2 (STDERR)를 리디렉션하라고 표시합니다.
이미 제공된 솔루션의 약간 향상된 버전
wget -so- -t 1 -t 1 http://myurl.com:15000/myhtml.html 2> & 1>/dev/null | grep -c 302
2>&1 >/dev/null
불필요한 출력을 제거합니다. 이런 식으로 Egrep은 wget의 stderr 만 구문 분석 할 것입니다. STDOUT에서 302가 포함 된 문자열을 잡을 수있는 가능성을 제거합니다 (HTML 파일 자체가 출력 + 다운로드 바이트 카운트가있는 Proces Bar) :)
egrep -c
단순히 출력하는 대신 일치하는 문자열의 수를 계산합니다. Egrep이 얼마나 많은 스트링이 일치했는지 알기에 충분합니다.
wget-서버-응답 http://www.amazon.de/xyz 2> & 1 | awk '/^ http/{print $ 2}'
조금만 설명하기 위해. 그만큼 -S
원래 질문의 전환은 속기입니다 --server-response
.
또한 OP가 지정된 것을 알고 있습니다 wget
, 하지만 curl
비슷하고 기본값이 stdout과 기본값입니다.
curl --head --silent $yourURL
또는
curl -I -s $yourURL
그만큼 --silent
스위치는 필요합니다 grep
-능력: (-s
진행 상황 % meter)