ตัวอย่างและการใช้งาน wget

จาก Research 2549, สารานุกรมฟรี


เรื่อง : ตัวอย่างและการใช้งาน wget


ผู้จัดทำ: นนร. เอก ศรีษา เลขที่ 3 ตอนเทคโนโลยีสารสนเทศ

นนร. เอก   ศรีษา
Enlarge
นนร. เอก ศรีษา

สารบัญ

บทคัดย่อ

การใช้งานอินเตอร์เนตในปัจจุบันเป็นที่แพร่หลายไปทั่วโลกทั้งทางฝั่งWindowsหรือ linux ไม่ว่าจะเป็นเรื่องการศึกษา
หรือบันเทิง ก็จะต้องมีการดาวน์โหลดไฟล์ต่างๆมาใช้ในเครื่องเราและถ้าหากไฟล์มีขนาดใหญ่มากก็ต้องใช้โปรแกรมช่วย
Wget เป็นโปรแกรมช่วยผู้ใช้ในการดาวน์โหลดไฟล์หรือ Source code มาใช้กับเครื่องเราให้ประหยัดเวลาในการใช้งานและ
ไม่หยุดชะงักในระหว่างการดาวน์โหลด

บทนำ

GNU Wget เป็นโปรแกรมฟรีทำหน้าที่ดาวน์โหลดข้อมูลให้ผู้ใช้เทียบได้กับโปรแกรม
Download Accelerator, Flash Get ฯลฯ แต่wget นี้มีขนาดที่เล็ก เรียกใช้ได้โดย Terminal
และ Command line (wget ใช้ได้ทั้ง windows และ Linux แต่ในที่นี้เราจะมาพูดถึง wget ใน Linux)
ทั้งนี้ความสามารถพิเศษของ wget นั้นคือหากการติดต่อหยุดชะงักลง แต่การดาวน์โหลดยังไม่สิ้นสุด การดาวน์โหลด
ก็จะยังไม่จบและเมื่อมีการเชื่อมต่อก็จะทำการดาวฯโหลดต่อไปทันที

ประวัติความเป็นมา/ทฤษฏีพื้นฐานที่เกี่ยวข้อง

GNU Wget is a free software package for retrieving files using HTTP, HTTPS and FTP, the most widely-used Internet protocols. It is a non-interactive commandline tool, so it may easily be called from scripts, cron jobs, terminals without X-Windows support, etc.
GNU Wget has many features to make retrieving large files or mirroring entire web or FTP sites easy, including:

   * Can resume aborted downloads, using REST and RANGE
   * Can use filename wild cards and recursively mirror directories
   * NLS-based message files for many different languages
   * Optionally converts absolute links in downloaded documents to relative, so that downloaded documents may link to each other locally
   * Runs on most UNIX-like operating systems as well as Microsoft Windows
   * Supports HTTP and SOCKS proxies
   * Supports HTTP cookies
   * Supports persistent HTTP connections
   * Unattended / background operation
   * Uses local file timestamps to determine whether documents need to be re-downloaded when mirroring
   * GNU Wget is distributed under the GNU General Public License.

การใช้งาน

การติดตั้งโปรแกรม Wget

โดยปกติแล้วเมื่อเราติดตั้งระบบปฏิบัติการ Linux จะมีโปรแกรม Wget มาให้เลย
แต่สำหรับเครื่องที่ Linux ไม่มี Packet wget ติดมากับการลงระบบปฏิบัติการเรา
สามารถทำการติดตั้งใหม่ได้โดย ทำตามวิธีการนี้
- รันคำสั่งด้านล่างนี้

./configure --prefix=/usr --sysconfdir=/etc &&
make
make install
" ต้องเป็น root จึงจะสามารถรันคำสั่งได้ "

- หากรันคำสั่ง make ไม่ได้ให้ติดตั้ง make ด้วยคำสั่ง

apt-get install make


คำสั่ง: wget [OPTION]... [URL]...

