{"id":816,"date":"2011-01-14T03:16:59","date_gmt":"2011-01-14T07:16:59","guid":{"rendered":"http:\/\/protofusion.org\/wordpress\/?p=816"},"modified":"2011-01-14T03:23:13","modified_gmt":"2011-01-14T07:23:13","slug":"downloading-wget-with-bash","status":"publish","type":"post","link":"http:\/\/protofusion.org\/wordpress\/2011\/01\/downloading-wget-with-bash\/","title":{"rendered":"Downloading wget Without wget: Use bash"},"content":{"rendered":"<p>\nThere are many ways to download and install <a href=\"http:\/\/www.gnu.org\/software\/wget\/\">wget<\/a> without having wget itself installed. For example, one can use <a href=\"http:\/\/curl.haxx.se\/\">curl<\/a>, a sort of competitor to wget, or a package manager with <span title=\"what is this lib's homepage?\">libfetch or some other library-level downloader integrated (such as <a href=\"http:\/\/archlinux.org\/pacman\/\">pacman<\/a>). One may be able to use SSH&#8217;s scp or sftp utility or even use netcat to transfer a wget tarball over a network. But these methods of obtaining wget are not always feasible or even possible whereas a <a href=\"http:\/\/gnu.org\/software\/bash\/\">bash<\/a> shell and a few core utilities are often readily available.<br \/>\n<\/span><\/p>\n<p><!--more--><\/p>\n<p>\n  I was introduced to the bash builtin <tt>\/dev\/tcp<\/tt> by <a href=\"http:\/\/forums.unrealircd.com\/viewtopic.php?f=9&amp;t=6498#p32272\" title=\"formerly known as aegis\">warg<\/a> the other day on <a href=\"irc:\/\/irc.x-tab.org\/chat\">x-tab#chat<\/a>. He explained a basic use of this device by demonstrating how to download wget&#8217;s compressed tarball. The download process itself can be done with pure bash, but some post-processing of the downloaded file must be done to remove HTTP headers. I document warg&#8217;s application of <tt>\/dev\/tcp<\/tt> here because I found the idea fascinating and want this documentation for myself ;-).\n<\/p>\n<h2>Connecting and Downloading<\/h2>\n<p>\n  To read about the <tt>\/dev\/tcp<\/tt> builtin for yourself, check out the following:\n<\/p>\n<p><code>$ info '<a href=\"http:\/\/gnu.org\/software\/bash\/manual\/bash.html#Redirections\">(bash) Redirections<\/a>'<\/code><\/p>\n<p>\n  With the <tt>exec<\/tt> line we initiate the connection, allocating a file descriptor and storing the numeic file descriptor into the HTTP_FD variable. Then, with the <tt>echo<\/tt> line, we send an HTTP request through the descriptor to the server. After sending the request, we process the server&#8217;s response with the <tt>sed<\/tt> line which skips over the HTTP headers sent by the server and stows the results into <tt>wget-latest.tar.gz<\/tt>. Note that this last command will sit around for a while. It is with this command that the builk of the data transfer is performed. And, since you&#8217;re using shell redirections to download the file, you cannot see the download progress. Instead, wait for the command to complete. This also involves waiting for the server to time out your connection since it supports pipelining. After this process is completed, the wget-latest.tar.gz file is as your disposal.\n<\/p>\n<p><code><\/p>\n<pre>\r\n$ WGET_HOSTNAME='ftp.gnu.org'\r\n$ exec {HTTP_FD}&lt;&gt;\/dev\/tcp\/${WGET_HOSTNAME}\/80\r\n$ echo -ne 'GET \/gnu\/wget\/wget-latest.tar.gz HTTP\/1.1\\r\\nHost: '\\\r\n    ${WGET_HOSTNAME}'\\r\\nUser-Agent: '\\\r\n    'bash\/'${BASH_VERSION}'\\r\\n\\r\\n'  &gt;&amp;${HTTP_FD}\r\n$ sed -e '1,\/^.$\/d' &lt;&amp;${HTTP_FD} &gt;wget-latest.tar.gz\r\n<\/pre>\n<p><\/code><\/p>\n<p>\n  Now you have a wget source tarball on your machine. As long as you have tar and a compiler on the machine, you are well on your way to downloading stuff using a self-compiled wget. In the commands above, you may replace &#8220;gz&#8221; with &#8220;bz2&#8221; or &#8220;lzma&#8221; for smaller downloads if the machine you&#8217;re using has bzip2 or xz-utils installed. And, of course, it should not be too hard to repurpose the above code to download a particular version of wget or even a completely unrelated software package.\n<\/p>\n<p>\n  Please feel free to point out problems with this approach or give pointers on porting this to other environments :-).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are many ways to download and install wget without having wget itself installed. For example, one can use curl, a sort of competitor to wget, or a package manager with libfetch or some other library-level downloader integrated (such as<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"http:\/\/protofusion.org\/wordpress\/2011\/01\/downloading-wget-with-bash\/\">Read more &#8250;<\/a><\/div>\n<p><!-- end of .read-more --><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kadence_starter_templates_imported_post":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7,10,40],"tags":[98,97,21,250,24,99,96],"class_list":["post-816","post","type-post","status-publish","format-standard","hentry","category-linux","category-quick-tips","category-technology","tag-bash","tag-download","tag-fromirc","tag-linux","tag-networking","tag-shell","tag-wget"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pNjAs-da","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/protofusion.org\/wordpress\/wp-json\/wp\/v2\/posts\/816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/protofusion.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/protofusion.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/protofusion.org\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/protofusion.org\/wordpress\/wp-json\/wp\/v2\/comments?post=816"}],"version-history":[{"count":8,"href":"http:\/\/protofusion.org\/wordpress\/wp-json\/wp\/v2\/posts\/816\/revisions"}],"predecessor-version":[{"id":821,"href":"http:\/\/protofusion.org\/wordpress\/wp-json\/wp\/v2\/posts\/816\/revisions\/821"}],"wp:attachment":[{"href":"http:\/\/protofusion.org\/wordpress\/wp-json\/wp\/v2\/media?parent=816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/protofusion.org\/wordpress\/wp-json\/wp\/v2\/categories?post=816"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/protofusion.org\/wordpress\/wp-json\/wp\/v2\/tags?post=816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}