DaemonForums  

Go Back   DaemonForums > OpenBSD > OpenBSD General

OpenBSD General Other questions regarding OpenBSD which do not fit in any of the categories below.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 27th April 2019
shep shep is offline
Real Name: Scott
Arp Constable
 
Join Date: May 2008
Location: Dry and Dusty
Posts: 1,507
Default 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
The output displays:

Code:
Current Wifi Connection:

nwid    Shepnet
chan    8
bssid   d8:eb:97:23:bb:1b
s/n     -80dBm
ip      192.168.2.5
My sense of scripting is that there are many ways to achieve the same result so I'm looking for comments on correctness, efficient methodology and style. It started as a one-liner, but ifconfig inconsistently pairs parameter name/parameter value. Signal strength and IP address had no parameter name so I appended them. I played with different table outputs and ended up using "paste" to 2 columns.

Last edited by shep; 27th April 2019 at 11:13 PM.
Reply With Quote
  #2   (View Single Post)  
Old 27th April 2019
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 7,983
Default

I'm not much of a script writer, but I do recommend you use mktemp(1) rather than creating and deleting a file in the working directory.
Reply With Quote
  #3   (View Single Post)  
Old 27th April 2019
TronDD TronDD is offline
Spam Deminer
 
Join Date: Sep 2014
Posts: 305
Default

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 would also try to build the result in a variable instead of using a file.
Reply With Quote
  #4   (View Single Post)  
Old 27th April 2019
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,131
Default

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
This will produce on my non-Wifi desktop:
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
Reply With Quote
  #5   (View Single Post)  
Old 27th April 2019
shep shep is offline
Real Name: Scott
Arp Constable
 
Join Date: May 2008
Location: Dry and Dusty
Posts: 1,507
Default

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.
Reply With Quote
  #6   (View Single Post)  
Old 27th April 2019
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,131
Default

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
Reply With Quote
  #7   (View Single Post)  
Old 28th April 2019
shep shep is offline
Real Name: Scott
Arp Constable
 
Join Date: May 2008
Location: Dry and Dusty
Posts: 1,507
Default

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
with this output
Code:
-----------------------------------
Current Wifi Connection Parameters
-----------------------------------

NWID     :  Shepnet
CHANNEL  :  8
BSSID    :  d8:eb:97:23:bb:1b
S/N      :  -81dBm
IP       :  192.168.2.5
The next step is a script to scan. I've seen commits that some intel wifi chips can do a full scan without stopping and restarting the connection. Don't believe that this applies to Realtek, RaLink or Atheros chips.

Last edited by shep; 28th April 2019 at 01:30 AM.
Reply With Quote
  #8   (View Single Post)  
Old 28th April 2019
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,131
Default

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)"
Maybe I don't understand you because of my lack of WiFi experience, but what I meant is that the NWID you capture from the ifconfig output in line 2 could be in theory out of date/changed when you capture the IP in line 6. A race condition.

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
And by using awk(1) you can get a slightly simpler extraction:
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
Reply With Quote
  #9   (View Single Post)  
Old 28th April 2019
shep shep is offline
Real Name: Scott
Arp Constable
 
Join Date: May 2008
Location: Dry and Dusty
Posts: 1,507
Default

@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.
Reply With Quote
Old 28th April 2019
shep shep is offline
Real Name: Scott
Arp Constable
 
Join Date: May 2008
Location: Dry and Dusty
Posts: 1,507
Default

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
nc -dwz1 8.8.8.8 443 does output
Quote:
Connection . . . . successful!
but my attempts to capture "Connection" and match the string fail.

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?
Reply With Quote
Old 28th April 2019
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,131
Default

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
Your invocations of grep will now all return this single line. ;-)
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$
By customizing IFS like this grep will only return a single line of which you only have to extract the second field $2

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.
Reply With Quote
Old 28th April 2019
shep shep is offline
Real Name: Scott
Arp Constable
 
Join Date: May 2008
Location: Dry and Dusty
Posts: 1,507
Default

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
I'm confused in that
Code:
Connection to 8.8.8.8 443 port [tcp/https] succeeded!
is already a single line and the text is not being written to the $TEXT string.

An unsuccessful connection via nc outputs a string that starts with
Code:
nc:
I'm not capturing the output of the nc command even though I'm using the same syntax as the ifconfig capture. Is my syntax wrong or is this specific to the nc command?

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.
Reply With Quote
Old 28th April 2019
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,131
Default

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
Reply With Quote
Old 28th April 2019
shep shep is offline
Real Name: Scott
Arp Constable
 
Join Date: May 2008
Location: Dry and Dusty
Posts: 1,507
Default

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$
If the nc command runs without errors, I'm not clear on why stderr (descriptor 2) needs to be > to stdout or why the grep ^Connect needs to go to /dev/null. Also thanks for the grep ^ to check the beginning of the line.

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.
Reply With Quote
Old 29th April 2019
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,131
Default

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
Reply With Quote
Old 30th April 2019
Head_on_a_Stick's Avatar
Head_on_a_Stick Head_on_a_Stick is offline
Real Name: Matthew
Bitchy Nerd Elitist
 
Join Date: Dec 2015
Location: London
Posts: 465
Default

Quote:
Originally Posted by shep View Post
Code:
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}')"
No need for grep if you're using awk:
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)"
It is possible to get the IP line with pure awk:
Code:
echo $IFCONFIG | awk '/inet/{i=$2} END{print i}'
But I think tail is quicker.

EDIT: and you could use the -q option for grep to save a redirect:
Code:
if echo $TEST | grep -q '^Connect' ; then
Probably not important though
__________________
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
Reply With Quote
Old 1st May 2019
shep shep is offline
Real Name: Scott
Arp Constable
 
Join Date: May 2008
Location: Dry and Dusty
Posts: 1,507
Default

@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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT. The time now is 02:43 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick