#!/usr/bin/perl # # This script is a command line program for fetching web pages. # It takes URLs as input and then connects to the specified web # server and retrieves the specified page using an HTTP/1.0 GET # request. It does not parse the resulting web page, and does # not retrieve any associated images, included files, or source # files for embedded frames. # # A single URL can be given on the command line, or the program # will read URLs from stdin, one URL per line. # # # Andy Welter # www.the-welters.com # 9/1/1999 # use Socket; if ( "$ARGV[0]" ne "" ) { $url="$ARGV[0]"; } else { $interactive=1; chop ($url = ); }; # # quiet means don't print prompts and diagnostics # $quiet=1; # # Read std in and write it to the server # Open a socket for each command # printf ("url: ") unless $quiet; while ( $url ) { $url=~s.http://..; ($host,$filename)=split /\//, $url, 2; ($host,$port)=split /:/,$host; if ($port eq "") { $port=80; }; $filename="/$filename"; printf ("set up socket to $host:$port\n") unless $quiet; $sockaddr = 'S n a4 x8'; ($name, $aliases, $proto) = getprotobyname('tcp'); ($name, $aliases, $type, $len, $thisaddr) = gethostbyname($host); $thisport = pack($sockaddr, &AF_INET, 0, $thisaddr); $thatport = pack($sockaddr, &AF_INET, $port, $thisaddr); printf ("Opening socket...\n") unless $quiet; socket(S, &PF_INET, &SOCK_STREAM, $proto) || die "cannot create socket\n"; connect(S,$thatport) || die "cannot connect socket\n"; # Set socket to write after each print select(S); $| = 1; select(STDOUT); # # Send command # printf ("Sending $filename") unless $quiet; print S "GET $filename HTTP/1.0\n\n"; # # Read responses from server # while ( $_ = ) { printf ("$_") unless $opt_n; }; close(S); if ($interactive) { printf ("url: ") unless $quiet; chop ($url=); } else { $url=""; }; }; exit 0;