Advertisement
Guest User

Untitled

a guest
Jan 28th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.92 KB | None | 0 0
  1. ~/src]$ cat get_file
  2. #!/bin/bash
  3. # By Ainsley Pereira
  4.  
  5. # This script is designed to be a primitive web/ftp client, to pull a file
  6. # from somewhere on the Internet when you have nothing but the base
  7. # LFS. Generally, it's recommended that you use it one time -- to pull
  8. # a Web or FTP client to get the rest of the stuff you need.
  9.  
  10. # If somebody doesn't know how to call the script, tell them.
  11. if [ x$1 = "x" ]
  12.         then echo "USAGE: $0 <URL>"
  13. fi
  14.  
  15. # Pick apart the URL, and break it into the pieces we need.
  16. URL=${1##http://} &&
  17. SERVER=${URL%%/*} &&
  18. FULLFILENAME=${URL#$SERVER} &&
  19. FILENAME=${FULLFILENAME##*/}
  20.  
  21. echo URL=$URL
  22. echo SERVER=$SERVER
  23. echo FULLNAME=$FULLFILENAME
  24. echo FILENAME=$FILENAME
  25.  
  26. # There are two important (and somewhat unusual pieces to this
  27. # next command (or series of commands -- it gets tricky trying to
  28. # say which it is.)
  29.  
  30. # The first is the "3<>" part. This says to open file descriptor
  31. # 3 for BOTH input and output.
  32. # The second important piece is "/dev/tcp". There is no actual entry in /dev
  33. # for this -- it's a construct of bash (actually, more properly
  34. # it's a construct of readline). It allows you to do TCP interactions
  35. # with any machine. The format is "/dev/tcp/<address>/<port>". <address> can
  36. # be either an IP address or a hostname. A hostname gets resolved just like
  37. # it does with any other TCP/IP application. Since we are using HTTP
  38. # in this case, the port will be 80.
  39.  
  40. # First, we send the HTTP command to file descriptor 3. Note the "&" on
  41. # the end. Without that, we'll never get out of the command so that we
  42. # can execute the next one.
  43. (echo -e "GET $FULLFILENAME HTTP/0.9\r\n\r\n" 1>&3 &
  44.  
  45. # Just like we rerouted stdout (file descriptor 1) to FD3 in the last
  46. # command, we reroute stdin (FD0) from FD3 in this one.
  47. # We also tell bash that we want to open FD3 for both input and output
  48. # at the same time.
  49. # Since our earlier command was written to it, what happens once FD3
  50. # is built and associated with the remote server, is that that server
  51. # gets sent "GET <path/filname> HTTP/0.9". The server very obediently
  52. # sends back that file.
  53. cat 0<&3) 3<> /dev/tcp/$SERVER/80 |\
  54. (
  55. # The "(" and ")" around all of these statements form a subshell.
  56. # One reason it is done is that it only requires one output
  57. # statement to handle all the commands in the subshell. In this
  58. # case, it has a more important purpose: it allows us to feed info
  59. # to all the commands with one INPUT. (The pipe, which we can only
  60. # have one of.
  61. # Without the subshell, the pipe would read into the first "read i",
  62. # and that's all that would ever happen.
  63.  
  64. # Prime the variable "i" so the while will work properly.
  65. read i
  66. # As long as there's HTTP text coming back from the GET request, keep
  67. # reading.
  68. while [ x"$(echo $i | tr -d '\r')" != "x" ]
  69. do
  70.         read i
  71. done
  72. # Copy whatever's coming in (from fd3) to stdout (which is about to
  73. # be redirected to $FILENAME).
  74. cat
  75. ) >$FILENAME
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement