Date: 2009oct1
OS: Linux
Language: bash
Q. Can I use pure bash to download a webpage?
A. Yes, use /dev/tcp
More on Using Bash's Built-in /dev/tcp File (TCP/IP)
http://www.linuxjournal.com/content/more-using-bashs-built-devtcp-file-tcpip
Everything is a file!
Putting together all the comments from that page we get:
#!/bin/bash
wfetch()
{
HOST=$1
FILE=$2
PORT=$3
[ -z "$PORT" ] && PORT=80
# Connect to server
exec 3<>/dev/tcp/$HOST/$PORT
# Send command to server
echo -en "GET / HTTP/1.1\r\nhost: http://$HOST/$FILE\r\nConnection: close\r\n\r\n" >&3
# Get response
cat <&3
# Close socket
exec 3>&-
}
wfetch www.google.com
This is cool but I would still recommend wget for a production script
that fetches web pages. For starters, wget follows redirects unlike this
script.
| What this info useful to you? You can donate to say thanks |
Add a comment
Sign in to add a comment