bashshellvariablesreconnectwifi

WICD-CLI Script: How i read variable from the list of wireless networks


My whole script is:

#!/bin/bash
while /bin/true; do
    if ! [ "$(ping -c 1 google.com)" ]; then
        echo "Warning: connection lost at $(date) -- restart"  
        wicd-cli --wireless -x
        sleep 5
        wicd-cli --wireless -cn 1
        sleep 60
        if ! [ "$(ping -c 1 google.com)" ]; then
             echo "Waiting for connection going up at $(date)"
             sleep 60
        else 
             echo "Connection on at $(date)" 
        fi
#    else
#        echo "Connection OK on $(date)"
    fi
sleep 60
done

And I need read value from the list:

wicd-cli --wireless -l
#   BSSID           Channel ESSID
0   00:1D:0F:EA:51:02   9   Liho
1   5C:6A:80:4C:67:BB   13  Heidrovi
2   F4:F2:6D:14:87:61   7   InternetNASA
3   00:23:F8:06:10:99   1   TP-LINK
4   D8:FE:E3:19:F8:C3   3   D-Link
5   C4:F0:81:24:15:1F   10  HUAWEI-B315-151F
6   40:A5:EF:2A:B5:2C   7   InternetNASA-plus
7   50:C7:BF:11:E7:9E   4   MAXXNET.cz-pravda2
8   20:2B:C1:96:15:10   1   Internet

for example ESSID D-Link has now connection number 4 I know this:

wicd-cli --wireless -l | grep 'D-Link'
4   D8:FE:E3:19:F8:C3   3   D-Link

My friend advice me this cmd: w

icd-cli --wireless -l | grep 'D-Link' | cut -d' ' -f1
still output is this:
4   D8:FE:E3:19:F8:C3   3   D-Link

wicd-cli --wireless -l | grep 'D-Link' | cut -d'' -f1

cut: oddělovač musí být jediný znak Více informací získáte příkazem „cut --help“. (Czech Language- Translation to English) cut: the separator must be the only character For more information, go to the command „cut --help“.

But works for my purpose cmd:

  wicd-cli --wireless -l | grep 'D-Link' | cut -c1

4

I can also use file for number of wifi by the list...

wicd-cli --wireless -l | grep 'D-Link' | cut -c1 > wifi_number
ao@ROUTER ~ $ cat wifi_number
4

And i need to change script like this:

#!/bin/bash
while /bin/true; do
    if ! [ "$(ping -c 1 google.com)" ]; then
        echo "Warning: connection lost at $(date) -- restart"  
        wicd-cli --wireless -x
        sleep 5
        wicd-cli --wireless -cn ***"READ_NUMBER FROM CMD:***"wicd-cli --    
        wireless -l | grep 'D-Link' | cut -c1  ***(Which is 4 for 
        NOW...Its changing during day...)***



 ***OR (My OR, not the logic OR, like i have second idea how to do it,

        but still dont know how to read character from file)***

        wicd-cli --wireless -l | grep 'D-Link' | cut -c1 > wifi_number

        wicd-cli --wireless -cn ***(READ_NUMBER_FROM_FILE=wifi_number) (I 
        dont know how)***
        ***(OR DIRECTLY SOME CMD LIKE THIS:)
        (HOW TO READ WRITE THIS CMD)***
        wicd-cli --wireless -cn | wicd-cli --wireless -l | grep 'D-Link' |        cut -c1
        ***(THIS SEQUENCE GIVES ME THIS:)***

wicd-cli --wireless -cn | wicd-cli --wireless -l | grep 'D-Link' | cut -c1
Usage: wicd-cli.py [options]

wicd-cli.py: error: -n option requires an argument
4

        sleep 60
        if ! [ "$(ping -c 1 google.com)" ]; then
             echo "Waiting for connection going up at $(date)"
             sleep 60
        else 
             echo "Connection on at $(date)" 
        fi
#    else
#        echo "Connection OK on $(date)"
    fi
sleep 60
done

DESIRED OUTPUT IS CHOICE I MANUALLY SELECT ESSID (e.g. ESSID: Horak_LazNet.cz) WHICH HAS DURING DAY DIFFERENT VALUES IN THE LIST:

wicd-cli -l --wireless
#   BSSID           Channel ESSID
0   00:1D:0F:EA:51:02   9   Liho
1   5C:6A:80:4C:67:BB   13  Heidrovi
2   E4:BE:ED:05:67:98   10  CZNetFreeSromovi
3   E4:BE:ED:7E:71:80   6   Horak_LazNet.cz
4   F4:F2:6D:14:87:61   7   InternetNASA
5   C4:F0:81:24:15:1F   10  HUAWEI-B315-151F
6   D8:B6:B7:86:DD:AD   10  Internet_AC
7   40:A5:EF:2A:B5:2C   7   InternetNASA-plus
8   50:C7:BF:11:E7:9E   4   MAXXNET.cz-pravda2
9   20:2B:C1:96:15:10   1   Internet
10  5C:F4:AB:00:CF:AB   8   Internet_A8
11  E4:BE:ED:B9:18:10   2   MAXXNET.cz-kuznikova1

NOW IS THE NUMBER 3 for CMD IN SCRIPT:

(wicd-cli --wireless -cn *"NUMBER"*) <<<----THIS I Dont know to set up like this pattern <<<----
wicd-cli --wireless -cn 3

Before i Found these script i use cmds in script:

ifconfig wlxd46e0e1576c6 down
ifconfig wlxd46e0e1576c6 up

-DOWN- works but -UP- does not work

And when I use these new script with number, for time that number fitz to my network (e.g.: D-Link) these script works well, with schedule in crontab to run every minute. When i run script from terminal when i am disconnect or in DEAD STATE, like connection EXISTs but connection to Internet does not works then This Script with Right Number of Wifi works excellent. I am apologizing for text bad formating, but this is My First post/question here. I read out first quick "manual" how This site works, and I tried to find answer by search engine, but I was unsucesfull. For clarification, i am using Linux Mint 18.2 Sonya 64bit (Mate version). And I am using together default Network Manager and WICD, because, default network manager i have configured for Access Point Hot Spot for my own personnal Wireless and WICD I use for connecting to Wireless connected to Internet. I find out that WICD work much better for LOW signal Wireless and keep that connection more Stable than original network manager. But i do not find out how to configure Access Point and Connection to Wireless connected to Internet togetger in WICD. There is only posibility to select device (My is wlxd46e0e1576c6 for Wireless with Internet, and another one in default network manager for Access Point for My Own purpose.


Solution

  • If you want a quick one-liner, you can use:

    wicd-cli --wireless -cn `wicd-cli -l --wireless | grep 'D-Link' | cut -c1`
    

    You can also introduce a new variable, say, NET_NUM to make the code more readable.

    NET_NUM=$(wicd-cli -l --wireless | grep 'D-Link' | cut -c1)
    wicd-cli --wireless -cn $NET_NUM
    

    Or even a bash function to make it more general.

    switch_net() {
      [[ $# != 2 ]] && echo "usage: switch_net <ssid_regexp> && exit 1
      SSID=$1
      NET_NUM=$(wicd-cli -l --wireless | grep $SSID | cut -c1)
      wicd-cli --wireless -cn $NET_NUM
    }
    

    and use it later on with:

    switch_net "D-Link"