determine if connection is wired or wireless?

混江龙づ霸主 提交于 2020-01-03 01:41:31

问题


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"


来源:https://stackoverflow.com/questions/28590874/determine-if-connection-is-wired-or-wireless

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!