This is all about using Ruby to check on network stuff — but the golden rule here is: only do this on networks and systems you own or have permission to test. Same idea as it being fine to check if your own front door is locked, but not okay to go around checking your neighbors' doors.
Checking if a "port" is open
Think of your computer or router like an apartment building with a bunch of numbered doors (called ports). Some doors are unlocked (open) because a service is listening there — like a web server sitting behind door 80. Others are locked (closed).
require 'socket'
def scan_port(host, port)
begin
socket = TCPSocket.new(host, port) # try knocking on the door
socket.close # say bye, we're just checking
puts "Port #{port} is open on #{host}"
true
rescue
false # if it errors out, the door's closed/locked
end
end
Then this checks a handful of common "doors" on a device (here, a home router at 192.168.1.1):
['22', '80', '443', '8080'].each do |port|
scan_port('192.168.1.1', port.to_i)
end
Translation: "Try knocking on ports 22, 80, 443, and 8080 on my router, and tell me which ones answer."
Finding info about the network
DNS lookup is basically asking "what's the actual address behind this website's name?" — like looking up someone's street address when you only know their name:
require 'resolv'
ip = Resolv.getaddress('google.com')
puts "Google's IP: #{ip}"
Pinging is like shouting "you there?" and seeing if something shouts back:
require 'net/ping'
pinger = Net::Ping::External.new('8.8.8.8')
if pinger.ping
puts "Host is reachable"
end
If it responds, great — the connection's alive.
Keeping an eye on your own system
def check_connections
connections = `netstat -an`
puts connections.lines.grep(/ESTABLISHED/)
end
This asks your computer: "show me every network conversation you're currently having," then filters it down to just the ones that are actively connected (ESTABLISHED).
def scan_wifi
networks = `iwlist scan 2>/dev/null | grep ESSID`
puts networks
end
This just lists the WiFi networks your own computer can currently see nearby — like glancing at the WiFi menu on your phone.
The important note
Everything above is genuinely useful for network administration — like a homeowner checking their own security cameras. But the same tools, pointed at systems that aren't yours, cross into territory that's illegal and harmful, similar to trying doorknobs on houses that aren't yours.
If you're interested in this stuff for real, better paths are:
- TryHackMe or HackTheBox — legal, gamified platforms specifically built for practicing security skills
- Building your own home lab (a virtual machine or two you control) to safely test against
- Formal cybersecurity courses that teach this properly, with ethics baked in
Want me to help you set up something like a home lab environment, or walk through legitimate network monitoring for systems you actually manage?