Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Q1: A mini version of the grep command implemented in Bash.

Supports case-insensitive search, line numbers, inverted match, and robust error handling.

Features

  • Case-insensitive search (default)
  • Show line numbers for matches (-n)
  • Invert match to show non-matching lines (-v)
  • Combinable options: e.g., -vn, -nv
  • Help flag: --help or -h for usage info
  • Graceful error handling for missing arguments or files

Usage

./mygrep.sh [-n] [-v] <search_string> <file>

Options

  • -n Show line numbers for each match
  • -v Invert the match (print non-matching lines)
  • --help or -h Display help message

Examples

Given a file testfile.txt:

Hello world
This is a test
another test line
HELLO AGAIN
Don't match this line
Testing one two three

Terminal Examples (as shown in the screenshot)

1. Basic search:

$ ./mygrep.sh hello testfile.txt
Hello world
HELLO AGAIN

2. With line numbers:

$ ./mygrep.sh -n hello testfile.txt
1:Hello world
4:HELLO AGAIN

3. Invert match with line numbers:

$ ./mygrep.sh -vn hello testfile.txt
2:This is a test
3:another test line
5:Don't match this line
6:Testing one two three

4. Missing search string:

$ ./mygrep.sh -v testfile.txt
Error: Missing search string or file.
Usage: ./mygrep.sh [-n] [-v] <search_string> <file>
Options:
  -n         Show line numbers for each match
  -v         Invert the match (print non-matching lines)
  --help     Display this help message

5. File not found:

$ ./mygrep.sh -vn hello testfil.txt
Error: File 'testfil.txt' not found.
Usage: ./mygrep.sh [-n] [-v] <search_string> <file>
Options:
  -n         Show line numbers for each match
  -v         Invert the match (print non-matching lines)
  --help     Display this help message

6. Keyword not found:

$ ./mygrep.sh -vn helloo testfile.txt
Keyword not Found in file
Usage: ./mygrep.sh [-n] [-v] <search_string> <file>
Options:
  -n         Show line numbers for each match
  -v         Invert the match (print non-matching lines)
  --help     Display this help message

7. Help message:

$ ./mygrep.sh --help
Usage: ./mygrep.sh [-n] [-v] <search_string> <file>
Options:
  -n         Show line numbers for each match
  -v         Invert the match (print non-matching lines)
  --help     Display this help message

How the Script Handles Arguments and Options

  • Option Parsing:
    Uses getopts to process -n, -v, and -h (help). Sets internal flags (show_line_numbers, invert_match) accordingly.
  • Argument Validation:
    After parsing options, checks if both a search string and filename are provided. If not, prints an error and usage.
  • File Check:
    Verifies the file exists before running the search.
  • Command Building:
    Dynamically builds the grep command with the appropriate options (-i, -n, -v) based on user input.
  • No Matches Handling:
    If no matches are found, prints "Keyword not Found in file" instead of just failing silently.

Extending the Script (Regex, -c, -l, etc.)

To support regex or additional options like -c (count), the script would:

  • Extend getopts to recognize new flags.
  • Add more internal flags (e.g., count_matches).
  • Adjust the dynamic grep command to include new options.
  • Possibly use a case block after parsing to organize how different combinations of options affect behavior.
  • This modular approach would make the script scalable for more features.

Hardest Part of Implementation

The most challenging aspect was handling option combinations (like -vn or -nv) and building the dynamic grep command correctly.
Ensuring robust error handling for invalid or missing arguments, so the script never crashes or behaves unexpectedly, also required careful logic and validation.

Q2: DNS and Service Reachability Troubleshooting Walkthrough

1. Verify DNS Resolution

  • Check what DNS servers are configured:

    cat /etc/resolv.conf
  • Try resolving using system DNS:

    dig internal.example.com
  • Try resolving with a public DNS (e.g., Google):

    dig @8.8.8.8 internal.example.com
    • If resolution works with 8.8.8.8 but not with system DNS, then internal DNS might be broken.
    • If it fails on both, maybe it's not a DNS issue or maybe the dashboard is only internal and not visible to public DNS like 8.8.8.8.

