8762617629294ec80a4368bcd5944caa27be37f3
1 #######################################################################
2 #/usr/lib/ddns/shell_get.sh
4 # Written by Eric Bishop, January 2008
5 # Distributed under the terms of the GNU General Public License (GPL) version 2.0
7 # This implements a wget-like program ("shell_get 1.0") that can handle
8 # basic http username/password authentication.
9 # It is implemented using the netcat (nc) utility.
10 # This is necessary because the default busybox wget
11 # does not include username/password functionality (it really sucks)
12 ##########################################################################
18 if [ $dec -lt 26 ]; then
20 elif [ $dec -ge 26 ] && [ $dec -lt 52 ]; then
21 hex
=$
(( ($dec-26) + 0x61))
22 elif [ $dec -ge 52 ] && [ $dec -lt 62 ]; then
23 hex
=$
(( ($dec-52) + 0x30))
24 elif [ $dec -eq 62 ]; then
36 hex_str
=$
( echo -n "$original_str" |
hexdump -v |
awk '{ for ( i = 2; i <= NF; i++ ) { h1=substr($i, 3, 2); h2=substr($i,0,2); printf("%s%s", h1, h2); }}' |
awk ' { $0~gsub(/00$/, "") };{ i=1; while(i <= length($0) ){ block= substr($0, i, 3); printf("%s ", block); i=i+3; }}' |
awk ' {$0~gsub(/ $/, "")}; { print $0 }' )
38 length
=$
(echo $hex_str |
awk '{$0~gsub(/ /, "")}; { print length($0) }')
39 remainder
=$
(($length % 3 ))
40 if [ $remainder -eq 1 ]; then
42 elif [ $remainder -eq 2 ]; then
50 for hex_block
in $hex_str
52 char1
=$
(to_ascii $
((0x
$hex_block / 64)))
53 char2
=$
(to_ascii $
((0x
$hex_block % 64)))
54 base_64_str
=$
(printf "$base_64_str\x$char1\x$char2")
58 if [ $remainder -eq 1 ]; then
59 base_64_str
=$
(echo "$base_64_str" |
awk '{ $0~gsub(/A$/, "=");} { print $0 }' )
60 elif [ $remainder -eq 2 ]; then
61 base_64_str
=$
(echo "$base_64_str==")
72 protocol_str
=$
(echo $full_url |
awk ' BEGIN {FS="://"} { if($0~/:\/\//)print $1 }')
73 if [ "$protocol_str" != "http" ] && [ -n "$protocol_str" ] ; then
74 echo "protocol = $protocol_str"
75 echo "Error, unsupported Protocol"
76 echo "Only http connections are supported at this time"
81 if [ -n "$protocol_str" ] ; then
82 full_url
=$
(echo $full_url |
awk ' {$0~gsub(/http:\/\//, "")}; {print $0}')
87 user_pass
=$
(echo $full_url |
awk ' BEGIN {FS="@"}; { if( $0~/@/ && $1~/^[^\?\/]+:[^\?\/]+$/ ) print $1 }')
89 if [ -n "$user_pass" ]; then
90 host_and_args
=$
(echo $full_url |
awk ' $0~gsub(/^[^@]+@/, "") {print $0}')
92 host_and_args
="$full_url"
95 host_name
=$
(echo $host_and_args |
awk ' BEGIN {FS="[:?/]"}; {print $1};')
96 port_num
=$
(echo $host_and_args |
awk ' BEGIN {FS="[?/]"}; { if($1~/:/){$1~gsub(/.*:/, ""); print $1;}else {print "80"}};')
98 path
=$
(echo $host_and_args |
awk ' { $0~gsub(/^[^\?\/]+/, "")}; {print $0};')
99 path_start_test
=$
(echo "$path" |
grep "^/")
100 if [ -z "$path_start_test" ]; then
105 #echo "full_url=\"$full_url\""
106 #echo "user_pass=\"$user_pass\""
107 #echo "host_name=\"$host_name\""
108 #echo "port_num=\"$port_num\""
109 #echo "path=\"$path\""
113 if [ -n "$user_pass" ]; then
114 auth_str
=$
(encode_base64
"$user_pass" )
115 #echo -e "GET $path HTTP/1.0\nHost: $host_name\nAuthorization: Basic $auth_str\nUser-Agent: shell_get 1.0\n\n"
116 retrieved_data
=$
(echo -e "GET $path HTTP/1.0\nHost: $host_name\nAuthorization: Basic $auth_str\nUser-Agent: shell_get 1.0\n\n" | nc
"$host_name" $port_num |
cat)
119 #echo -e "GET $path HTTP/1.0\nHost: $host_name\nUser-Agent: shell_get 1.0\n\n"
120 retrieved_data
=$
(echo -e "GET $path HTTP/1.0\nHost: $host_name\nUser-Agent: shell_get 1.0\n\n" | nc
"$host_name" $port_num |
cat)
123 echo -e "$retrieved_data"