-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlf_curl.sh
More file actions
executable file
·168 lines (156 loc) · 4 KB
/
lf_curl.sh
File metadata and controls
executable file
·168 lines (156 loc) · 4 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
#!/bin/bash
# set -veux
if [ -f /home/lanforge/lanforge.profile ]; then
. /home/lanforge/lanforge.profile
else
echo "/home/lanforge/lanforge.profile not found, bye."
exit 1
fi
CURL=`which curl`
IP=`which ip`
#echo $CURL;
[ -f lib_vrf.bash ] && . ./lib_vrf.bash
SOURCE_IP=""
SOURCE_PORT=""
DEST_HOST=""
OUT_FILE=/dev/null
NUM_LOOPS=1
UPFILE=""
PROGRESS=""
help="$0 options:
-d {destination_url}
-f {file to upload}
-h # this help
-i {source ip}
-n {number of times, 0 = infinite}
-o {output file prefix, /dev/null is default}
-p {source port, eg sta0000}
-v # verbose curl option -#
E.G.:
$0 -i 10.0.0.1 -p eth1 -o /tmp/output -d http://example.com/
becomes
curl -sq. ~Lk --interface 10.0.0.1 -o /tmp/output_eth1 http://example.com/
Best if used from lf_generic_ping.pl to construct commands referencing this script:
./lf_generic_ping.pl --mgr cholla-f19 -r 2 -n curl_ex_ --match 'eth2#' --cmd 'lf_curl.sh -o /tmp/curl_%p.out -i %i -d %d -p %p' --dest http://localhost/
"
LFCURL=''
while getopts ":d:f:vhi:n:o:p:" OPT ; do
#echo "OPT[$OPT] OPTARG[$OPTARG]"
case $OPT in
h)
echo $help
exit 0
;;
d)
DEST_HOST="$OPTARG"
;;
f)
if [[ -z "${OPTARG:-}" ]]; then
echo "Upload file (-f) value missing! Format as -f 'parameter=@<filename>'"
exit 1
fi
UPFILE="$OPTARG"
my_upload_file="${UPFILE##*[=@]}";
if [[ "$UPFILE" == "$my_upload_file" ]]; then
echo "Please specify upload filename as 'parameter=@/home/lanforge/filename.txt'"
exit 1
fi
if [[ ! -r "$my_upload_file" ]]; then
echo "File [$my_upload_file] not found or readable, exiting."
exit 1
fi
if [[ $UPFILE != @* ]]; then
UPFILE="@$UPFILE"
fi
UPFILE="-d $UPFILE"
;;
i)
PORT_IP="$OPTARG"
if [[ $CURL = ~/local/bin/curl ]] || [[ $CURL = /home/lanforge/local/bin/curl ]]; then
LFCURL=1
fi
;;
n)
NUM_LOOPS=$OPTARG
;;
o)
OUT_FILE="$OPTARG"
;;
p)
PORT="$OPTARG"
SOURCE_PORT="--interface $OPTARG"
;;
v)
PROGRESS='-#'
;;
*)
echo "Unknown option [$OPT] [$OPTARG]"
;;
esac
done
if [[ -z "$DEST_HOST" ]]; then
echo "$help"
exit 1
fi
if [[ x$OUT_FILE != x/dev/null ]] && [[ x$SOURCE_PORT != x ]] ; then
OUT_FILE="-o ${OUT_FILE}_${SOURCE_PORT}"
elif [[ $OUT_FILE = /dev/null ]]; then
OUT_FILE="-o ${OUT_FILE}"
fi
VRF=''
NUM_GOOD=0
LB='#'
L_SOURCE_PORT="$PORT"
if [[ $PORT = *$LB* ]] && [[ $PORT != *@* ]]; then
L_SOURCE_PORT="${PORT}@${PORT//#*/}"
fi
if [[ ${#IFNAMES[@]} -lt 1 ]]; then
[[ x$PROGRESS != x ]] && echo "NO VRF PORTS: ${#IFNAMES[@]}"
else
[[ x$PROGRESS != x ]] && echo "SOME VRF PORTS: ${#IFNAMES[@]}"
if [[ x${IFNAMES[$L_SOURCE_PORT]} = x ]]; then
[[ x$PROGRESS != x ]] && echo "No vrf port detected for $L_SOURCE_PORT"
else
[[ x$PROGRESS != x ]] && echo "VRF port: ${IFNAMES[$L_SOURCE_PORT]}"
VRF=1
fi
fi
if [[ $VRF = 1 ]]; then
SOURCE_IP=''
elif [[ $LFCURL = 1 ]]; then
SOURCE_IP="--dns-ipv4-addr $OPTARG --interface $OPTARG"
else
SOURCE_IP="--interface $OPTARG"
fi
STD_O="/tmp/lf_curl_so.$$"
if [[ x$PROGRESS = x ]]; then
VERB="-s"
STD_E="/tmp/lf_curl_se.$$"
else
VERB=""
STD_E=""
fi
CCMD="$CURL $VERB -Lk --connect-timeout 2 --max-time 10 $PROGRESS \
-D /tmp/lf_curl_h.$$ $OUT_FILE $SOURCE_IP $UPFILE $DEST_HOST"
if [[ x$VRF != x ]]; then
CCMD="$IP vrf exec ${IFNAMES[$L_SOURCE_PORT]} $CCMD"
fi
for N in `seq 1 $NUM_LOOPS`; do
if [[ x$PROGRESS = x ]]; then
$CCMD > $STD_O &> $STD_E
else
echo "Running $CCMD"
$CCMD
fi
if [[ $? > 0 ]]; then
echo "Failed $DEST_HOST"
[ -f /tmp/lf_curl_se.$$ ] && head -1 /tmp/lf_curl_se.$$
else
NUM_GOOD=$(( $NUM_GOOD +1))
[ -f /tmp/lf_curl_so.$$ ] && head -1 /tmp/lf_curl_so.$$
[ -f /tmp/lf_curl_h.$$ ] && head -1 /tmp/lf_curl_h.$$
fi
sleep 1
done
echo "Finished $NUM_LOOPS, $NUM_GOOD successful"
#