本文介绍了确定连接是有线还是无线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个bash脚本,当在每台计算机上运行该脚本时,它会检测CPU内核数,HDD/分区,是否存在电池等,并生成conkyrc文件来显示该PC的相关信息使用我喜欢的风格.但是,我很难确定PC是通过有线还是无线Internet连接.

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.

有人知道确定bash脚本连接类型的方法吗?

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

推荐答案

尝试一下:

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

详细信息

在有线系统上,/proc/net/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

  • tail -n+3命令用于删除标题.

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

grep -q .命令测试无线接口处于活动状态时是否存在后续线路.

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

iwconfig是从/proc/net/wireless读取信息的实用程序:

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

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

这篇关于确定连接是有线还是无线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 05:41