<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SpaLinux.com &#187; wget</title>
	<atom:link href="http://spalinux.com/tag/wget/feed" rel="self" type="application/rss+xml" />
	<link>http://spalinux.com</link>
	<description>Resources for Relaxing Linux System Administrators</description>
	<lastBuildDate>Sat, 21 Jan 2012 16:07:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>วิธีการใช้คำสั่ง wget</title>
		<link>http://spalinux.com/2010/01/using_wget</link>
		<comments>http://spalinux.com/2010/01/using_wget#comments</comments>
		<pubDate>Fri, 22 Jan 2010 08:04:48 +0000</pubDate>
		<dc:creator>editor</dc:creator>
				<category><![CDATA[Console]]></category>
		<category><![CDATA[Proxy]]></category>
		<category><![CDATA[Web Server]]></category>
		<category><![CDATA[wget]]></category>

		<guid isPermaLink="false">http://spalinux.com/?p=1047</guid>
		<description><![CDATA[wget เป็นโปรแกรมที่ใช้เพื่อดาวน์โหลดไฟล์จากเว็บไซต์ได้ ในรูปแบบ text console วิธีการใช้งานก็ง่ายคือรันคำสั่ง wget ตามด้วย URL ที่ต้องการดาวน์โหลด ผลลัพธ์ที่ได้จะถูกบันทึกเป็นไฟล์ของเว็บเพจหน้านั้นๆ รูปแบบการใช้งาน $ wget [option]... [URL]... ตัวอย่างเช่น ต้องการดาวน์โหลดเว็บเพจหน้าแรกของ www.google.co.th $ wget 'http://www.google.co.th' --13:52:23--  http://www.google.co.th/            =&#62; `index.html' Resolving www.google.co.th... 72.14.203.147, 72.14.203.103, 72.14.203.104, ... Connecting to www.google.co.th&#124;72.14.203.147&#124;:80... connected. HTTP request sent, awaiting response... 200 OK Length: unspecified [text/html]     [  &#60;=&#62;                                                                                ] 6,726         11.96K/s 13:52:24 (11.96 [...]]]></description>
			<content:encoded><![CDATA[<p>wget เป็นโปรแกรมที่ใช้เพื่อดาวน์โหลดไฟล์จากเว็บไซต์ได้ ในรูปแบบ text console วิธีการใช้งานก็ง่ายคือรันคำสั่ง wget ตามด้วย URL ที่ต้องการดาวน์โหลด ผลลัพธ์ที่ได้จะถูกบันทึกเป็นไฟล์ของเว็บเพจหน้านั้นๆ</p>
<p><span id="more-1047"></span></p>
<h4>รูปแบบการใช้งาน</h4>
<pre>$ <strong>wget</strong> [option]... [URL]...</pre>
<p>ตัวอย่างเช่น ต้องการดาวน์โหลดเว็บเพจหน้าแรกของ www.google.co.th</p>
<pre>$ <strong>wget 'http://www.google.co.th'
</strong>--13:52:23--  http://www.google.co.th/
           =&gt; `index.html'
Resolving www.google.co.th... 72.14.203.147, 72.14.203.103, 72.14.203.104, ...
Connecting to www.google.co.th|72.14.203.147|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]</pre>
<pre>    [  &lt;=&gt;                                                                                ] 6,726         11.96K/s</pre>
<pre>13:52:24 (11.96 KB/s) - `<span style="color: #0000ff;">index.html</span>' saved [6726]</pre>
<p>จากตัวอย่าง ผลลัพธ์ที่ได้จะถูกบันทึกเป็นชื่อ index.html</p>
<p>หมายเหตุ แนะนำให้ใส่เครื่องหมายคำพูด &#8216;  คลอบ URL เพราะบางตัวอักษรอาจมีความหมายใน SHELL</p>
<pre>$ <strong>cat index.html
</strong>&lt;!doctype html&gt;&lt;html&gt;&lt;head&gt;&lt;meta http-equiv="content-type" content="text/html; charset=windows-874"&gt;&lt;title&gt;Google&lt;/title&gt;
...</pre>
<p>ระบุออปชั่น &#8216;&#8211;help&#8217; เพื่อดูวิธีการใช้งาน พร้อมทั้งออปชั่นต่างๆ</p>
<pre>$ <strong>wget --help
</strong>GNU Wget 1.10.2 (Red Hat modified), a non-interactive network retriever.
Usage: wget [OPTION]... [URL]...</pre>
<pre>Mandatory arguments to long options are mandatory for short options too.</pre>
<pre>Startup:
  -V,  --version           display the version of Wget and exit.
  -h,  --help              print this help.
  -b,  --background        go to background after startup.
  -e,  --execute=COMMAND   execute a `.wgetrc'-style command.</pre>
<p>ในบทความนี้ขอแนะนำการใช้ออปชั่นที่ผู้เขียนใช้บ่อยๆ</p>
<h4>ดูรายละเอียดการโหลด (debug)</h4>
<p>ระบุออปชั่น &#8216;-d&#8217; เพื่อดูรายละเอียดการดาวน์โหลด เช่นดู HTTP Header, Protocol, Cookie ที่เกิดขึ้นในการดาวน์โหลด</p>
<pre>$ <strong>wget -d 'http://www.google.co.th'
</strong>DEBUG output created by Wget 1.10.2 (Red Hat modified) on linux-gnu.</pre>
<pre>--14:16:24--  http://www.google.co.th/
           =&gt; `index.html'
Resolving www.google.co.th... 72.14.203.105, 72.14.203.106, 72.14.203.99, ...
Caching www.google.co.th =&gt; 72.14.203.105 72.14.203.106 72.14.203.99 72.14.203.104 72.14.203.103 72.14.203.147
Connecting to www.google.co.th|72.14.203.105|:80... connected.
Created socket 3.
Releasing 0x000000000083d390 (new refcount 1).</pre>
<pre>---request begin---
GET / HTTP/1.0
User-Agent: Wget/1.10.2 (Red Hat modified)
Accept: */*
Host: www.google.co.th
Connection: Keep-Alive</pre>
<pre>---request end---
HTTP request sent, awaiting response...
---response begin---
HTTP/1.0 200 OK
Date: Fri, 22 Jan 2010 07:16:29 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=windows-874
Set-Cookie: PREF=ID=79a6955a36392a2d:TM=1264144589:LM=1264144589:S=c99kK7cOeBwCp4KG; expires=Sun, 22-Jan-2012 07:16:29 GMT; path                                             =/;</pre>
<pre>domain=.google.co.th
Set-Cookie: NID=31=h8-gos7iGB-MXzlXOo4C1_CRqzSNE6wu5Os2WBu6C-jm7tPhrlj4kJp1aiENCM66f388IScBzce4Q-6x7rwbfxUUbp2DGAIcxXu1zwSGy6zKH                                            </pre>
<pre>cjUQ2_59hiym_wuaRt2; expires=Sat, 24-Jul-2010 07:16:29 GMT; path=/; domain=.google.co.th; HttpOnly
Server: gws
X-XSS-Protection: 0</pre>
<pre>---response end---
200 OK
cdm: 1 2 3 4 5 6 7 8
Stored cookie google.co.th -1 (ANY) / &lt;permanent&gt; &lt;insecure&gt; [expiry 2012-01-22 14:16:29] PREF ID=79a6955a36392a2d:TM=1264144589                                            </pre>
<pre>:LM=1264144589:S=c99kK7cOeBwCp4KG
cdm: 1 2 3 4 5 6 7 8
Stored cookie google.co.th -1 (ANY) / &lt;permanent&gt; &lt;insecure&gt; [expiry 2010-07-24 14:16:29] NID 31=h8-gos7iGB-MXzlXOo4C1_CRqzSNE6w                                            </pre>
<pre>u5Os2WBu6C-jm7tPhrlj4kJp1aiENCM66f388IScBzce4Q-6x7rwbfxUUbp2DGAIcxXu1zwSGy6zKHcjUQ2_59hiym_wuaRt2
Length: unspecified [text/html]</pre>
<pre>    [  &lt;=&gt;                                                                                ] 6,702          6.21K/s</pre>
<pre>Closed fd 3
14:16:31 (6.20 KB/s) - `index.html' saved [6702]</pre>
<h4>ดาวน์โหลดต่อไฟล์ (continue)</h4>
<p>สามารถดาวน์โหลดไฟล์ต่อจากที่ดาวน์โหลดยังไม่ครบได้</p>
<p>เช่นต้องการดาวน์โหลดไฟล์ iso ที่มีขนาดใหญ่มาก แล้วดาวน์โหลดครั้งแรกยังไม่ครบ</p>
<pre>$ <strong>wget 'http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso'
</strong>--14:09:51--  http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso
           =&gt; `Fedora-11-x86_64-Live.iso'
Resolving <a href="http://www.mirror.in.th/">www.mirror.in.th</a>... 61.7.253.242
Connecting to www.mirror.in.th|61.7.253.242|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 724,097,024 (691M) [application/octet-stream]</pre>
<pre> 0% [                                                                                     ] 1,090,066     87.82K/s  ETA 2:13:52</pre>
<p>หมายเหตุ ในระหว่างการดาวน์โหลด หากต้องการยกเลิกให้กด [Ctrl-C]</p>
<p>ครั้งแรกดาวน์โหลดไฟล์ได้แค่ 1 Meg กว่าๆ</p>
<pre>$ <strong>ls -l
</strong>total 1080
-rw-r--r-- 1 user1  users  1098754 2010-01-22 14:10 Fedora-11-x86_64-Live.iso</pre>
<p>ถ้าต้องการดาวน์โหลดต่อ ต้องระบุออปชั่น &#8216;-c&#8217;</p>
<pre>$ <strong>wget -c 'http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso'
</strong>--14:11:46--  http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso
           =&gt; `Fedora-11-x86_64-Live.iso'
Resolving www.mirror.in.th... 61.7.253.242
Connecting to www.mirror.in.th|61.7.253.242|:80... connected.
HTTP request sent, awaiting response... 206 <span style="color: #0000ff;">Partial Content</span>
Length: 724,097,024 (691M), 722,998,270 (690M) remaining [application/octet-stream]</pre>
<pre> 0% [                                                                                     ] 1,401,045     94.59K/s  ETA 2:04:50</pre>
<pre>$ <strong>ls -l
</strong>total 1384
-rw-r--r-- 1 user1  users  1409733 2010-01-22 14:11 Fedora-11-x86_64-Live.iso</pre>
<h4>จำกัดความเร็วการโหลด</h4>
<p>ต้องการโหลดไฟล์แต่ไม่ต้องการเบียดบังความเร็วการใช้อินเตอร์เน็ตจากคนอื่นมากเกินไป เราสามารถจำกัดความเร็วในการโหลดของเราได้ ด้วยออปชั่น &#8216;&#8211;limit-rate&#8217;</p>
<p>เช่นต้องการจำกัดไว้ที่ 10 KBytes/s</p>
<pre>$ <strong>wget --limit-rate 10000 'http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso'
</strong>--14:25:14--  http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso
           =&gt; `Fedora-11-x86_64-Live.iso'
Resolving www.mirror.in.th... 61.7.253.242
Connecting to www.mirror.in.th|61.7.253.242|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 724,097,024 (691M) [application/octet-stream]</pre>
<pre> 0% [                                                                                     ] 62,618         9.77K/s ETA 20:07:01</pre>
<p>หมายเหตุ หน่วยของออปชั่น &#8216;&#8211;limit-rate&#8217; เป็น bytes per second</p>
<h4>โหลดผ่าน proxy</h4>
<p>หากต้องใช้ proxy ในการโหลด ต้องมีการระบุตัวแปร &#8216;http_proxy&#8217; ไว้ใน shell ก่อนรันคำสั่ง เช่น IP ของ proxy คือ 192.168.1.1 และพอร์ต proxy คือ 3128 ถ้าใช้ BASH สามารถทำได้โดย</p>
<pre>$ <strong>export http_proxy=http://192.168.1.1:3128</strong></pre>
<p>ใช้คำสั่ง wget พร้อมระบุออปชั่น &#8216;&#8211;proxy&#8217; เพื่อให้โหลดผ่าน proxy</p>
<pre>$ <strong>wget --proxy 'http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso'
</strong>--2010-01-22 14:30:26--  http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso
Connecting to 192.168.1.1:3128... connected.
Proxy request sent, awaiting response... 200 OK
Length: 724097024 (691M) [application/octet-stream]
Saving to: `Fedora-11-x86_64-Live.iso'</pre>
<pre> 0% [                                                                                     ] 63,434      96.4K/s</pre>
<p>แต่ถ้า proxy ต้องระบุ user, password ด้วย จะขึ้น error ดังนี้</p>
<pre>$ <strong>wget --proxy  'http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso'
</strong>--2010-01-22 14:31:06--  http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso
Connecting to 192.168.1.1:3128... connected.
Proxy request sent, awaiting response... 407 Proxy Authentication Required
2010-01-22 14:31:06 ERROR 407: Proxy Authentication Required.</pre>
<pre>ใช้ออปชั่น '--proxy-user' และ '--proxy-password' เพื่อระบุ user, password ในการใช้ proxy</pre>
<pre>$ <strong>wget --proxy-user=prxuser --proxy-password=prx1234  'http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso'
</strong>--2010-01-22 14:30:26--  http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso
Connecting to 192.168.1.1:3128... connected.
Proxy request sent, awaiting response... 200 OK
Length: 724097024 (691M) [application/octet-stream]
Saving to: `Fedora-11-x86_64-Live.iso'</pre>
<pre> 0% [                                                                                     ] 63,434      96.4K/s</pre>
<h4>ไฟล์คอนฟิก .wgetrc</h4>
<p>แทนที่จะต้องระบุออปชั่น proxy ทุกครั้ง สามารถสร้างเป็นไฟล์คอนฟิกสำหรับใช้คำสั่ง wget โดยสร้างไฟล์ชื่อ .wgetrc (มีเครื่องหมายจุดนำหน้าชื่อไฟล์ด้วย) ไว้ใน HOME ของผู้ใช้งาน</p>
<pre>$ <strong>cat ~/.wgetrc
</strong>http_proxy=http://192.168.1.1:3128/
proxy_user=prxuser
proxy_password=prx1234</pre>
<p>หลังจากสร้างไฟล์คอนฟิก .wgetrc แล้ว เวลาใช้คำสั่ง wget ก็ไม่ต้องระบุออปชั่นใดๆ อีก</p>
<pre>$ <strong>wget  'http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso'
</strong>--2010-01-22 14:37:46--  http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso
Connecting to 192.168.1.1:3128... connected.
Proxy request sent, awaiting response... 200 OK
Length: 724097024 (691M) [application/octet-stream]
Saving to: `Fedora-11-x86_64-Live.iso.1'</pre>
<pre> 0% [                                                                                     ] 80,810      95.8K/s</pre>
<h4>ข้อมูลอ้างอิง</h4>
<ul>
<li><a href="http://www.gnu.org/software/wget/" target="_blank">GNU Wget</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spalinux.com/2010/01/using_wget/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

