network_info.sh is a Bash script that retrieves and displays network-related information for your local machine. It provides:
- Hostname of the machine
- IP address of Google (via DNS lookup)
- Internet connectivity status
This project was built as a learning exercise to understand network commands, Bash pipelines, command substitution, and logical operators.
The goal of this script is to:
- Practice retrieving network information using shell commands
- Understand DNS resolution and how to extract IP addresses
- Check if the local machine has internet connectivity
- Combine commands with pipes and logical operators for clean output
The script formats the results into a readable CLI report.
- A Unix-based system (Linux or macOS)
- Bash shell
Standard commands used: hostname, nslookup, ping, grep, tail, awk. No extra installations required.
git clone https://github.com/AJ1732/shell-script-dashboard.git
cd shell-script-dashboardchmod +x scripts/network_info.shThis step is required because scripts are not executable by default.
./scripts/network_info.sh==== Network Information ====
Hostname: AJ1732s-MacBook-Pro
Google IP: 142.250.190.78
Internet Status: Connected ✓
=============================
The script uses command substitution ($(...)) and pipes to gather network information:
-
$(hostname -s)→ retrieves the short hostname of the machine -
nslookup google.com | grep 'Address' | tail -n1 | awk '{print $2}'→ performs a DNS lookup and extracts Google’s IP address:grep 'Address'→ filters lines containing “Address”tail -n1→ takes the last line (actual IP)awk '{print $2}'→ extracts the second column (the IP)
-
ping -c 1 google.com > /dev/null 2>&1 && echo "Connected ✓" || echo "Disconnected ✗"→ checks if internet is reachable:&&executes if ping succeeds||executes if ping fails- Exit codes determine connectivity status
All values are formatted into a multi-line string and printed using echo.
Challenge: nslookup output includes multiple Address: lines (DNS server + domain IP).
Solution: Used a pipeline to isolate the domain IP:
nslookup google.com | grep 'Address' | tail -n1 | awk '{print $2}'Challenge: How to reliably determine if the machine is online.
Solution: Used a one-liner with ping and logical operators:
ping -c 1 google.com > /dev/null 2>&1 && echo "Connected ✓" || echo "Disconnected ✗"- Exit code
0→ online - Non-zero → offline
Challenge: Avoiding verbose output from ping and nslookup.
Solution: Redirected stdout and stderr to /dev/null for a clean report:
> /dev/null 2>&1- Bash scripting fundamentals
- Command substitution (
$(...)) - Pipelines (
|) and logical operators (&&/||) - DNS lookup using
nslookup - Internet connectivity checking with
ping - Formatting multi-line CLI output