|
OpenBSD General Other questions regarding OpenBSD which do not fit in any of the categories below. |
|
Thread Tools | Display Modes |
|
|||
Wifi script to display current connection
I'm working on a script which I plan to call from a x11/tint2 panel button. The goal is provide a quick means to display the parameters of the current wifi connection that are available to non-root users.
obsd_wifi_tint2.sh Code:
#!/bin/ksh echo "Current Wifi Connection:" echo "" ifconfig | tr " " "\n" | grep -A5 nwid > wifi.txt echo "s/n" >> wifi.txt ifconfig | tr " " "\n" | grep -A6 nwid | tail -n -1 >> wifi.txt echo "ip" >> wifi.txt ifconfig | tr " " "\n" | grep -A1 inet | tail -n -2 | tail -n -1 >> wifi.txt cat wifi.txt | paste -s -d '\t\n' - rm wifi.txt Code:
Current Wifi Connection: nwid Shepnet chan 8 bssid d8:eb:97:23:bb:1b s/n -80dBm ip 192.168.2.5 Last edited by shep; 27th April 2019 at 11:13 PM. |
|
|||
Not that it probably matters much here, but to possibly improve efficiencyi: you're taking the entire output of ifconfig and translating every space to a new line then using grep and throwing away most of that work.
Grep first, then translate. But I like to use cut(1) which can return multiple parts. For example: Code:
ifconfig | grep nwid | cut -d ' ' -f 3,5,7 |
|
|||
I also would recommend to create a temporary file with mktemp(1) in the /tmp directory.
But if you use a so-called "Here document" you don't need to use a temporary file at all. Code:
#!/bin/sh IP="$(ifconfig | tr ' ' "\n" | grep -A1 inet | tail -n -2 | tail -n -1)" cat <<END -------------------------------- Wifi parameters ------------------------------- IP : ${IP} END Code:
-------------------------------- Wifi parameters ------------------------------- IP : 192.168.222.242
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
Initially I was able to capture nwid XXXX, chan XX and bssid XX:XX:XX:XX:XX with the first ifconfig invocation but it is messy and not clear what the code is doing. I'm thinking I'll use @j65nk0 "Here Document" strategy and parse out nwid, chan, bssid, s/n and ip to 5 variables and then 'cat' them to sdtout. I think this would make it easier for users to customize their outputs. Also will read up on cut and see if I can extract the values more efficiently than pipeing through 2 instances of tail.
Thanks Last edited by shep; 27th April 2019 at 11:45 PM. |
|
|||
Considering the possibility that the WiFi parameters in theory could change between multiple invocations of ifconfig you could save its text output in a variable once. You then echo that variable multiple times for each processing with grep.
The resulting output could in theory be out of date, but at least it would be a consistent report of the past ....
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
I was thinking in terms of just the current connection when using the new auto-join feature:
OpenBSD gains Wi-Fi "auto-join". At an area with several Access points, it would be nice to see and perhaps decide to adjust. Revised script Code:
#!/bin/ksh NWID="$(ifconfig | grep nwid | cut -d " " -f 3)" CHAN="$(ifconfig | grep nwid | cut -d " " -f 5)" BSSID="$(ifconfig | grep nwid | cut -d " " -f 7)" SN="$(ifconfig | grep nwid | cut -d " " -f 8)" IP="$(ifconfig | grep inet | cut -d " " -f 2 | tail -n -1)" cat <<END ---------------------------------- Current WiFi Connection Parameters ---------------------------------- NWID : $NWID CHAN : $CHAN BSSID : $BSSID S/N : $SN IP : $IP END Code:
----------------------------------- Current Wifi Connection Parameters ----------------------------------- NWID : Shepnet CHANNEL : 8 BSSID : d8:eb:97:23:bb:1b S/N : -81dBm IP : 192.168.2.5 Last edited by shep; 28th April 2019 at 01:30 AM. |
|
|||
Code:
hp_server$ cat -n shep_final 1 #!/bin/ksh 2 NWID="$(ifconfig | grep nwid | cut -d " " -f 3)" 3 CHAN="$(ifconfig | grep nwid | cut -d " " -f 5)" 4 BSSID="$(ifconfig | grep nwid | cut -d " " -f 7)" 5 SN="$(ifconfig | grep nwid | cut -d " " -f 8)" 6 IP="$(ifconfig | grep inet | cut -d " " -f 2 | tail -n -1)" To prevent this race condition the following snippet that runs ifconfig once and reuses it for extracting the other data fields.You only have to keep in mind that newlines are converted to blanks/spaces when text output is saved in a variable. Actually this depends on the value of the shell's internal IFS variable. Code:
#!/bin/sh # newlines are converted to blanks/spaces .... IFCONFIG=$(ifconfig) echo ${IFCONFIG} # IFS; Inter Field Separator (see man ksh) # to preserve newlines,save and set IFS to space only OLD_IFS="${IFS}" IFS=" " # capture 'ifconfig' output once and reuse .... IFCONFIG=$(ifconfig) echo ${IFCONFIG} NWID="$(echo ${IFCONFIG} | grep nwid | cut -d " " -f 3)" # # # IP="$(echo ${IFCONFIG} | grep inet | cut -d " " -f 2 | tail -n -1)" cat <<END -------------------------------- Wifi parameters ------------------------------- NWID : ${NWID} IP : ${IP} END Code:
hp_server$ ifconfig | grep inet | tail -n1 | awk '{print $2}' 192.168.222.242
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
@J65nko
It makes sense to capture the output and then parse. With auto-join, a weak signal could drop the connection and establish a new, different connection. If that happened while running the script, the script output would be an inaccurate mix of 2 access points. I will read up on awk and try again tomorrow. |
|
|||
The 'awk' extractions when smoothly but then I tested with my wifi turned off. The script still produced an IP address. I spent the morning trying to write a test for an active connection with a conditional if-the-else.
Code:
#!/bin/ksh #TEST=$(nc -dzw1 8.8.8.8 443) #CONN="$(echo ${TEST} | awk '{print $1}')" #if test ${CONN} = Connection #then # IFS; Inter Field Separator (see man ksh) # to preserve newlines,save and set IFS to space only OLD_IFS="${IFS}" IFS=" " # capture 'ifconfig' output once and reuse .... IFCONFIG=$(ifconfig) #echo ${IFCONFIG} NWID="$(echo ${IFCONFIG} | grep nwid | awk '{print $3}')" CHAN="$(echo ${IFCONFIG} | grep nwid | awk '{print $5}')" BSSID="$(echo ${IFCONFIG} | grep nwid | awk '{print $7}')" SN="$(echo ${IFCONFIG} | grep nwid | awk '{print $8}')" IP="$(echo ${IFCONFIG} | grep inet | tail -n1 | awk '{print $2}')" cat <<END ---------------------------------- Current WiFi Connection Parameters ---------------------------------- NWID : $NWID CHAN : $CHAN BSSID : $BSSID S/N : $SN IP : $IP END #else #cat <<END #---------------------------------- #Currently No WAN Connection #---------------------------------- #END #fi Quote:
I'm also not completely happy with nc 'ing 8.8.8.8 443. I can envision that the Router/LAN is up but the WAN is down. Critique/Suggestions? |
|
|||
If you save the output of ifconfig in a shell variable the newlines are eliminated or replaced with a blank.This results in a single line of text without embedded newlines
Code:
hp_server$ X="$(ifconfig bge)" hp_server$ echo $X bge0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500 lladdr 98:4b:e1:10:08:6b index 1 priority 0 llprio 3 groups: egress media: Ethernet autoselect (100baseTX full-duplex,rxpause,txpause) status: active inet 192.168.222.242 netmask 0xffffff00 broadcast 192.168.222.255 To prevent this you have to customize the internal shell variable IFS to a space. Code:
hp_server$ sh -c 'IFS=" " ; X="$(ifconfig bge)" ; echo $X' bge0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500 lladdr 98:4b:e1:10:08:6b index 1 priority 0 llprio 3 groups: egress media: Ethernet autoselect (100baseTX full-duplex,rxpause,txpause) status: active inet 192.168.222.242 netmask 0xffffff00 broadcast 192.168.222.255 hp_server$ PS. Sorry I missed that you already customized IFS in your latest version .........
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump Last edited by J65nko; 28th April 2019 at 10:46 PM. |
|
|||
The code I posted actually outputs correctly. The problem is when I take the connection down, it will still output an IP address. I thought the solution would be to test for a connection with nc -dzw1 8.8.8.8 443.
I'm having difficulty capturing the ouput of nc. Code:
PooBear$ TEST="$(nc -dzw1 8.8.8.8 443)" Connection to 8.8.8.8 443 port [tcp/https] succeeded! PooBear$ TEST=$(nc -dzw1 8.8.8.8 443) ; echo $TEST Connection to 8.8.8.8 443 port [tcp/https] succeeded! blank line here PooBear$ echo $TEST blank line here Code:
Connection to 8.8.8.8 443 port [tcp/https] succeeded! An unsuccessful connection via nc outputs a string that starts with Code:
nc: Once captured I would like to determine if it starts with "Connection" or "nc". If the string starts with Connection, I would like to run the ifconfig section, else output "There is no WAN Connection" I'm making this complex but do appreciate the help. Last edited by shep; 28th April 2019 at 10:40 PM. |
|
|||
Code:
#!/bin/sh # we also want STDERR (descriptor 2) text to STDOUT (descriptor 1) TEST="$(nc -dzw1 8.8.8.8 443 2>&1)" if echo $TEST | grep '^Connect' >/dev/null ; then echo OK else echo not OK fi
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
Thanks @J65nko, that did the trick
Code:
Huffalump$ sh obsd_wifi_tint2.sh ---------------------------------- Current WiFi Connection Parameters ---------------------------------- NWID : Shepnet CHAN : 8 BSSID : d8:eb:97:23:bb:1b S/N : -81dBm IP : 192.168.2.9 ---ifconfig ral0 down--- Huffalump$ sh obsd_wifi_tint2.sh ---------------------------------- Currently No WAN Connection ---------------------------------- Huffalump$ The final code: Code:
#!/bin/sh TEST="$(nc -dzw1 8.8.8.8 443 2>&1)" if echo $TEST | grep '^Connect' >/dev/null ; then # IFS; Inter Field Separator (see man ksh) # to preserve newlines,save and set IFS to space only OLD_IFS="${IFS}" IFS=" " # capture 'ifconfig' output once and reuse .... IFCONFIG=$(ifconfig) #echo ${IFCONFIG} NWID="$(echo ${IFCONFIG} | grep nwid | awk '{print $3}')" CHAN="$(echo ${IFCONFIG} | grep nwid | awk '{print $5}')" BSSID="$(echo ${IFCONFIG} | grep nwid | awk '{print $7}')" SN="$(echo ${IFCONFIG} | grep nwid | awk '{print $8}')" IP="$(echo ${IFCONFIG} | grep inet | tail -n1 | awk '{print $2}')" cat <<END ---------------------------------- Current WiFi Connection Parameters ---------------------------------- NWID : $NWID CHAN : $CHAN BSSID : $BSSID S/N : $SN IP : $IP END else cat <<END ---------------------------------- Currently No WAN Connection ---------------------------------- END fi Last edited by shep; 28th April 2019 at 11:41 PM. |
|
|||
You mentioned that nc: is displayed when it cannot connect. I had the impression you wanted to check on that and I was not sure if that message would go to stdout or stderr. Just a matter of defensive programming.
The >/devnull redirect prevents cluttering the output of your script with the output of the grep of the successful connection output. To improve readability you could put split the code in in a function when there is a connection and another function to handle the no connection output. The logic would then be something like this: Code:
if .nc ....etc........ ; then wifi_connected function else no_wifi_message function fi
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
||||
Quote:
Code:
NWID="$(echo ${IFCONFIG} | awk '/nwid/{print $3}')" CHAN="$(echo ${IFCONFIG} | awk '/nwid/{print $5}')" BSSID="$(echo ${IFCONFIG} | awk '/nwid/{print $7}')" SN="$(echo ${IFCONFIG} | awk '/nwid/{print $8}')" IP="$(echo ${IFCONFIG} | awk '/inet/{print $2}' | tail -n1)" Code:
echo $IFCONFIG | awk '/inet/{i=$2} END{print i}' EDIT: and you could use the -q option for grep to save a redirect: Code:
if echo $TEST | grep -q '^Connect' ; then
__________________
Are you infected with Wetiko? Last edited by Head_on_a_Stick; 30th April 2019 at 07:17 PM. Reason: Missed the N from NWID |
|
|||
@Head_on_a_Stick
Thanks. I will test/integrate the suggestions this weekend. Also plan to see what is involved with a scan for available AP's. |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Shell script I wrote for connecting to wifi | bceverly | OpenBSD General | 0 | 6th April 2016 05:45 PM |
rc script to start and stop a local ssh socks connection | gso | Programming | 0 | 15th September 2015 12:14 PM |
FreeBSD 11 Current Wifi iwn | ramib | FreeBSD General | 12 | 2nd April 2015 03:15 AM |
autoconnect to wifi (script) | 22decembre | OpenBSD General | 9 | 20th January 2015 06:50 PM |
Losing connection with Atheros wifi | noonereallycares | FreeBSD General | 2 | 11th June 2008 03:57 AM |