Dave's Brain

Browse - computer tips - bash get webpage with pure bash

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
Copyright © 2008-2010, 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.