Latest / CyberCode Academy / Course 4 - Learning Linux Shell Scripting | Episode 5: Shell Scripting for Web Automation, Data Retrieval, and Parsing
Transcript
- 0:00Speaker 1: Welcome back to the deep dive. Today, we're talking about
- 0:02Speaker 1: the web, specifically all that data sitting out there. Usually
- 0:05Speaker 1: you need a browser right clicking around, copying, pasting.
- 0:09Speaker 2: It's slow, Yeah, it really is, especially when you need
- 0:12Speaker 2: data at scale exactly.
- 0:14Speaker 1: So what if you could just skip the browser entirely.
- 0:16Speaker 1: Imagine downloading a whole website or checking thousands of links,
- 0:20Speaker 1: maybe building your own little dictionary tool, all with just
- 0:23Speaker 1: like a few lines of code.
- 0:26Speaker 2: That's precisely what we're diving into today. We're going command
- 0:29Speaker 2: line using simple tools that talk HTTP directly. It's about
- 0:33Speaker 2: automating those web tasks in a way that's honestly, way
- 0:37Speaker 2: faster and more powerful than clicking around.
- 0:39Speaker 1: Yeah, it feels like unlocking a superpower for anyone dealing
- 0:42Speaker 1: with web data, especially in programming or cybersecurity. So okay,
- 0:46Speaker 1: let's get into it. Where do we start? I guess
- 0:48Speaker 1: with the big download tools.
- 0:50Speaker 2: Absolutely, the two pillars really are rigged and Curl. Let's
- 0:54Speaker 2: start with the rig It. It's your classic super flexible
- 0:56Speaker 2: file downloader.
- 0:57Speaker 1: Okay, rig At. What makes it flexible?
- 0:59Speaker 2: Well, its strength is in reliability and how it manages resources.
- 1:03Speaker 2: Think about scripts running unattended maybe on a server. You
- 1:07Speaker 2: need them to be robust, right.
- 1:08Speaker 1: You can't have it crashing if the network kickups for
- 1:10Speaker 1: a second.
- 1:11Speaker 2: Exactly and rugged has that built in. Yeah, you use
- 1:13Speaker 2: the Dutch T flag tell it how many times to
- 1:16Speaker 2: retry a failed download?
- 1:17Speaker 1: Okay?
- 1:18Speaker 2: Or for something really critical, you can even use dash
- 1:20Speaker 2: t infinite. It'll just keep trying until it gets the file.
- 1:23Speaker 2: That resilience is baked right in.
- 1:25Speaker 1: That's incredibly useful for background jobs. But what about the
- 1:28Speaker 1: other side. If you're pulling down huge amounts of data,
- 1:31Speaker 1: you don't want to accidentally swamp your network.
- 1:34Speaker 2: Good point. That's where Riggot's resource controls are key. You
- 1:37Speaker 2: can use limit rate to cap the bandwidth it uses
- 1:40Speaker 2: like limit read five hundred k for five hundred kilobytes
- 1:43Speaker 2: per second or one meter.
- 1:45Speaker 1: For a megabyte, so you play nice with other applications
- 1:47Speaker 1: and users precisely.
- 1:49Speaker 2: And you can also set a download quota with quota
- 1:51Speaker 2: or DASHQ to stop it from filling up your disk
- 1:53Speaker 2: if something goes wrong or the site is bigger than
- 1:56Speaker 2: you thought.
- 1:56Speaker 1: It makes sense being responsible with automation. Now, what about
- 2:00Speaker 1: rabbing like an entire web site right for archiving or
- 2:03Speaker 1: offline viewing.
- 2:04Speaker 2: That's the mirror option. It basically acts like a web crawler,
- 2:06Speaker 2: downloading everything recursively. You can also fine tune it with
- 2:09Speaker 2: natch R for recursive and and L for the depth level.
- 2:13Speaker 1: Okay, so it pulls down all the HTML images, everything.
- 2:16Speaker 2: Yep, But there's a crucial step if you want to
- 2:18Speaker 2: actually browse that local copy effectively.
- 2:21Speaker 1: The links probably still point to the live site, right.
- 2:23Speaker 2: Exactly by default they do, so you need to add
- 2:25Speaker 2: the dash K or convert link's flag that tells Gollie
- 2:29Speaker 2: get to rewrite the links inside the downloaded files to
- 2:31Speaker 2: point to the local copies. Makes the mirror actually navigable offline.
- 2:35Speaker 1: Got it. Dash K is essential for mirroring and probably
- 2:39Speaker 1: a good idea to be careful about permissions when mirroring
- 2:41Speaker 1: entire sites.
- 2:42Speaker 2: Right. Oh, absolutely, always respect robots, dot txt and the
- 2:46Speaker 2: site's terms of service.
- 2:47Speaker 1: Be ethical about it.
- 2:48Speaker 2: Okay. So to baliget is the robust downloader and mirror tool.
- 2:51Speaker 2: What about curl? You called it the protocol powerhouse. Yeah,
- 2:55Speaker 2: curl is different. It supports way more protocols h GGP, HGGPS, FTP,
- 2:59Speaker 2: SF loads more, but its real strength is its versatility
- 3:03Speaker 2: and how it interacts with the web and handles data
- 3:06Speaker 2: how so well, by default, curl just dumps the downloaded
- 3:10Speaker 2: content straight to your standard output.
- 3:12Speaker 1: Ah, so you can immediately pipe it into another command
- 3:14Speaker 1: like GP or said exactly.
- 3:16Speaker 2: It's designed for that kind of command line chaining. And
- 3:19Speaker 2: if you don't want to progress meter messing up that output,
- 3:22Speaker 2: you just add silent okay.
- 3:23Speaker 1: And saving to a file.
- 3:24Speaker 2: You can use so that's upper case O to save
- 3:27Speaker 2: it using the final name from the URL, or use
- 3:30Speaker 2: as lowercase O to specify your own file name.
- 3:33Speaker 1: Right and resuming downloads like w get YEP.
- 3:36Speaker 2: Curl uses dash e FATS. The hyphen tells it to
- 3:38Speaker 2: automatically figure out where the download left off and restart
- 3:41Speaker 2: from there. Nice.
- 3:42Speaker 1: Okay, but you mentioned deeper interaction. What's a key feature there?
- 3:45Speaker 2: One of the most powerful is retrieving only the HTDP
- 3:48Speaker 2: headers use ash I or head.
- 3:50Speaker 1: Why would you want just the headers?
- 3:52Speaker 2: Think about efficiency. Let's say you want to check if
- 3:54Speaker 2: a link is even alive before you download a potentially
- 3:57Speaker 2: huge file, or you want to know the file size
- 4:00Speaker 2: that's in the content link header, or maybe check the
- 4:03Speaker 2: last modified date to see if you even need to
- 4:05Speaker 2: download it again.
- 4:06Speaker 1: Ah, so you get all that metadata without downloading the
- 4:09Speaker 1: actual content exactly.
- 4:11Speaker 2: It's just a tiny request saves tons of bandwidth and time,
- 4:15Speaker 2: especially if you're checking hundreds or thousands of URLs. It's
- 4:18Speaker 2: a fundamental trick for efficient webscripting.
- 4:20Speaker 1: Makes perfect sense, and kurl can also deal with websites
- 4:23Speaker 1: that behave differently based on how you access them.
- 4:26Speaker 2: Yes, sometimes a site wants to know where you came from.
- 4:28Speaker 2: You can mimic clicking a link from say Google using
- 4:32Speaker 2: the referraor flag okay. Or you might hit an old
- 4:35Speaker 2: site that only works with like Internet Explorer six. It
- 4:38Speaker 2: blocks your script because it doesn't recognize the user agent.
- 4:42Speaker 2: So you use a or user agent to tell the server, hey,
- 4:45Speaker 2: I'm actually Internet Explorer six or whatever browser it expects
- 4:48Speaker 2: you spoof your identity basically hah.
- 4:51Speaker 1: Okay, So you can pretend to be a different browser
- 4:53Speaker 1: to get past those checks. That's pretty slick.
- 4:56Speaker 2: It often gets you access where you'd otherwise be blocked.
- 4:58Speaker 1: This leads us nicely into automating more interactive stuff like
- 5:01Speaker 1: submitting forms or logging in right.
- 5:03Speaker 2: This usually involves POC requests as opposed to the simpler
- 5:07Speaker 2: get requests, where parameters are just in the URL with
- 5:11Speaker 2: PCT that data is sent in the request body.
- 5:14Speaker 1: Kind of hidden, like when you submit a log in form.
- 5:16Speaker 1: How do you automate that with curl or regate.
- 5:19Speaker 2: You need to send that form data structure it as
- 5:21Speaker 2: a string usually like username using password might pass. Then
- 5:24Speaker 2: you pass this string using curl G or WEG post data.
- 5:28Speaker 1: Okay, tapa D for curl post data for regat. But
- 5:30Speaker 1: you mentioned something critical about that string.
- 5:32Speaker 2: Yes, this trips up so many people. That data string
- 5:36Speaker 2: far one value one of our two value two. It
- 5:39Speaker 2: must be enclosed in quotes, single or double quotes.
- 5:41Speaker 1: Why what happens if you forget the quotes?
- 5:43Speaker 2: The shell interprets the ampersand the and area completely differently.
- 5:47Speaker 2: It sees and thinks you want to run the command
- 5:50Speaker 2: before the ampersand in the background, and then it tries
- 5:52Speaker 2: to run whatever comes after it as a new command.
- 5:55Speaker 1: So it breaks the data string and the command fails,
- 5:58Speaker 1: probably in a confusing way.
- 5:59Speaker 2: Totally. The shell's special characters get in the way. Quoting
- 6:03Speaker 2: the string tells the shell treat this whole thing as
- 6:06Speaker 2: one piece of data. Don't interpret the amper sands. It's crucial,
- 6:10Speaker 2: got it?
- 6:10Speaker 1: Quote your POSD data. That's a key takeaway. Okay, let's
- 6:14Speaker 1: combine some of these ideas. How about building a custom
- 6:17Speaker 1: tool like a command line dictionary.
- 6:19Speaker 2: Perfect example of chaining tools. Let's say you find a
- 6:22Speaker 2: dictionary website with an API.
- 6:24Speaker 1: You'd start with curl right, curl to fetch the definition
- 6:27Speaker 1: from the API, probably passing your apikey and the word
- 6:30Speaker 1: you want to find exactly.
- 6:32Speaker 2: Curl grabs the raw data, maybe it's JSON, maybe HTML
- 6:36Speaker 2: often messy. Then you pipe that output directly into GP grap.
- 6:41Speaker 1: For pattern matching, so you'd g rep for the lines
- 6:43Speaker 1: that actually contain the definition tag precisely.
- 6:46Speaker 2: Maybe the API uses a specific tag like definition or DT.
- 6:50Speaker 2: You GP for lines containing DT. Now you have just
- 6:53Speaker 2: the relevant lines, but they still have the tags.
- 6:55Speaker 1: Okay, so tags are still there. How do we clean
- 6:57Speaker 1: that up?
- 6:58Speaker 2: Pipe it again? This time INTISA said the.
- 7:00Speaker 1: Stream editor FIST for text transformation.
- 7:02Speaker 2: Right, you use it, said command to strip out those
- 7:04Speaker 2: DT tags or whatever tags they are.
- 7:06Speaker 1: Just delete them, leaving just the plain text definition yep.
- 7:10Speaker 2: And maybe for a final touch, pike deck clean text
- 7:12Speaker 2: into nl NLL adds line numbers, just makes the output
- 7:16Speaker 2: look neat If there are multiple definitions. Yeah, so crow vetches, grep, selects,
- 7:20Speaker 2: clean zieled in formats. Four tools chained together to make
- 7:23Speaker 2: a brand new utility.
- 7:24Speaker 1: That's really cool, taking standard tools and composing them. Okay,
- 7:28Speaker 1: let's shift to a common maintenance headache. Finding broken links
- 7:31Speaker 1: on a website.
- 7:32Speaker 2: Doing that manually is just painful impossible on any reasonably
- 7:36Speaker 2: sized site. The tool for this is actually Links, which
- 7:39Speaker 2: is primarily a text based web browser.
- 7:41Speaker 1: Links not write or Curl this time.
- 7:43Speaker 2: For this specific task. Links has a great option at
- 7:46Speaker 2: dash traversal. You pointed at a starting ur links dash
- 7:49Speaker 2: traversal http dot example dot com. It crawls the site
- 7:54Speaker 2: following links.
- 7:54Speaker 1: Okay, it crawls. What does it produce?
- 7:56Speaker 2: It generates a file usually called reject dot dot or similar,
- 8:00Speaker 2: which confusingly contains all the URLs that found during the traversal,
- 8:03Speaker 2: not just broken ones.
- 8:05Speaker 1: Ah. Okay, so it gives you a big list of
- 8:07Speaker 1: every link on the site exactly.
- 8:08Speaker 2: Now you have your target list. You then write a
- 8:11Speaker 2: simple loop in your shell script for each URL on
- 8:14Speaker 2: that list.
- 8:14Speaker 1: Let me guess we're back to curl.
- 8:16Speaker 2: You got it, but specifically curl the header only requests
- 8:20Speaker 2: we talked about it's.
- 8:21Speaker 1: Super efficient because you only need the status cup precisely.
- 8:24Speaker 2: You run curl gaya I on the URL and check
- 8:27Speaker 2: the very first line of the output. If it starts
- 8:29Speaker 2: with something like ahgtp one point one two hundred, okay,
- 8:33Speaker 2: the link is good.
- 8:34Speaker 1: And if it's four oh four not found or four
- 8:36Speaker 1: oh three forbidden or anything.
- 8:38Speaker 2: Else, bingo, that's a broken link. You log it. This
- 8:41Speaker 2: turns a task that could take days manually into like
- 8:45Speaker 2: a few minutes of script execution.
- 8:47Speaker 1: That is a massive timesaver for anyone maintaining a website. Okay,
- 8:51Speaker 1: final stretch, let's talk about Parson content and some more
- 8:53Speaker 1: creative automation. Getting clean text out of messy HTML is
- 8:57Speaker 1: often the goal.
- 8:58Speaker 2: Yeah, raw HTML is full of job script CSS tags.
- 9:02Speaker 2: Trying to extract just the readable text with say, regular
- 9:05Speaker 2: expressions is usually a nightmare.
- 9:06Speaker 1: In there done that, it's brittle.
- 9:08Speaker 2: The elegant solution back to links the text browser. This
- 9:11Speaker 2: time you use the dump.
- 9:12Speaker 1: Flag links duck url exactly.
- 9:15Speaker 2: It renders the page internally, then dumps the formatted text
- 9:18Speaker 2: content to standard output. Like reading the page in a
- 9:21Speaker 2: text only browser. All the HTML tags are gone.
- 9:24Speaker 1: Whoa, so it just gives you the clean text, no
- 9:27Speaker 1: parson required.
- 9:28Speaker 2: Pretty much. It does the hard work of rendering and
- 9:30Speaker 2: extracting the text for you. It's brilliant for grabbing articles,
- 9:33Speaker 2: blog posts, or any text heavy content for processing with
- 9:37Speaker 2: standard text tools like said, ox, sort, et cetera.
- 9:40Speaker 1: That simplifies things enormously. Does it handle.
- 9:42Speaker 2: Links even better? Links eat dump actually lists all the
- 9:46Speaker 2: hyperlinks it found separately at the bottom under a references section.
- 9:50Speaker 2: You don't even need to hunt for a tags in
- 9:52Speaker 2: the main text.
- 9:53Speaker 1: Wow, okay. Links d eight dump sounds incredibly useful for
- 9:56Speaker 1: text extraction. How about images? Say I want to download
- 9:59Speaker 1: all images from a specific page.
- 10:01Speaker 2: That's a bit more involved but totally scriptable.
- 10:04Speaker 1: Yeah.
- 10:04Speaker 2: You typically fetch the pages HTML first, probably with CURL
- 10:07Speaker 2: or reget.
- 10:07Speaker 1: Okay, got the HTML source.
- 10:09Speaker 2: Then you need to parse it to find all the
- 10:10Speaker 2: MG tags. You could use grap or said or a
- 10:13Speaker 2: more robust HKML parser if needed. From the MG tag,
- 10:16Speaker 2: you extract the URL from the HTRT attribute.
- 10:19Speaker 1: Right. The src attribute holds the image location.
- 10:22Speaker 2: But here's the tricky part path resolution. Some SRT attributes
- 10:26Speaker 2: will have full URLs like http dot image dot jpg.
- 10:30Speaker 1: Those are easy, but others will be relative paths like
- 10:32Speaker 1: images logo dot png or just header dot gift exactly.
- 10:36Speaker 2: If you just try to curl images logo dot png,
- 10:38Speaker 2: it won't work because curl doesn't know the base website context.
- 10:42Speaker 1: So the script needs to be smart about it.
- 10:44Speaker 2: It does. It needs to know the base URL of
- 10:46Speaker 2: the page it's scraped. You need sy have http dot
- 10:49Speaker 2: example dot com. Then for every relative image path it
- 10:52Speaker 2: finds one starting with or just a file name, it
- 10:55Speaker 2: has to prepend that base URL to make it a
- 10:57Speaker 2: full downloadable url.
- 10:59Speaker 1: Ah, images logo dot png becomes http dot example dot com.
- 11:03Speaker 1: A feud is logo dot png.
- 11:04Speaker 2: Correct. Once all paths are absolute URLs, you can loop
- 11:07Speaker 2: through them and use curl maybe with silent and adro
- 11:10Speaker 2: to save them to download all the images. Handling those
- 11:12Speaker 2: relevant paths is.
- 11:13Speaker 1: Key makes sense. It adds a layer of logic. What
- 11:16Speaker 1: about tracking changes on a website, like getting notified if
- 11:18Speaker 1: a page updates.
- 11:19Speaker 2: Classic monitoring task, yeah, very douable. With scripting, you use
- 11:22Speaker 2: the diff utility.
- 11:23Speaker 1: Ye if the file comparison tool yep.
- 11:25Speaker 2: The script runs periodically maybe viacron. First time it runs,
- 11:29Speaker 2: it downloads the page using curl or reget and saves it,
- 11:32Speaker 2: say as last dot html.
- 11:34Speaker 1: That's the baseline exactly.
- 11:36Speaker 2: On subsequent runs, it downloads the current version of the page,
- 11:39Speaker 2: saving it as maybe recent dot html. Then it runs
- 11:42Speaker 2: diff last dot html recent dot html.
- 11:45Speaker 1: And diff will output the differences between the two file Right.
- 11:49Speaker 2: If diff produces any output, it means the page has changed.
- 11:52Speaker 2: The scripts can then maybe email you the output of
- 11:55Speaker 2: diff showing you exactly what lines were added.
- 11:57Speaker 1: Or removed, and if there are changes, the script should
- 11:59Speaker 1: then update the baseline.
- 12:01Speaker 2: Good catch. Yeah, if changes are found, it should replace
- 12:04Speaker 2: last dot html with the contents of recent dot html,
- 12:07Speaker 2: so that becomes the new baseline for the next check.
- 12:09Speaker 1: Very clever, simple tools, powerful result. One lass creative one.
- 12:13Speaker 1: Automating a web photo album.
- 12:15Speaker 2: Yeah, imagine you have a folder full of photos. Creating
- 12:18Speaker 2: the HTML gallery page. Manually writing all the MG tags
- 12:21Speaker 2: linking thumbnails to full images.
- 12:23Speaker 1: Is tedious, definitely repetitive.
- 12:25Speaker 2: You can automate it. A Bash script can loop through
- 12:27Speaker 2: all the jpeg files in a directory. For each image,
- 12:30Speaker 2: it can use the convert.
- 12:31Speaker 1: Utility convert from image Magic.
- 12:33Speaker 2: That's the one. Use convert to automatically create a small
- 12:36Speaker 2: thumbnail version maybe one hundred pixels.
- 12:38Speaker 1: Y okay, so it generates thumbnails on the fly.
- 12:41Speaker 2: Then the script just uses simple echo or cat commands
- 12:44Speaker 2: to write the necessary HTML code to a file, say
- 12:47Speaker 2: index dot html. It writes a tag linking the thumbnail
- 12:51Speaker 2: image Energsrc thumbnail dot jpg to the full size image
- 12:56Speaker 2: h referro full size dot jpg.
- 12:58Speaker 1: And it does this for every image in the floor,
- 13:00Speaker 1: building up the index dot html page automatically.
- 13:03Speaker 2: Exactly, You run the script and boom you have a
- 13:05Speaker 2: ready to upload photo gallery web page without any manual
- 13:09Speaker 2: htmail coding or image re sizing.
- 13:11Speaker 1: That's a great example of automating a common, repetitive web
- 13:14Speaker 1: related task right from the command line.
- 13:16Speaker 2: It really shows how these tools bridge system tasks and
- 13:19Speaker 2: web content.
- 13:19Speaker 1: We've covered a ton today, from the reliability of raygooget
- 13:22Speaker 1: for downloads and mirroring, to the surgical precision of CURL,
- 13:25Speaker 1: especially with header texts, jacks automating form submissions with post
- 13:30Speaker 1: data and remembering the quotes, combining tools like gp and
- 13:34Speaker 1: said for custom utilities.
- 13:35Speaker 2: Using linkstaedtge dump for clean text extraction, scripting image downloads
- 13:39Speaker 2: with path handling, tracking site changes with diff, and even
- 13:43Speaker 2: generating HTML like that photo album.
- 13:45Speaker 1: The big takeaway really is that shell scripting isn't some
- 13:48Speaker 1: outdated technique. It's actually an incredibly fast, efficient, and adaptable
- 13:53Speaker 1: way to interact with the web's data and automate tasks
- 13:56Speaker 1: By passing the browser entirely.
- 13:59Speaker 2: It puts direct programmatic control in your hands. You're not
- 14:02Speaker 2: limited by what a browser interface lets you do.
- 14:03Speaker 1: Absolutely it turns the Web into something you can truly
- 14:06Speaker 1: program against. So final thought for everyone listening a bit
- 14:10Speaker 1: of a review question. Imagine you need to check the
- 14:13Speaker 1: status of, say five thousand links on a site. You
- 14:16Speaker 1: want to know if there are two hundred okay, or
- 14:19Speaker 1: four or four not found before you even think about
- 14:22Speaker 1: downloading the content of those pages. Why is using curl
- 14:25Speaker 1: I the most efficient way to do this compared to
- 14:27Speaker 1: just running a standard CURL command on each url mull that.
- 14:29Speaker 2: One over thinking about the why behind using I really
- 14:33Speaker 2: cements the efficiency mindset that makes command line automation so powerful.
- 14:36Speaker 1: Great point until our next deep dive, Happy scripting.