Skip to content

Latest commit

 

History

History
140 lines (89 loc) · 3.52 KB

File metadata and controls

140 lines (89 loc) · 3.52 KB

🌐 Network Information Script (network_info.sh)

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.


📌 Project Description

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.


🛠️ Requirements

  • A Unix-based system (Linux or macOS)
  • Bash shell

Standard commands used: hostname, nslookup, ping, grep, tail, awk. No extra installations required.


🚀 How to Run the Script

1️⃣ Clone or download the project

git clone https://github.com/AJ1732/shell-script-dashboard.git
cd shell-script-dashboard

2️⃣ Give the script execute permission

chmod +x scripts/network_info.sh

This step is required because scripts are not executable by default.

3️⃣ Run the script

./scripts/network_info.sh

🧪 Example Output

==== Network Information ====
    Hostname: AJ1732s-MacBook-Pro
    Google IP: 142.250.190.78
    Internet Status: Connected ✓
=============================

🧠 How the Script Works

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.


⚠️ Challenges Faced & How They Were Solved

1. Extracting Google’s IP

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}'

2. Checking Internet Connectivity

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

3. Hiding Command Output

Challenge: Avoiding verbose output from ping and nslookup.

Solution: Redirected stdout and stderr to /dev/null for a clean report:

> /dev/null 2>&1

📈 What This Project Demonstrates

  • Bash scripting fundamentals
  • Command substitution ($(...))
  • Pipelines (|) and logical operators (&& / ||)
  • DNS lookup using nslookup
  • Internet connectivity checking with ping
  • Formatting multi-line CLI output