2. Diagnose Service Reachability

  • Check if the website is reachable using curl to test HTTP(S) responses:
    curl -v http://internal.example.com
    curl -v https://internal.example.com

3. Listing All Possible Causes

  • DNS:

    1. Incorrect DNS entry for internal.example.com
    2. DNS server misconfigured or down
    3. Firewall blocking DNS requests
    4. Caching issues
  • Network:

    1. Firewall blocking traffic to and from the server
    2. Routing issue
    3. NAT misconfigurations
  • Web Server:

    1. Web server up but bound to wrong IP/interface
    2. TLS misconfiguration

4. Propose Fixes

  • Incorrect DNS Entry or Misconfigured DNS Server:

    • If dig @internal-dns internal.example.com returns nothing or wrong IP, SSH into DNS server and fix the zone entry, then restart the service:
    sudo nano /etc/bind/zones/internal.example.com.zone
    sudo systemctl restart named
  • Firewall Blocks DNS or Web Traffic:

    • Check firewall rules on client and server:
    sudo iptables -L -n
    • Allow traffic on ports 80 and 443:
    sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

Bonus Part

  • Configure /etc/hosts for Quick Testing

    • Edit /etc/hosts to bypass DNS:
    sudo nano /etc/hosts
    # Add line
    <resolved_ip> internal.example.com
    • Test with:
    curl -v http://internal.example.com
    curl -v https://internal.example.com
  • Make DNS Settings Persistent

    • Using systemd-resolved:
    sudo mkdir -p /etc/systemd/resolved.conf.d
    sudo nano /etc/systemd/resolved.conf.d/dns_servers.conf
    
    # Add:
    [Resolve]
    DNS=10.0.0.2
    Domains=example.com
    
    # Restart service
    sudo systemctl restart systemd-resolved

Troubleshooting Report for internal.example.com Connectivity Issue

Steps Taken

  1. DNS Resolution Verification

    • Checked configured nameserver in /etc/resolv.conf:

      nameserver 10.255.255.254
    • Tested DNS resolution using dig:

      dig internal.example.com

      Result: NXDOMAIN — no answer returned.

    • Cross-checked with public DNS (8.8.8.8):

      dig @8.8.8.8 internal.example.com

      Result: NXDOMAIN.

    • Verified using nslookup:

      nslookup internal.example.com

      Result: server can't find internal.example.com: NXDOMAIN.

  2. Service Reachability Check

    • Could not test service reachability (ports 80/443) as domain resolution failed.

Root Cause Analysis

  • DNS server (10.255.255.254) does not resolve internal.example.com.
  • Public DNS (8.8.8.8) also does not recognize it.
  • Issue is isolated to DNS resolution failure.

🔪 List of Possible Causes

Layer Possible Cause
DNS Layer Missing DNS record for internal.example.com
DNS Layer Incorrect DNS server configuration on clients
DNS Layer Internal DNS server outage or misconfiguration
DNS Layer Firewall blocking DNS queries to the internal DNS server
Application Layer (Less likely) Service moved to a different domain without DNS update

Next Actions Proposed

Step Action Command Example
1 Confirm correct internal DNS server IP address (Contact network team)
2 Temporarily add static IP in /etc/hosts if IP address is known sudo nano /etc/hosts
3 Update /etc/resolv.conf with the correct DNS server (temporary test) sudo nano /etc/resolv.conf
4 Persist DNS settings with systemd-resolved or NetworkManager if needed sudo resolvectl dns eth0 <correct_dns_server>

Blockers

  • Missing correct IP address for internal.example.com.
  • Need either:
    • Internal DNS server IP that knows the record, or
    • Direct IP address of internal.example.com.

Status: Issue isolated to DNS resolution.
Waiting for: IP address or internal DNS server information.


Bonus: Commands to Apply Fix Once Information Is Available

Update /etc/hosts (Temporary Bypass)

sudo nano /etc/hosts
# Add the line:
192.168.x.x internal.example.com

Update /etc/resolv.conf Temporarily

sudo nano /etc/resolv.conf
# Replace with correct nameserver
nameserver <correct_internal_dns>

Persist DNS Using systemd-resolved

sudo resolvectl dns eth0 <correct_internal_dns>
sudo resolvectl domain eth0 example.com

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages