-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-url
More file actions
executable file
·190 lines (148 loc) · 3.77 KB
/
test-url
File metadata and controls
executable file
·190 lines (148 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
#
# version history
# 1.0 initial
# 1.1 'url' server added
#
version=1.1
function usage() {
cat <<EOF
USAGE:
$0 server path-or-file
'server' is a scheme + fully qualified host, eg https://example.com
(or known abbrevs: local, or an alias you add below),
or 'url' if the file contains full URLs.
'path-or-file' is a single server path, or name of a file containing a list, one per line
(blank and # comment lines will be ignore)
Each path will be tested against server, with output to <filename>-yyyymmdd.hhmm-out.txt
for later comparison
EOF
exit 1
}
# override to change rate of testing
: ${SLEEP=1}
server="$1"
if [ "$server" == "" ]; then
usage
fi
shift
url_or_file="$1"
if [ "$url_or_file" == "" ]; then
usage
fi
set -u
set -e
# Without a timeout, curl could hang indefinitely.
# These values should be generously sufficient.
connect_timeout=2
max_time=15
COMMAND="curl -si --user-agent test-url/1.0 --connect-timeout $connect_timeout --max-time $max_time"
function test_url() {
local server=$1
local path=$2
local url="$server$path"
#local pstatus
#declare -a pstatus
set +e
$COMMAND "$url" | { head -30; cat >/dev/null; } | egrep --text '^HTTP/1|^Location:|<title>'
# set | grep PIPEST >> /tmp/tt.debug
local status=${PIPESTATUS[0]}
#local hstatus=n/a # ${PIPESTATUS[1]}
# local gstatus=${PIPESTATUS[2]}
case $status in
23) :;; # can't write all to 'head' .. s'okay.
0) : success ;; #
6)
echo "#! unable to resolve host $server";
echo "#! command: $COMMAND \"$url\"";
sleep 20;;
7)
echo "#! FAILED TO CONNECT after $connect_timeout seconds"; sleep 1;;
28)
echo "#! TIMEOUT - no output in max_time=$max_time seconds.";
sleep 2;;
51)
echo "#! SSL certificate was not OK."; sleep 1;;
*)
echo "#! Unhandled status (see curl), please investigate - status $status "
echo "#! command: $COMMAND \"$url\"";
sleep 2;;
esac
set -e
}
function test_1() {
local server=$1
local path=$2
test_url "$server" "$path"
# temp output, nothing special
# show summary
}
## helper??
# watch <file> then <action>
# watch foo.twig then test-url
#
function test_file() {
local server=$1
local path=$2
local output="$3"
# { echo "# Path: $path"; test_url "$server" "$path"; echo; }
{ echo "# Path: $path"; test_url "$server" "$path"; echo; } | tee -a "$3"
}
# fix up server name
case "$server" in
local)
# alias for local testing; assumes :80 but you could change
server=localhost;;
short1)
# for convenience you could define shortname(s) here
server='https://your-long-domain-name.org';;
url*)
# full URLs are listed, not paths
server=URL
esac
function timestamp () { date +%Y%m%d.%H%M; }
function header() {
local server="$1"
local inname="$2"
local tstamp="$3"
cat <<EOF
## v$version test-url
## $tstamp server: $server input: $inname
#
EOF
}
tstamp=$(timestamp)
if [ -f "$url_or_file" ]; then
#file=$urlfile
: ${output:=$(dirname "$url_or_file")/$(basename "$url_or_file" .txt)-$tstamp-out.txt}
echo
echo "*** Output will be saved to: $output"
echo
echo
# start fresh
echo -n > "$output"
header "$server" "$url_or_file" "$tstamp" | tee -a "$output"
line=1
egrep -v '^[ ]*$|^#' "$url_or_file" \
| while read path; do
echo "## $line."
if [ "$server" = URL ]; then
test_file "" "$path" "$output"
else
test_file "$server" "$path" "$output"
fi
sleep "$SLEEP"
line=$((line+1))
done
echo Summary:
echo
egrep 'HTTP/|#!' "$output" | sort | uniq -c | sort -nr
echo
echo "*** See output in: $output"
echo
ls -l "$output"
echo
else
echo just one path ..
fi
exit 0