我正在测试一些IP:端口代理,通过下载内容来查看其中一些代理是否有效。我的工作脚本是
#!/bin/bash for pro in $(cat testing.txt);do wget -e use_proxy=yes -e http_proxy=$pro --tries=1 --timeout=15 http://something.com/download.zip if grep --quiet "200 OK"; then echo $pro >> ok.txt else echo $pro >>notok.txt fi done
wget成功的典型输出是
--2014-06-08 10:45:31-- http://something.com/download.zip Connecting to 186.215.168.66:3128... connected. Proxy request sent, awaiting response... 200 OK Length: 30688 (30K) [application/zip] Saving to: `download.zip' 100%[======================================>] 30,688 7.13K/s in 4.2s
并且失败时输出
--2014-06-08 10:45:44-- http://something.com/download.zip Connecting to 200.68.9.92:8080... connected. Proxy request sent, awaiting response... Read error (Connection timed out) in headers. Giving up.
现在的问题是,grep似乎无法正常工作!它输出notok.txt文件中的所有IP地址。天气wget成功与否它输出notok.txt中的所有地址。我该如何解决这个问题?
答案 0 :(得分:0)
以下是一些更正:
1)您应该避免使用for i in $(cmd)
。你应该阅读:
2)grep
必须读取一个流:
grep [options...] [pattern] file
command|grep [options...] [pattern]
grep [options...] [pattern] < <(command)
grep [options...] [pattern] <<< "some text"
3)在这种情况下无需使用grep
:
#!/bin/bash
while read -r pro; do
out="$(wget -e use_proxy=yes -e http_proxy=$pro --tries=1 --timeout=15 http://something.com/download.zip)"
if [[ $out =~ "200 OK" ]]; then
echo "$pro" >> ok.txt
else
echo "$pro" >> notok.txt
fi
done < testing.txt
答案 1 :(得分:0)
#!/usr/bin/bash
files="proxies_list.txt" #file with proxies to test in <ip>:<port> format
rm not_ok.txt
rm ok.txt
while read -r pro; do
out=$(wget.exe -qO- --tries=1 --timeout=5 --no-check-certificate -e use_proxy=on -e https_proxy=$pro https://example.com)
if [[ $out ]]; then
echo "$pro" >> ok.txt
echo "$pro ---- OK"
else
echo "$pro" >> not_ok.txt
echo "$pro ---- NOT OK"
fi
done < "$files"