Dave's Brain

Browse - Computer Tips - Can I use pure bash to download a webpage?

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)

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.

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.