Startup:
-V, --version           แสดง Version ของ Wget แล้วออก.
-h, --help แสดง help ของ Wget.
-b, --background go to background after startup.
-e, --execute=COMMAND execute คำสั่ง `.wgetrc'-style

Logging and input file:

-o,  --output-file=FILE    บันทึก log messages ไปสู่ FILE 
-a, --append-output=FILE เพิ่ม messages ไปสู่ FILE.
-d, --debug แสดงข้อบกพร่อง.
-q, --quiet ปิด (no output).
-v, --verbose be verbose (default).
-nv, --no-verbose ปิด verboseness, โดยไม่ปิดโปรแกรมลง.
-i, --input-file=FILE ดาวน์โหลด URLs ที่อยู่ใน FILE.
-F, --force-html จัดการ input file ให้อยู่ในรูป HTML.
-B, --base=URL ตัวหน้าของ URL ที่มีความสัมพันธ์กับ links ในไฟล์ -F -i

Download:

  • -t
--tries=NUMBER ---- แก้ข้อมูลที่ได้รับให้เป็นตัวเลข (0 unlimits)
--retry-connrefused ---- ทำการดาวโหลดใหม่เมื่อการเชื่อมต่อมีปัญหา.
  • -O , --output-document=FILE ---- write เอกสาร ไปสู่ FILE.
  • -nc , --no-clobber ---- การข้ามการดาวน์โหลด หากคิดว่าเอกสารที่จะดาวน์โหลดมีอยู่แล้ว.
  • -c ,
--continue ---- เริ่มการดาวฯ์โหลดเอกสารใหม่บางส่วน.
--progress=TYPE ---- เลือก ตัวแสดง progress ของการดาวน์โหลด.
  • -N, --timestamping ไม่ดาวน์โหลดเอกสารใหม่จนกว่าจะมีตัวที่ใหม่กว่าปัจจุบัน.
  • -S, --server-response แสดงสถานะของ server.
    --spider ไม่ทำการดาวน์โหลดอะไร.
  • -T
--timeout=SECONDS         ตังค่าเวลาให้อยู่ในหน่วยวินาที.
--dns-timeout=SECS ตั้งค่า timeout ของ DNS lookup ให้อยู่ในรูปวินาที
--connect-timeout=SECS ตั้งค่า timeout การเชื่อมต่อให้อยู่ในรูปวินาที.
--read-timeout=SECS ตั้งค่า read timeout ให้อยู่ในรูปวินาที.
  • -w
--wait=SECONDS            ตั้งการรอเชื่อมต่อใหม่ภายใน SECONDS.
--waitretry=SECONDS รอ SECONDS ในระหว่าการย้อนกลับไปค้นหาใหม่.
--random-wait random ว่าจะเชื่อมต่อใหม่ภายในกี่วินาที.
  • -Y
--proxy                   เปิด proxy.
--no-proxy ปิด.
  • -Q
--quota=NUMBER กำหนดโควต้าของการดาวน์โหลด.
--bind-address=ADDRESS ซ่อนที่อยู่.
--limit-rate=RATE ตั้ง Limit การดาวน์โหลด.
--no-dns-cache ไม่ใช้ DNS lookups.
--restrict-file-names=OS จำกัด งานใน file ดังกล่าว.
--inet4-only เชื่อมต่อ IPv4 เท่านั้น.
  • -6
--inet6-only เชื่อมต่อ IPv6 เท่านั้น.
--prefer-family=FAMILY connect first to addresses of specified family,one of IPv6, IPv4, or none.
--user=USER ตั้งค่าผู้ใช้ของ ftp และ http เป็น USER .
--password=PASS ตั้งค่ารหัสผ่านของทั้ง ftp และ http เป็น PASS.

Directories:

  • -nd, --no-directories ---- ไม่มีการสร้างไดเร็คทอรี่ใหม่.
  • -x, --force-directories ---- ให้มีการสร้างไดเรคทอรี่.
  • -nH,
--no-host-directories ---- ไม่ให้มีการสร้าง host directories.
--protocol-directories ---- ใช้ชื่อโปรโทคอลในไดเรคทอรี่ .
  • -P,
--directory-prefix=PREFIX ---- บันทึกไฟล์ลงใน PREFIX/...
--cut-dirs=NUMBER ---- ยอมรับให้ NUMBER remote มายังไดเรคทอรี่.

HTTP options:

--http-user=USER ตั้งค่าผู้ใช้ http เป็น USER.
--http-password=PASS ตั้งค่ารหัสผ่านของ http ให้เป็น pass.
--no-cache ไม่ยอมรับให้ server ดักจับข้อมูล.

  • -E,
--html-extension        บันทึก เอกสาร html เป็น `.html'.
--ignore-length ยอมให้ `Content-Length' เป็นหัวของไฟล์.
--header=STRING ใส่ STRING ใน header.
--proxy-user=USER ตั้งค่าให้ USER เป็น proxy user.
--proxy-password=PASS ตั้งค่าให้รหัสผ่านเป็น proxy password.
--referer=URL include `Referer: URL' header in HTTP request.
--save-headers บันทึก HTTP header สู่ ไฟล์.
  • -U,
--user-agent=AGENT      identify as AGENT instead of Wget/VERSION.
--no-http-keep-alive ยกเลิก HTTP keep-alive (persistent connections).
--no-cookies ไม่ใช้คุกกี้.
--load-cookies=FILE load cookies จากไฟล์.
--save-cookies=FILE save cookies ลงไฟล์.
--keep-session-cookies load and save cookies.
--post-data=STRING send STRING as the data.
--post-file=FILE send contents of FILE.

HTTPS (SSL/TLS) options:

--secure-protocol=PR     เลือกโปรโตคอลที่มีความปลอดภัย, one of auto, SSLv2,SSLv3, and TLSv1.
--no-check-certificate ไม่ทำการตรวจสอบเว็บปลายทาง.
--certificate=FILE client certificate file.
--certificate-type=TYPE client certificate type, PEM or DER.
--private-key=FILE กำหนด private key.
--private-key-type=TYPE กำหนด private key คือ PEM or DER.
--ca-certificate=FILE file with the bundle of CA's.
--ca-directory=DIR directory where hash list of CA's is stored.
--random-file=FILE file with random data for seeding the SSL PRNG.
--egd-file=FILE file naming the EGD socket with random data.

FTP options:

      --ftp-user=USER         ตั้งค่า ผู้ใช้งานของftpเป็น USER
--ftp-password=PASS ตั้งค่ารหัสผ่านของ ftp เป็น PASS
--no-remove-listing ไม่ทำการลบไฟล์ `.listing'
--no-glob ปิดการใช้ globbing.
--no-passive-ftp ยกเลิกการส่งแบบ "passive"
--retr-symlinks when recursing, get linked-to files (not dir).
--preserve-permissions กำหนดสิทธิ

Recursive download:

  • -r, --recursive specify recursive download.
  • -l,
--level=NUMBER       maximum recursion depth (inf or 0 for infinite).
--delete-after delete files locally after downloading them.
  • -k, --convert-links make links in downloaded HTML point to local files.
  • -K, --backup-converted before converting file X, back up as X.orig.
  • -m, --mirror short-cut for -N -r -l inf --no-remove-listing.
  • -p,
--page-requisites    get all images, etc. needed to display HTML page.
--strict-comments turn on strict (SGML) handling of HTML comments.

Recursive accept/reject:

 -A,  --accept=LIST               comma-separated list of accepted extensions.
 -R,  --reject=LIST               comma-separated list of rejected extensions.
 -D,  
      --domains=LIST              comma-separated list of accepted domains.
      --exclude-domains=LIST      comma-separated list of rejected domains.
      --follow-ftp                follow FTP links from HTML documents.
      --follow-tags=LIST          comma-separated list of followed HTML tags.
      --ignore-tags=LIST          comma-separated list of ignored HTML tags.
 -H,  --span-hosts                go to foreign hosts when recursive.
 -L,  --relative                  follow relative links only.
 -I,  --include-directories=LIST  list of allowed directories.
 -X,  --exclude-directories=LIST  list of excluded directories.
 -np, --no-parent                 don't ascend to the parent directory.

การใช้ wget ผ่าน proxy

สำหรับเครื่องที่อยู่ใน Proxy Server แล้วใช้คำสั่ง wget ไม่ได้ก็
ให้ทำการตั้งค่าให้ยอมรับ Proxy ก่อน โดย

  • แก้ไขไฟล์ /etc/wgetrc โดยใช้ Editer เช่น vi /etc/wgetrc
  • ใส่ข้อมูลของ proxy เช่น
HTTP_PROXY=10.134.0.23:8080
FTP_PROXY=10.134.0.23:8080

ตัวอย่าง

การดาวน์โหลดโดยใช้ โปรแกรม wget โดยจะยกตัวอย่าง
การดาวน์โหลด โปรแกรม Snort เวอร์ชัน 2.6.1
ภาพ:wget1.jpg
หลังจากพิมคำสั่งเรียบร้อยก็จะเริ่มทำการดาวน์โหลดโดยมี สถานะว่าดาวน์โหลดไปถึงขั้นใหนแล้ว
ภาพ:wget2.jpg
เมื่อครบ 100% ก็จะกลับเข้าสู้หน้าจอปกติ
ภาพ:wget3.jpg
file ที่ดาวน์โหลดมาก็จะถูกบันทึกที่จุดที่เราได้ดาวน์โหลดมาเก็บไว้
ภาพ:wget4.jpg

สรุป

เอกสารอ้างอิง และเว็บลิงค์

การ Install wget
wget ผ่าน ProxyServer
wget --help, man wget

Personal tools