diff --git a/nmcli-rofi b/nmcli-rofi index 95da6cd..aa6fffd 100755 --- a/nmcli-rofi +++ b/nmcli-rofi @@ -103,7 +103,7 @@ function rofi_menu () { rofi_cmd "$MENU" } -function get_ssid () { +function get_ssid_device () { # get fields in order eval FIELDSARR=( $(cat $NMCLI_ROFI_SOURCE | awk 'BEGIN { FS=","; OFS="\n" } /^FIELDS/ \ { $1 = substr($1, 8); print $0; }') ) @@ -116,11 +116,12 @@ function get_ssid () { done # let for arithmetical vars - let AWKSSIDPOS=$SSID_POS+1 + let AWKFIELDPOS=$SSID_POS+$2 # get SSID from AWKSSIDPOS - CHSSID=$(echo "$1" | sed 's/\s\{2,\}/\|/g' | awk -F "|" '{print $'$AWKSSIDPOS'}') - echo "$CHSSID" + CHSSID=$(echo "$1" | sed 's/\s\{2,\}/\|/g' | awk -F "|" '{print $'$AWKFIELDPOS'}') + + echo "$CHSSID" } function cleanup_networks () { @@ -128,6 +129,11 @@ function cleanup_networks () { | while read line; do nmcli con delete uuid $line; done } +function get_available_connections() { + avail_conn=$(nmcli connection show | cut -d" " -f1 | tail -n +2) + echo $avail_conn +} + function main () { if [ -r "$DIR/config" ]; then source "$DIR/config" @@ -138,11 +144,28 @@ function main () { fi OPS=$(rofi_menu) - CHSSID=$(get_ssid "$OPS") + CHSSID=$(get_ssid_device "$OPS", 1) + CHDEV=$(get_ssid_device "$OPS", 3) + AVALABLE_CONNECTIONS=$(get_available_connections) if [[ "$OPS" =~ 'Disable' ]]; then nmcli radio wifi off + + elif [[ "$CHSSID" == '--' ]]; then + MSSID=$(echo -en "" | rofi -dmenu -p "SSID: " -mesg "Enter the SSID of the network" \ + -lines 0 -font "$FONT") + + # manual entry of the PASSWORD + MPASS=$(echo -en "" | rofi -dmenu -password -p "PASSWORD: " -mesg \ + "Enter the PASSWORD of the network" -lines 0 -font "$FONT") + + nmcli c del "$MSSID" + nmcli c add type wifi con-name "$MSSID" ifname "$CHDEV" ssid "$MSSID" + nmcli c modify "$MSSID" wifi-sec.key-mgmt wpa-psk + nmcli c modify "$MSSID" wifi-sec.psk "$MPASS" + nmcli c u "$MSSID" + elif [[ "$OPS" =~ 'Enable' ]]; then nmcli radio wifi on @@ -151,28 +174,36 @@ function main () { elif [[ "$OPS" =~ 'Manual' ]]; then # Manual entry of the SSID - MSSID=$(echo -en "" | rofi -dmenu -p "SSID" -mesg "Enter the SSID of the network" \ + MSSID=$(echo -en "" | rofi -dmenu -p "SSID: " -mesg "Enter the SSID of the network" \ -lines 0 -font "$FONT") # manual entry of the PASSWORD - MPASS=$(echo -en "" | rofi -dmenu -password -p "PASSWORD" -mesg \ + MPASS=$(echo -en "" | rofi -dmenu -password -p "PASSWORD: " -mesg \ "Enter the PASSWORD of the network" -lines 0 -font "$FONT") # If the user entered a manual password, then use the password nmcli command if [ "$MPASS" = "" ]; then - nmcli dev wifi con "$MSSID" + nmcli dev wifi con "$MSSID" ifname "$CHDEV" elif [ "$MSSID" != '' ] && [ "$MPASS" != '' ]; then - nmcli dev wifi con "$MSSID" password "$MPASS" + nmcli dev wifi con "$MSSID" password "$MPASS" ifname "$CHDEV" fi else if [[ "$OPS" =~ "WPA2" ]] || [[ "$OPS" =~ "WEP" ]]; then - WIFIPASS=$(echo -en "" | rofi -dmenu -password -p "PASSWORD" \ - -mesg "Enter the PASSWORD of the network" -lines 0 -font "$FONT") + if [[ `echo "$AVALABLE_CONNECTIONS" |grep -w "$CHSSID"` ]]; then + nmcli c u "$CHSSID" + else + WIFIPASS=$(echo -en "" | rofi -dmenu -password -p "PASSWORD: " \ + -mesg "Enter the PASSWORD of the network" -lines 0 -font "$FONT") + + if [[ "$CHSSID" != '' ]] && [[ "$WIFIPASS" != '' ]]; then + nmcli dev wifi con "$CHSSID" password "$WIFIPASS" ifname "$CHDEV" + fi + fi fi if [[ "$CHSSID" != '' ]] && [[ "$WIFIPASS" != '' ]]; then - nmcli dev wifi con "$CHSSID" password "$WIFIPASS" + nmcli dev wifi con "$CHSSID" password "$WIFIPASS" ifname "$CHDEV" fi fi }