Created
June 28, 2015 22:24
-
-
Save 0x5742/24377284e9090b24fdc1 to your computer and use it in GitHub Desktop.
a wget-ish thing with bash sockets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| die() { | |
| echo "usage: $0 [http://]somehost[:port][/blah/blah]" >&2 | |
| echo " or $0 somehost [port] /blah/blah" >&2 | |
| exit 1 | |
| } | |
| case "$#" in | |
| 1) | |
| set -- `echo "$1" | sed -n s/\'/\'\\\'\'/g'; | |
| s=^\(http://\)\?\([a-zA-Z0-9.-]\+\)\(:\([0-9]*\)\)\?\(/.*\)\?$=\2 \5 \4=p | |
| '` | |
| test "$1" || { | |
| echo "$0: can't parse url" >&1 | |
| die | |
| } | |
| ;; | |
| 2) | |
| set -- "$1" "$2" 80 | |
| ;; | |
| 3) | |
| set -- "$1" "$3" "$2" | |
| ;; | |
| *) | |
| die | |
| ;; | |
| esac | |
| test "$1" && host=$1 || die | |
| test "$2" && path=$2 || path=/ | |
| test "$3" && port=$3 || port=80 | |
| exec 3<> /dev/tcp/$host/$port 0<&3 | |
| ( | |
| echo -ne "GET $path HTTP/1.1\015\012" | |
| echo -ne "Host: $host\015\012" | |
| echo -ne "User-Agent: Mozilla/4.0 (compatible; bash/$BASH_VERSION)\015\012" | |
| echo -ne "Connection: close\015\012" | |
| echo -ne "\015\012" | |
| ) >&3 | |
| exec cat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment