Advertisement
Rnery

Dialog to wifi..

Jan 9th, 2024
1,153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.38 KB | Source Code | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. TEMPFILE=$(mktemp /tmp/wifi_config.XXXXXX)
  4.  
  5. function connect_to_wifi() {
  6.     wpa_passphrase "$SSID" "$PASSPHRASE" > /etc/wpa_supplicant/wpa_supplicant.conf
  7.     wpa_supplicant -B -i "$INTERFACE" -c /etc/wpa_supplicant/wpa_supplicant.conf
  8.     dhclient "$INTERFACE"
  9. }
  10.  
  11. function display_form() {
  12.     dialog --title "WiFi Connection" \
  13.            --form "Enter WiFi SSID and passphrase" \
  14.            10 50 0 \
  15.            "SSID:" 1 1 "$SSID" 1 20 30 0 \
  16.            "Passphrase:" 2 1 "$PASSPHRASE" 2 20 30 0 \
  17.            2> "$TEMPFILE"
  18.  
  19.     SSID=$(sed -n 1p "$TEMPFILE")
  20.     PASSPHRASE=$(sed -n 2p "$TEMPFILE")
  21. }
  22.  
  23. # Entry Point..
  24. function main() {
  25.     # Default values, Change to your values
  26.     INTERFACE="<your_interface>"
  27.     SSID=""
  28.     PASSPHRASE=""
  29.  
  30.     # Display the form
  31.     display_form
  32.  
  33.     # Check if the user pressed OK or Cancel
  34.     case $? in
  35.         0)  # OK button pressed
  36.             connect_to_wifi
  37.             dialog --title "Success" --msgbox "Connected to WiFi!" 8 30
  38.             ;;
  39.         1)  # Cancel button pressed
  40.             dialog --title "Cancelled" --msgbox "Operation cancelled." 8 30
  41.             ;;
  42.         255) # Escape key pressed or dialog closed
  43.             dialog --title "Cancelled" --msgbox "Operation cancelled." 8 30
  44.             ;;
  45.     esac
  46.  
  47.     # Clean up temporary files
  48.     rm -f "$TEMPFILE"
  49. }
  50.  
  51. main
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement