Skip to content

Instantly share code, notes, and snippets.

@perfectmak
Created September 30, 2024 18:26
Show Gist options
  • Save perfectmak/23d65b74920ebd1a7e9a19fb4652aa0e to your computer and use it in GitHub Desktop.
Save perfectmak/23d65b74920ebd1a7e9a19fb4652aa0e to your computer and use it in GitHub Desktop.
Making an http request in bash (without CURL or Wget)
#!/bin/bash
#Sample usage: HOST=google.com ./tcp_request.sh
# Open a file descriptor 3 for input and output to tcp host and port 80
exec 3<>/dev/tcp/$HOST/80
lines=(
'GET /push_archive HTTP/1.1'
"Host: $HOST"
'Connection: close'
''
)
# Write each HTTP header line to socket
printf '%s\r\n' "${lines[@]}" >&3
# Read all output to stdout
while read -r data <&3; do
echo "got server data: $data"
done
# Close the file descriptor 3
exec 3>&-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment