Skip to content

Add networkwizard.sh: a TUI for network configuration #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

rkarahan80
Copy link

@rkarahan80 rkarahan80 commented Jun 12, 2025

This commit introduces networkwizard.sh, a shell script that provides a text-based user interface (TUI) for managing common network configurations on FreeBSD. It utilizes the bsddialog utility.

Features:

  • View detailed status of all network interfaces, including IP addresses, MAC addresses, and Wi-Fi connection details (SSID, signal).
  • Manage wired Ethernet connections:
    • List available wired interfaces.
    • Activate interfaces using DHCP.
    • Deactivate interfaces.
  • Manage Wi-Fi connections:
    • Create wlan (cloned) interfaces from physical Wi-Fi devices.
    • Scan for available Wi-Fi networks.
    • Connect to Open and WPA-PSK secured Wi-Fi networks.
    • Disconnect from Wi-Fi networks.
    • Destroy wlan interfaces.

The utility is designed to simplify common networking tasks for you if you may prefer a menu-driven interface over manual command-line operations. It sources bsdconfig's common.subr and dialog.subr for helper functions.

A README.md file is included, detailing the script's purpose, requirements, usage, and limitations (e.g., changes are non-persistent).

Summary by Sourcery

Add networkwizard.sh, a bsddialog-based TUI utility that simplifies network configuration tasks on FreeBSD.

New Features:

  • Provide a text-based interface to view detailed network interface status
  • Enable activation, deactivation, and status inspection of wired Ethernet interfaces via a menu
  • Allow Wi-Fi management including creating/destroying wlan interfaces, scanning, connecting (Open/WPA-PSK), and disconnecting

Documentation:

  • Add README.md outlining the script’s purpose, requirements, usage, features, and limitations

This commit introduces `networkwizard.sh`, a shell script that provides
a text-based user interface (TUI) for managing common network
configurations on FreeBSD. It utilizes the `bsddialog` utility.

Features:
- View detailed status of all network interfaces, including IP addresses,
  MAC addresses, and Wi-Fi connection details (SSID, signal).
- Manage wired Ethernet connections:
    - List available wired interfaces.
    - Activate interfaces using DHCP.
    - Deactivate interfaces.
- Manage Wi-Fi connections:
    - Create `wlan` (cloned) interfaces from physical Wi-Fi devices.
    - Scan for available Wi-Fi networks.
    - Connect to Open and WPA-PSK secured Wi-Fi networks.
    - Disconnect from Wi-Fi networks.
    - Destroy `wlan` interfaces.

The utility is designed to simplify common networking tasks for you if you
may prefer a menu-driven interface over manual command-line
operations. It sources `bsdconfig`'s `common.subr` and `dialog.subr`
for helper functions.

A README.md file is included, detailing the script's purpose,
requirements, usage, and limitations (e.g., changes are non-persistent).
@rkarahan80 rkarahan80 requested review from a team as code owners June 12, 2025 07:55
@ghostbsd-bot
Copy link

Can one of the admins verify this patch?

Copy link

sourcery-ai bot commented Jun 12, 2025

Reviewer's Guide

This PR adds a new shell-based TUI utility, networkwizard.sh, that leverages bsddialog and helper subr scripts to interactively view network status and manage both wired and Wi-Fi interfaces on FreeBSD, accompanied by a README.md with usage instructions and caveats.

Sequence Diagram for Viewing Network Status

sequenceDiagram
    actor User
    participant NW as networkwizard.sh
    participant IFCONF as ifconfig
    participant WPA as wpa_cli
    participant NETS as netstat
    participant BSDD as bsddialog

    User->>NW: Selects "View Network Status"
    NW->>IFCONF: Executes `ifconfig -l` (get interfaces)
    IFCONF-->>NW: List of interfaces
    loop For each relevant interface
        NW->>IFCONF: Executes `ifconfig <iface>` (get details)
        IFCONF-->>NW: Interface details (MAC, IP, status)
        alt Interface is Wi-Fi
            NW->>WPA: Executes `wpa_cli -i <iface> status`
            WPA-->>NW: Wi-Fi status (SSID, etc.)
            NW->>WPA: Executes `wpa_cli -i <iface> signal_poll`
            WPA-->>NW: Signal strength
        end
    end
    NW->>NETS: Executes `netstat -rn -f inet` (IPv4 route)
    NETS-->>NW: IPv4 default gateway
    NW->>NETS: Executes `netstat -rn -f inet6` (IPv6 route)
    NETS-->>NW: IPv6 default gateway
    NW->>BSDD: Formats and displays all info via `bsddialog --textbox`
    BSDD-->>User: Shows network status
Loading

Sequence Diagram for Activating a Wired Interface

sequenceDiagram
    actor User
    participant NW as networkwizard.sh
    participant DHC as dhclient
    participant IFCONF as ifconfig
    participant BSDD as bsddialog

    User->>NW: Selects "Manage Wired", Interface, "ACTIVATE"
    NW->>DHC: Executes `dhclient <iface>` (background)
    DHC-->>NW: (process starts)
    NW->>IFCONF: Executes `ifconfig <iface> up`
    IFCONF-->>NW: (command status)
    alt Activation initiated
        NW->>BSDD: `bsddialog --msgbox "Interface activating..."`
        BSDD-->>User: Shows status message
    else Activation command failed
        NW->>BSDD: `bsddialog --msgbox "Failed to bring up..."`
        BSDD-->>User: Shows error message
    end
Loading

Sequence Diagram for Connecting to a Wi-Fi Network

sequenceDiagram
    actor User
    participant NW as networkwizard.sh
    participant BSDD as bsddialog
    participant WPA as wpa_cli
    participant IFCONF as ifconfig
    participant DHC as dhclient

    User->>NW: Selects "Manage Wi-Fi", <wlan_iface>, "CONNECT"
    NW->>BSDD: `bsddialog --inputbox "Enter SSID:"`
    BSDD-->>User: Prompts for SSID
    User->>BSDD: Enters SSID
    BSDD-->>NW: SSID
    NW->>BSDD: `bsddialog --yesno "Is network open?"`
    BSDD-->>User: Prompts for network type
    User->>BSDD: Selects Yes/No
    BSDD-->>NW: Network type
    alt Network requires password
        NW->>BSDD: `bsddialog --passwordbox "Password for SSID:"`
        BSDD-->>User: Prompts for password
        User->>BSDD: Enters password
        BSDD-->>NW: Password
    end
    NW->>WPA: `add_network`, `set_network ssid`, `set_network key_mgmt/psk`
    WPA-->>NW: Network ID, command status
    NW->>WPA: `enable_network`, `select_network`
    WPA-->>NW: Command status
    NW->>IFCONF: Executes `ifconfig <wlan_iface> up`
    IFCONF-->>NW: Command status
    NW->>DHC: Executes `dhclient <wlan_iface>` (background)
    DHC-->>NW: (process starts)
    NW->>BSDD: `bsddialog --infobox "Attempting to connect..."`
    BSDD-->>User: Shows connecting message
    NW->>WPA: `status` (to verify)
    WPA-->>NW: Connection status
    NW->>IFCONF: `ifconfig <wlan_iface>` (to verify IP)
    IFCONF-->>NW: Interface status with IP
    alt Connection successful
        NW->>BSDD: `bsddialog --msgbox "Connected to SSID, IP: X.X.X.X"`
        BSDD-->>User: Shows success message
    else Connection failed/partial
        NW->>BSDD: `bsddialog --msgbox "Failed to connect or get IP..."`
        BSDD-->>User: Shows failure/status message
    end
Loading

Sequence Diagram for Creating a wlan Interface

sequenceDiagram
    actor User
    participant NW as networkwizard.sh
    participant IFCONF as ifconfig
    participant BSDD as bsddialog

    User->>NW: Selects "Manage Wi-Fi", "CREATE wlan for <phys_dev>"
    NW->>NW: Determine next available wlan number (e.g., wlanX)
    NW->>IFCONF: Executes `ifconfig wlanX create wlandev <phys_dev> up`
    IFCONF-->>NW: Command status
    alt Creation successful
        NW->>BSDD: `bsddialog --msgbox "Interface wlanX created..."`
        BSDD-->>User: Shows success message
    else Creation failed
        NW->>BSDD: `bsddialog --msgbox "Failed to create wlanX..."`
        BSDD-->>User: Shows error message
    end
Loading

Class Diagram for networkwizard.sh Structure and Dependencies

classDiagram
    class networkwizard_sh {
        <<Shell Script>>
        +COMMON_SUBR : String
        +DIALOG_SUBR : String
        +view_status() void
        +manage_one_wired_interface(iface_name: String) void
        +manage_wired() void
        +_get_wifi_interfaces() String list
        +manage_one_wifi_interface(wlan_iface: String) int
        +manage_wifi() void
        +_populate_wifi_interface_lists() void
        +main_menu() String
        # Main script execution / loop implicitly uses these functions
    }
    class bsdconfig_libraries {
        <<External Shell Libraries>>
        +f_dialog_title(title: String) void
        +f_dialog_backtitle(backtitle: String) void
        +f_dialog_buttonbox_size(...)
        +...other common and dialog helper functions...
    }
    networkwizard_sh ..> bsdconfig_libraries : sources and uses
Loading

File-Level Changes

Change Details Files
Introduce core TUI script structure and main loop
  • Source bsdconfig common.subr and dialog.subr for helper functions
  • Initialize dialog titles and backtitles for consistency
  • Define main_menu for top-level option selection (Status, Wired, Wi-Fi, Exit)
  • Implement script entrypoint loop to dispatch on menu choices
