Latest / CyberCode Academy / Course 4 - Learning Linux Shell Scripting | Episode 7: Comprehensive Network Diagnostics and Remote Host Management
Transcript
- 0:00Speaker 1: If you work in programming, system administration, or cybersecurity, the
- 0:04Speaker 1: true power center isn't some glossy graphical interface. It's the
- 0:08Speaker 1: command line. Is where you go from being just a
- 0:11Speaker 1: network user to being a network architect, a controller exactly.
- 0:15Speaker 2: I mean all the systems you manage, the security protocols
- 0:18Speaker 2: you put in place, they all rely on that fundamental layer.
- 0:21Speaker 2: So today we're cutting through all that complexity and we're
- 0:24Speaker 2: going to distill the definitive network command toolkit. We've brought
- 0:28Speaker 2: together the most essential powerful utilities that let you diagnose connections,
- 0:32Speaker 2: automate huge tasks, set up encrypted tunnels, and even create
- 0:35Speaker 2: your own communication channels on the fly.
- 0:38Speaker 1: And our mission here is to get you from just
- 0:40Speaker 1: diagnosing a single connection failure to having that raw surgical
- 0:46Speaker 1: control you need to manage dozens of servers at the
- 0:48Speaker 1: same time. We're starting small with the absolute basics of
- 0:52Speaker 1: network communication, and then we're going all the way up
- 0:54Speaker 1: to advanced automation and analysis. So every network conversation has
- 0:59Speaker 1: to start with ping.
- 1:00Speaker 2: No.
- 1:00Speaker 1: I think everyone knows it checks if a machine is online,
- 1:03Speaker 1: But for a professional audience, what's the real value here
- 1:06Speaker 1: beyond just as simple? Are you there check.
- 1:09Speaker 2: Well, the real value is measured in milliseconds. Ping uses
- 1:13Speaker 2: what are called ICMP echo packets, and crucially, it measures
- 1:16Speaker 2: the round trip time or RTT.
- 1:18Speaker 1: Ah. Okay, so if.
- 1:19Speaker 2: You're dealing with anything that's sensitive to latency, think like
- 1:21Speaker 2: VoIP quality or financial trading systems, that RTT is basically
- 1:25Speaker 2: the vital sign of your network's health. And for automation,
- 1:28Speaker 2: what we really care about is its exit status. A
- 1:30Speaker 2: simple zero means success, which lets a script instantly know
- 1:33Speaker 2: if a host is reachable, no complex parsing needed.
- 1:36Speaker 1: Right, But we've also heard that administrators sometimes can figure
- 1:39Speaker 1: routers to just ignore ICMP to not respond. So if
- 1:44Speaker 1: PING is being blocked for security, how do you even
- 1:47Speaker 1: verify if a host is really alive?
- 1:49Speaker 2: Yeah, that's a pretty standard security hardening measure. It's designed
- 1:53Speaker 2: to stop an attacker from just you know, swooping your
- 1:55Speaker 2: network to fine live ips. When ICMP is blocked, you
- 1:59Speaker 2: have to probe using a different protocol like TCP or UDP.
- 2:03Speaker 2: Tools like ENMP can send specialized TCP syn packets to
- 2:07Speaker 2: common ports, say Port eighty or four forty three, and
- 2:11Speaker 2: if the host responds, you know it's alive. It's a
- 2:13Speaker 2: way to work around that blocked ping.
- 2:15Speaker 1: So once you confirm the machine is reachable, then trace
- 2:18Speaker 1: route becomes our map. It tells us the journey the
- 2:20Speaker 1: packet actually took precisely.
- 2:22Speaker 2: Trace route shows you the address of every intermediate gateway
- 2:25Speaker 2: or hop as we call them, that the packet crosses
- 2:27Speaker 2: to get to its destination. This is absolutely essential for
- 2:30Speaker 2: troubleshooting where a failure or maybe just a major slowdown
- 2:33Speaker 2: is happening along that path.
- 2:35Speaker 1: And if it's a chrodic problem, maybe you're dealing with
- 2:37Speaker 1: a flaky carrier network. A single static snapshot just isn't
- 2:40Speaker 1: enough information.
- 2:41Speaker 2: That's where MTR comes in. It's basically a dynamic trace route.
- 2:44Speaker 2: Instead of just one snapshot, it's continuously sending packets and
- 2:48Speaker 2: refreshing the data in real time. I actually remember a
- 2:51Speaker 2: time we were troubleshooting this massive connection instability for a client.
- 2:55Speaker 2: We ran MTR for about ten minutes and we saw
- 2:58Speaker 2: the latency spike consistently at Hop number four. There's a
- 3:01Speaker 2: huge carrier network two states away. That real time view
- 3:05Speaker 2: saved us days of wasted time troubleshooting our own local gear.
- 3:08Speaker 1: Okay, let's scale that up. We need to scan an
- 3:11Speaker 1: entire subnet, say two hundred and fifty five addresses, to
- 3:14Speaker 1: find all the live hosts running two hundred and fifty
- 3:18Speaker 1: five pings one after another would take forever. The script
- 3:20Speaker 1: just waits for each one to finish before it moves on.
- 3:23Speaker 2: That accumulated delay is well, it's the enemy of efficiency.
- 3:26Speaker 2: The solution, if you're scripting in Bash, is what i'd
- 3:28Speaker 2: call radical parallelization. You use a standard for loop to
- 3:31Speaker 2: go through the addresses, but inside the loop you run
- 3:34Speaker 2: the pin command in a subshell and you immediately pushed
- 3:36Speaker 2: to the background with an amber sand So.
- 3:38Speaker 1: You're basically launching dozens, maybe hundreds of these pings almost
- 3:42Speaker 1: at the exact same time.
- 3:43Speaker 2: Correct, you launch them all at once, and then this
- 3:46Speaker 2: is the most critical part, You end your script with
- 3:48Speaker 2: the weight command. This make sure the script won't actually
- 3:51Speaker 2: finish until all of those background processes have returned their results.
- 3:56Speaker 2: You get your output in seconds instead of minutes.
- 3:58Speaker 1: That's a great approach for a cut to Bash environment.
- 4:01Speaker 1: But why bother scripting it at all? If I'm a
- 4:04Speaker 1: sisedmin why wouldn't I just install a specialized tool like flipping. Yeah,
- 4:08Speaker 1: what's the real performance gain over a well written parallel script.
- 4:11Speaker 2: The answer is optimization. A parallel Bash script is way
- 4:15Speaker 2: faster than a sequential one. Sure, but flipping is a
- 4:18Speaker 2: dedicated utility. It's written in highly optimized C code. It
- 4:22Speaker 2: manages the sending and receiving of all those ICMP packets
- 4:26Speaker 2: much more efficiently at the operating system level than a
- 4:28Speaker 2: shell script ever could. It's just purpose built for speed.
- 4:32Speaker 1: And it comes with helpful flags, which makes automation even simpler.
- 4:35Speaker 2: Oh. Absolutely, you can generate a whole address range with leg.
- 4:38Speaker 2: You can print only the live machines with lecho, or
- 4:41Speaker 2: just output the unreachable ones with a lequ. It's just
- 4:43Speaker 2: the superior tool when scanning large network ranges is your
- 4:47Speaker 2: main goal.
- 4:48Speaker 1: So once we find a machine, we need to be
- 4:49Speaker 1: able to command it. SSH is the industry standard for that.
- 4:53Speaker 1: It gives us that secure encrypted tunnel, usually running on
- 4:56Speaker 1: Port twenty two, and the basic syntax is pretty simple
- 5:00Speaker 1: username at remote host.
- 5:02Speaker 2: And that first log in is so critical. SSH uses
- 5:06Speaker 2: fingerprint verification to make sure the remote server is who
- 5:09Speaker 2: it claims to be. That's your primary defense against man
- 5:11Speaker 2: in the middle attacks when you connect for the first time.
- 5:14Speaker 1: Right now, when we talk about automation. That interactive password
- 5:17Speaker 1: prompt is the achilles heel of any large scale script.
- 5:21Speaker 1: We'll get to the solution for that in a bit,
- 5:23Speaker 1: but first let's talk about executing a command without needing
- 5:25Speaker 1: a full interactive shell session.
- 5:28Speaker 2: This is absolutely fundamental for scripting. You just passed the
- 5:31Speaker 2: command right after the host name, for example, slush user
- 5:34Speaker 2: at remote host uptime free m. It executes those two
- 5:37Speaker 2: commands remotely, and the output just gets piped right back
- 5:40Speaker 2: to your local terminal. This lets your monitoring scripts pull
- 5:43Speaker 2: info without ever starting up a full shell.
- 5:45Speaker 1: And SSH is a lot more flexible than just command output.
- 5:48Speaker 1: What if I need to run something that relies on
- 5:50Speaker 1: say data compression or even graphics.
- 5:53Speaker 2: Yeah, for limited bandwidth, NASI flag enables data compression right
- 5:57Speaker 2: inside the tunnel, which speeds up transfers, and for graphics,
- 6:00Speaker 2: SSH supports X eleven forwarding. This is fantastic. The CPU
- 6:06Speaker 2: intensive application runs entirely on the powerful remote machine, but
- 6:10Speaker 2: the x protocol output is securely tunneled back to your
- 6:13Speaker 2: local machine and the graphical window just displays right on
- 6:16Speaker 2: your best top. So you get the processing power of
- 6:19Speaker 2: the remote server with the convenience of your local desktop.
- 6:22Speaker 1: Okay, that password prompt has to go. For automation, we
- 6:25Speaker 1: need a way to authenticate securely without a human typing
- 6:29Speaker 1: something in. That's where asymmetric keys really come into their own.
- 6:33Speaker 2: This is really the foundation of secure automation. You generate
- 6:36Speaker 2: a public key and a private key using slush keygen.
- 6:39Speaker 2: The private key is your secret credential. You keep it
- 6:42Speaker 2: safe on your local client machine. The public key is
- 6:45Speaker 2: the one you can share. It gets appended to a
- 6:47Speaker 2: file called authorized keys on the remote server. The server
- 6:50Speaker 2: then uses that public key to challenge your client, and
- 6:53Speaker 2: if your private key can decrypt that challenge, you're authenticated instantly.
- 6:57Speaker 1: And it's secure because the server never actually ease your
- 7:00Speaker 1: private key. So what's the trade off when we're creating
- 7:03Speaker 1: these keys.
- 7:04Speaker 2: It's a balance between security and automation convenience. If you're
- 7:07Speaker 2: creating a key for say an unattended monitoring script, you
- 7:11Speaker 2: often have to choose an MP passphrase so the script
- 7:13Speaker 2: can log in silently. But if the key is for
- 7:16Speaker 2: your personal use, you should always use a strong passphrase
- 7:19Speaker 2: for that extra layer of protection. Luckily, there are tools
- 7:23Speaker 2: like strash copy aid that automate the whole public key
- 7:26Speaker 2: transfer so you don't have to edit files manually.
- 7:29Speaker 1: With automated login sorted, moving files becomes trivial and secure.
- 7:34Speaker 1: We've got two main protocols that build on top of SSH. Right.
- 7:37Speaker 2: First, there's SFTP, the Secure File Transfer Protocol. It gives
- 7:41Speaker 2: you an interactive session, which is really useful if you're
- 7:43Speaker 2: browsing files and directories. It feels a lot like old
- 7:46Speaker 2: school FTP, but it's all running securely over that encrypted
- 7:49Speaker 2: SSH tunnel.
- 7:50Speaker 1: And then there's the favorite of cisadmin's for quick scripted
- 7:53Speaker 1: transfers SCP.
- 7:55Speaker 2: Ah Yes, secure copy. It's the most direct way to
- 7:59Speaker 2: move file. The syntax is a simple source destination format
- 8:03Speaker 2: and it uses that same SSH encryption. For system administrators,
- 8:07Speaker 2: there are two key flags to remember. Dash are for
- 8:10Speaker 2: recursively copying entire directories and DIGP for preserving all the
- 8:14Speaker 2: critical file permissions, modification times, and modes during the copy.
- 8:18Speaker 1: Okay, let's move beyond simple file copying and into actually
- 8:22Speaker 1: manipulating the network itself. Starting with port forwarding, we can
- 8:26Speaker 1: reroute traffic destined for one port right through our own machine.
- 8:29Speaker 2: Standard port forwarding is useful, yeah, it lets you access
- 8:32Speaker 2: a service on a remote machine. Using a local port.
- 8:35Speaker 2: But the real power move is reverse port forwarding. Imagine
- 8:38Speaker 2: you have a sensitive machine and it's trapped behind a
- 8:41Speaker 2: really restrictive corporate firewall. It's completely unreachable from the public Internet.
- 8:45Speaker 1: A local machine with absolutely no public access exactly.
- 8:49Speaker 2: But if that local machine can make an outbound SSH
- 8:52Speaker 2: connection to a public Internet accessible server, you can create
- 8:56Speaker 2: a reverse tunnel. That public server then acts as a
- 8:58Speaker 2: kind of proxy. It receives requests from the outside world
- 9:02Speaker 2: and secretly tunnels them back through the SSH channel to
- 9:05Speaker 2: your restricted local machine.
- 9:07Speaker 1: That sounds like it could be a massive security risk
- 9:10Speaker 1: if it's misused. How do we make sure that only
- 9:13Speaker 1: authorized traffic uses that public server to get to our
- 9:17Speaker 1: private machine, whether the tunnel doesn't just stay open forever.
- 9:21Speaker 2: That is a critical point. First, you have to restrict
- 9:24Speaker 2: access on the public server. You only allow the listening
- 9:26Speaker 2: port to be bound to a trusted local interface, not
- 9:29Speaker 2: the entire Internet. Second, when you're setting up the tunnel,
- 9:32Speaker 2: use the non interactive flags fn to run the forwarding
- 9:35Speaker 2: process silently in the background. This ensures no interactive shell
- 9:39Speaker 2: is left open and it minimizes your exposure.
- 9:41Speaker 1: Speaking of making the remote feel local, what about making
- 9:43Speaker 1: a remote filesystem just appear right on my desktop.
- 9:46Speaker 2: That's where sshfs is just brilliant. It's an extension that
- 9:50Speaker 2: uses the SSH protocol to mount a remote host's filesystem
- 9:54Speaker 2: directly onto your local machine. You can then interact with
- 9:58Speaker 2: remote files and folders just as if they were physically
- 10:00Speaker 2: attached to your own computer. It makes development in data
- 10:04Speaker 2: management incredibly efficient.
- 10:05Speaker 1: Finally, for security and just for debugging, we need to
- 10:08Speaker 1: know who is talking to whom. Listing open ports is
- 10:12Speaker 1: absolutely essential.
- 10:13Speaker 2: It is the first line of defense. If you find
- 10:15Speaker 2: an unexpected port open, it could be unauthorized software or
- 10:19Speaker 2: some other kind of unwanted communication. We have two main
- 10:22Speaker 2: tools for this, Elsoft SDL and Netstat DESHTNP.
- 10:27Speaker 1: For an engineer, what's the functional difference between them? Why
- 10:29Speaker 1: would I choose one over the other?
- 10:31Speaker 2: Well, NETSTA is a classic and it's widely available, but
- 10:34Speaker 2: modern admins they often prefer elsoft, which stands for List
- 10:38Speaker 2: open files. The reason is that it ties the network
- 10:40Speaker 2: connection directly back to the process ID, the PID and
- 10:43Speaker 2: the actual file descriptor. This gives you a complete, really
- 10:46Speaker 2: granular view of the application that's using that port, which
- 10:49Speaker 2: is invaluable for finding root causes or malicious activity.
- 10:53Speaker 1: So we've covered the heavy hitters SSHSFTP, but sometimes you
- 10:57Speaker 1: don't need all that encryption or file structure. You just
- 11:00Speaker 1: need raw, lightweight network communication, and that's.
- 11:03Speaker 2: The perfect scenario for netcat or NC. It's often called
- 11:07Speaker 2: the Swiss Army Knife of networking because it lets you
- 11:09Speaker 2: create arbitrary network sockets. It's just pure undulterated communication over
- 11:14Speaker 2: TCP or UDP.
- 11:16Speaker 1: And to get it working you need two pieces, right,
- 11:18Speaker 1: a listener and a.
- 11:19Speaker 2: Connector exactly on the machine that's receiving, you'd run nc
- 11:22Speaker 2: dashel one two three four to set up a socket
- 11:24Speaker 2: listening on port twelve thirty four. Then from the sender machine,
- 11:28Speaker 2: you connect to that host in that port. Once you're connected,
- 11:31Speaker 2: anything you type on the connecting machine instantly appears on
- 11:33Speaker 2: the listening machine. It's like a literal direct pipe between them.
- 11:36Speaker 1: And since it's so raw and direct, how would a
- 11:39Speaker 1: system administrator use this to do something like a lightning
- 11:42Speaker 1: fast file copy.
- 11:43Speaker 2: You take advantage of shell redirection on the receiving side.
- 11:47Speaker 2: You listen and you redirect the input to a file
- 11:50Speaker 2: ncsh L one two three four received file dot txt.
- 11:54Speaker 2: Then on the sending side, you connect and you redirect
- 11:57Speaker 2: the file's contents into the socket NZ receiver it one two,
- 12:00Speaker 2: three four local file dot txt. The file just flows
- 12:04Speaker 2: across that raw socket instantly. It's the fastest, simplest way
- 12:08Speaker 2: to move data if you can afford to skip the encryption.
- 12:10Speaker 1: That was the complete journey. We went from a single
- 12:13Speaker 1: ICMP packet, understanding its automation value and RTT all the
- 12:17Speaker 1: way through parallel network scanning, securing remote access with SSH keys,
- 12:22Speaker 1: enabling that powerful reverse port forwarding, and finally using netcat
- 12:25Speaker 1: to build custom channels. Mastering the terminal really is mastering
- 12:29Speaker 1: the network.
- 12:29Speaker 2: It is. It's all about combining these fundamental tools to
- 12:32Speaker 2: solve complex real world problems. So, for your final challenge,
- 12:36Speaker 2: if you needed to execute a critical system monitoring script
- 12:38Speaker 2: on fifty independent remote servers and you had to ensure
- 12:41Speaker 2: all fifty commands run simultaneously to save time, and the
- 12:44Speaker 2: script absolutely could not prompt for a password, what specific
- 12:47Speaker 2: set of tools and techniques from today's deep dive would
- 12:49Speaker 2: you use and how would you structure that execution?
- 12:52Speaker 1: That scenario definitely demands integrating SSH keys for that non
- 12:56Speaker 1: interactive access. The specific syntax for running commands remotely over SSH,
- 13:01Speaker 1: and that parallel execution method using sub shells with the
- 13:04Speaker 1: amper sand combined with the weight command. At the end,
- 13:07Speaker 1: that holistic approach is where the real power is.
- 13:10Speaker 2: Focus on those combinations. Experiment with these fundamentals and you
- 13:13Speaker 2: will unlock a much deeper, more powerful understanding of every
- 13:17Speaker 2: network you touch.
- 13:18Speaker 1: Until next time, keep digging.