determine if connection is wired or wireless?

2019-05-30 00:57发布

问题:

I have a bash script I made which when run on each of my computers, detects the number of CPU cores, HDDs/partitions, battery present or not, etc, and generate a conkyrc file to display the relevant info for that PC using the style I prefer in my conky. I am having difficulty determining whether the PC is on a wired or wireless internet connection however.

Does anyone know a way to determine the type of connection with a bash script?

回答1:

Try this:

tail -n+3 /proc/net/wireless | grep -q . && echo "We are wireless"

Details

On a hardwired system, the contents of /proc/net/wireless consist of two header lines:

# cat /proc/net/wireless 
Inter-| sta-|   Quality        |   Discarded packets               | Missed | WE
 face | tus | link level noise |  nwid  crypt   frag  retry   misc | beacon | 22

On a system with an active wireless interface, there will be a third line displaying data about that interface.

The command above works as follows

  • The tail -n+3 command is used to remove the header.

  • The grep -q . command tests for the presence of subsequent lines that are present if a wireless interface is active.

Alternative

iwconfig is a utility that reads information from /proc/net/wireless:

iwconfig 2>&1 | grep -q ESSID && echo "We are wireless"


标签: bash conky