usr.sbin/networkwizard.sh
Implement network status viewer
  • List all non-loopback interfaces via ifconfig -l
  • Parse MAC, IPv4, IPv6, flags, and active status from ifconfig output
  • Detect wireless interfaces to fetch SSID, BSSID, signal, auth via wpa_cli
  • Retrieve default IPv4/IPv6 gateway from netstat
  • Display aggregated info in a scrollable bsddialog textbox
usr.sbin/networkwizard.sh
Add wired-interface management functions
  • Filter out wireless and loopback devices to list only Ethernet
  • Provide per-interface menu to Activate (DHCP), Deactivate, View Details
  • Invoke dhclient in background and use ifconfig up/down
  • Show success or error messages via bsddialog msgbox
usr.sbin/networkwizard.sh
Add Wi-Fi management functions
  • Detect physical Wi-Fi devices and existing wlan clones
  • Allow creation of new wlan interfaces from physical devices
  • Implement scan, connect (open or WPA-PSK), disconnect, destroy, status actions
  • Use wpa_cli for network operations and dhclient for DHCP
  • Refresh menus after clone creation or destruction
usr.sbin/networkwizard.sh
Add README.md with documentation
  • Describe utility purpose and features overview
  • List requirements (FreeBSD, bsddialog, root privileges)
  • Provide usage instructions and example invocation
  • Note non-persistent nature of changes and advanced configuration caveats
usr.sbin/README.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@rkarahan80
Copy link
Author

rkarahan80 commented Jun 12, 2025 via email

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @rkarahan80 - I've reviewed your changes - here's some feedback:

  • There’s duplicated wireless interface detection logic (_get_wifi_interfaces vs _populate_wifi_interface_lists); consolidate into a single function and remove the unused implementation.
  • Functions like view_status and manage_wifi are very large and complex—consider breaking them into smaller helper functions or modules to improve readability and maintainability.
  • Add explicit checks at startup for required external commands (e.g., wpa_cli, dhclient, netstat) so the script can fail fast if a dependency is missing.
Here's what I looked at during the review
  • 🟡 General issues: 8 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

# Get all interfaces
interfaces=$(ifconfig -l)

status_info="Network Interface Status:\n\n"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Literal \n sequences aren’t converted to newlines in the string

To display multiline text correctly, use printf, a here-document, or embed actual newlines in the variable instead of literal \n sequences.


# Display the collected information
# Use --textbox which is better for scrollable text
bsddialog --title "Network Status" --textbox "$status_info" 22 78
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): --textbox requires a filename, not a content string

--textbox expects a file path, not the content itself. Write status_info to a temporary file and pass its path, or use an option that accepts piped input.

scan_menu_items=""
item_count=0
# Skip header line and parse results
echo "$scan_results_raw" | awk 'NR>1 {printf "%s \"%s %s %s\"\n", $5, $5, $3, $4}' | while IFS= read -r line; do
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Variables set inside piped while loop run in a subshell

Assignments to scan_menu_items and item_count inside the piped while loop won't persist after the loop. Consider using process substitution or input redirection to ensure variable changes are retained.

done
}

# Helper to identify Wi-Fi interfaces
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: The _get_wifi_interfaces stub is unused and can be removed

Since _get_wifi_interfaces is not used, please remove it to avoid dead code and potential confusion.


status_info="Network Interface Status:\n\n"

for iface in $interfaces; do
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Unquoted variable expansion can lead to word splitting or globbing

Quote "$interfaces" or use an array to prevent unintended word splitting or glob expansion during iteration.

status_info="$status_info--------------------------------------------------\n\n"

# Get IPv4 default route
ipv4_route=$(netstat -rn -f inet | grep '^default' | awk '{print "Default Gateway (IPv4): " $2 " via " $4}')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Label 'via' might misrepresent the interface field

In netstat, $4 is the interface name. Use 'Gateway:

on ' for accuracy.

# dhclient -r "$iface_name" >/dev/null 2>&1
# Start new dhclient
# Need to run dhclient in background otherwise it might block the script
dhclient "$iface_name" >/dev/null 2>&1 &
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Starting dhclient before bringing the interface up

Bring the interface up with ifconfig up before starting dhclient to ensure it can bind to the active link.

- **View Detailed Status:** Show the full `ifconfig` output for the interface.

3. **Manage Wi-Fi Connections:**
- Lists existing `wlan` (virtual Wi-Fi) interfaces and physical Wi-Fi capable devices.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (typo): Suggest hyphenating "Wi-Fi capable" to "Wi-Fi-capable".

Hyphenating compound modifiers before nouns improves clarity, so use "Wi-Fi-capable devices."

Suggested change
- Lists existing `wlan` (virtual Wi-Fi) interfaces and physical Wi-Fi capable devices.
- Lists existing `wlan` (virtual Wi-Fi) interfaces and physical Wi-Fi-capable devices.

@rkarahan80 rkarahan80 closed this by deleting the head repository Jun 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

2 participants