I'm trying to fetch a page from https://termbin.com/9hc2k
using bash redirections and socat
, especially using special file /dev/tcp/localhost/8080
to open a network connection.
# fetch.sh
# fetch uploaded text from termbin.com
url=$1
check if url is provided as argument
if [[ $# -lt 1 ]]; then
echo "error: provide url" >&2
echo >&2
echo "Usage: fetch.sh [url]" >&2
exit 1
fi
n1=$'\r\n'
request=$(cat <<END
GET /${url}/ HTTP/1.1
Host: termbin.com
Connection: close
$n1
$n1
END
)
#echo "$request"
socat TCP-LISTEN:8080,fork,reuseaddr ssl:termbin.com:443,verify=0 &
socat_pid=$!
exec 3<>/dev/tcp/localhost/8080
echo "$request" >&3
cat <&3
exec 3>&-
If I try to fetch https://termbin.com/9hc2k
:
$ ./fetch.sh 9hc2k
./fetch.sh: connect: Connection refused
./fetch.sh: line 26: /dev/tcp/localhost/8080: Connection refused
./fetch.sh: line 27: 3: Bad file descriptor
./fetch.sh: line 28: 3: Bad file descriptor
First time I get Connection refused error.
If I try again however,
$ ./fetch.sh 9hc2k
2022/07/30 12:29:18 socat[497218] E bind(5, {AF=2 0.0.0.0:8080}, 16): Address already in use
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 30 Jul 2022 02:29:20 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 6
Last-Modified: Sat, 30 Jul 2022 01:51:42 GMT
Connection: close
ETag: "62e48eae-6"
Accept-Ranges: bytes
hello
As you can see the second time of trying it works. I have fetched text hello
from the link.
I don't know why it fails the first time and then second time (and subsequent) around it works. Can you tell me why this is so and what is the most rational fix? Thanks.