1 Answer 正确答案
- Is there a place I can find other possible device names for my wifi card? When running `-listpreferredwirelessnetworks en1´ I get the error "en1 is not a Wi-Fi interface. ** Error: Error obtaining wireless information."– ssssaaaaFeb 20, 2018 at 9:38
- Just realised I hadn't added my device specs to this post, so have done that now, hopefully that helps.– ssssaaaaFeb 20, 2018 at 9:47
- 1
sudo networksetup -listallhardwareports
gives you the list of the devices Feb 20, 2018 at 15:17 - 1
- 1could also be
en0
, that's the case on a 2019 MBP at least. but yes,-listallhardwareports
shows that too, after I goten1 is not a Wi-Fi interface.
. Oct 3, 2022 at 1:49
You must log in to answer this question.
Not the answer you're looking for? Browse other questions tagged
How to find the currently connected network service from the command line?
11 Answers
- True - each record contains a
status
field that either hasactive
orinactive
as a value.– nwinklerJun 17, 2015 at 6:18 - 1It will give you false result if you are sharing your internet. Suppose your are sharing ethernet internet via wifi then status for both Ethernet and wifi will be "active" Jan 2, 2017 at 10:18
- 5This doesn't show you which service is being used - both wifi and ethernet will show as 'active' if you have both enabled and an ethernet cord plugged in.– tog22May 19, 2017 at 18:00
- 1This is quite handy for checking if a connection is not connected. For example my ethernet is generally only connected at work. So I can deduce I am at home by this not being in the list.
ifconfig | grep $(networksetup -listnetworkserviceorder | grep 'Ethernet, Device' | sed -E "s/.*(en[0-9]).*/\1/")
. Then I can switch locations based on the above being empty. Nov 16, 2017 at 21:28
- I had to pipe the first line to
tac
to iterate through the interfaces in reverse order because I often have WiFi connected as well as a USB ethernet adapter (which is the preferred device in Network). In this case I want the most preferred active devise to be printed:services=$(networksetup -listnetworkserviceorder | grep 'Hardware Port' | tac)
– ghrAug 1, 2016 at 22:07 - @ghr that doesn't make any sense,
networksetup -listnetworkserviceorder
already outputs "the most preferred device" first...– MotselMar 1, 2019 at 10:37 - Looks like I ended up modifying the above script a little so that it only prints out 1 service, rather than any connected. I had to
tac
so that later (non-preferred) services wouldn't overwrite$currentservice
. Should have been clearer in that original comment.– ghrMar 2, 2019 at 16:59 - 1can confirm that this is still the best answer in these trying times when most computers are connected to VPN, either fully tunnelled or not, where other answers may return the device name for the VPN tunnel
tunX
. Jun 3, 2020 at 2:01 - Note this script uses hardware port names which are generally the same as service names, but can fail if you manually rename or if you have multiple services with the same hardware port name, typical for USB Ethernet. This gist has an amended version to fix this issue. Feb 11, 2022 at 20:38
- In my script I've replaced public query with:
set public (dig +short myip.opendns.com @resolver1.opendns.com)
My reasoning for this is that a dns server (like opendns) is less likely to be down than a website and is faster. And I removed the sleep statement. No need to wait for the dns-server reply. Execution time for my script 177 ms. Yours takes 5.237 seconds, but does more of course. Still a big difference.– PeterVPMay 1, 2017 at 16:09 - This assumes that network services have the same names as their underlying hardware ports.
networksetup -getdnsservers ...
takes a network service name, which can sometimes be different than the hardware port passed to it in this script. You can see them all with-listnetworkserviceorder
– ChrisSep 13, 2021 at 21:28
- the second
grep
can be done insideawk
:networksetup -listnetworkserviceorder |grep -B1 "$(route get example.com | awk "/interface/ {print \$2}")"
– ccpizzaJan 24, 2022 at 9:43 - @ccpizza Yours is better, as its the same command on the cli and in the shell alias (wrt ~~quoting~~ escaping). I've updated our answer. Jan 25, 2022 at 15:14
- 3If you want just the network service name:
networksetup -listnetworkserviceorder | awk "/$(route get example.com | awk '/interface/ {print $2}')/{sub(/\([0-9]+\)\ /,\"\",a); print a} {a=\$0}"
– PierzMay 10, 2022 at 9:25 - This also work for just getting the network service name:
networksetup -listnetworkserviceorder |grep -B1 "$(route get 1.1.1.1 | awk '/interface/ {print $2}')" | awk -F'\\) ' '/\([0-9]+\)/ {print $2}'
– ChrisOct 25, 2022 at 20:18
- 1You are assuming a Wi-Fi connection which is not necessarily the case for all scenarios.– ccpizzaDec 16, 2021 at 18:01
- 2Might be easier to define a
echo_italic
shell function instead of wrapping all theseecho
s inset_color
calls.– nohillside♦May 2, 2017 at 11:54 - 1
You must log in to answer this question.
Not the answer you're looking for? Browse other questions tagged
最近用Keyboard Maestro写在Mac 上自动登入公司VPN 的脚本,
不过偶尔遇到一些例外状况要处理,
像今天是遇到Wi-fi 网路被我手动关闭,导致脚本执行失败…
因此要想个方法,来侦测Wi-fi 的状况,以及将Wi-fi 打开~
在网路上找了一下,原来Mac 内建的networksetup 指令就能做到这些事了~
举例来说,可以用networksetup -listallnetworkservice 列出所有的网路类型:
testuser@localhost ~ $ networksetup -listallnetworkservices
An asterisk ( * ) denotes that a network service is disabled.
MT65xx Preloader
Ethernet
FireWire
Wi-Fi
Bluetooth PAN
Thunderbolt Bridge
可以看到有个网路类型是“Wi-Fi”,接着用networksetup -getinfo 可以取得相关资讯,
像是目前的IP address、Wi-Fi ID 等等~
如果是要侦测Wi-Fi 有没有成功连上网路的话,搜寻下面的输出里面,
有没有IP address 之类的字串加上一个合法的IP 位址就行了:
testuser@localhost ~ $ networksetup -getinfo Wi-Fi
DHCP Configuration
IP address: 10.1.2.3
Subnet mask: 255.255.252.0
Router: 10.1.2.254
Client ID:
IPv6: Automatic
IPv6 IP address: none
IPv6 Router: none
Wi-Fi ID: 11:22:33:44:55:66
如果要操作Wi-Fi 设备的话,得先知道设备的名称,
这个设备的名称在每台电脑上都有可能不一样(不像“Wi-Fi” 这个网路类型是每台Mac 都应该有的),
因此要用networksetup -listallhardwareports 来列出所有的网路设备,
像我们可以从下面的输出中,得知Wi-Fi 的网卡设备是en1:
testuser@localhost ~ $ networksetup -listallhardwareports
Hardware Port: Ethernet
Device: en0
Ethernet Address: 11:11:11:11:15:85
Hardware Port: FireWire
Device: fw0
Ethernet Address: 22:22:22:22:fe:6a:59:dc
Hardware Port: Wi-Fi
Device: en1
Ethernet Address: 33:33:33:33:aa:8e
Hardware Port: Bluetooth PAN
Device: en3
Ethernet Address: 44:44:44:44:aa:8f
Hardware Port: Thunderbolt 1
Device: en2
Ethernet Address: 55:55:55:55:9d:c0
Hardware Port: Thunderbolt Bridge
Device: bridge0
Ethernet Address: 66:66:66:66:9d:c0
VLAN Configurations
===================
接着用networksetup -getairportpower 就能得知目前Wi-Fi 设备是不是开启的:
testuser@localhost ~ $ networksetup -getairportpower en1
Wi-Fi Power ( en1 ) : Off
用networksetup -setairportpower 就能开关指定的Wi-Fi 设备:
networksetup -setairportpower en1 on
用networksetup -getairportpower 再看一次,果真Wi-Fi 设备已经被打开啰:
testuser@localhost ~ $ networksetup -getairportpower en1
Wi-Fi Power ( en1 ) : On
参考资料:Managing WIFI connections using the Mac OSX terminal command line
How to pull the "en0" from this: 'networksetup -listallhardwareports'
- Kindly always show us your expected output in CODE TAGS as well as your efforts too in your posts. May 17, 2018 at 17:11
2 Answers
- Thanks....the output from this networksetup -listallhardwareports | awk '/Hardware Port: Wi-Fi/{getline;print "Device id: "$2}' Is: Device id: en0 May 17, 2018 at 17:46
最近用Keyboard Maestro写在Mac 上自动登入公司VPN 的脚本,
不过偶尔遇到一些例外状况要处理,
像今天是遇到Wi-fi 网路被我手动关闭,导致脚本执行失败…
因此要想个方法,来侦测Wi-fi 的状况,以及将Wi-fi 打开~
在网路上找了一下,原来Mac 内建的networksetup 指令就能做到这些事了~
举例来说,可以用networksetup -listallnetworkservice 列出所有的网路类型:
testuser@localhost ~ $ networksetup -listallnetworkservices An asterisk ( * ) denotes that a network service is disabled. MT65xx Preloader Ethernet FireWire Wi-Fi Bluetooth PAN Thunderbolt Bridge
可以看到有个网路类型是“Wi-Fi”,接着用networksetup -getinfo 可以取得相关资讯,
像是目前的IP address、Wi-Fi ID 等等~
如果是要侦测Wi-Fi 有没有成功连上网路的话,搜寻下面的输出里面,
有没有IP address 之类的字串加上一个合法的IP 位址就行了:
testuser@localhost ~ $ networksetup -getinfo Wi-Fi DHCP Configuration IP address: 10.1.2.3 Subnet mask: 255.255.252.0 Router: 10.1.2.254 Client ID: IPv6: Automatic IPv6 IP address: none IPv6 Router: none Wi-Fi ID: 11:22:33:44:55:66
如果要操作Wi-Fi 设备的话,得先知道设备的名称,
这个设备的名称在每台电脑上都有可能不一样(不像“Wi-Fi” 这个网路类型是每台Mac 都应该有的),
因此要用networksetup -listallhardwareports 来列出所有的网路设备,
像我们可以从下面的输出中,得知Wi-Fi 的网卡设备是en1:
testuser@localhost ~ $ networksetup -listallhardwareports Hardware Port: Ethernet Device: en0 Ethernet Address: 11:11:11:11:15:85 Hardware Port: FireWire Device: fw0 Ethernet Address: 22:22:22:22:fe:6a:59:dc Hardware Port: Wi-Fi Device: en1 Ethernet Address: 33:33:33:33:aa:8e Hardware Port: Bluetooth PAN Device: en3 Ethernet Address: 44:44:44:44:aa:8f Hardware Port: Thunderbolt 1 Device: en2 Ethernet Address: 55:55:55:55:9d:c0 Hardware Port: Thunderbolt Bridge Device: bridge0 Ethernet Address: 66:66:66:66:9d:c0 VLAN Configurations ===================
接着用networksetup -getairportpower 就能得知目前Wi-Fi 设备是不是开启的:
testuser@localhost ~ $ networksetup -getairportpower en1 Wi-Fi Power ( en1 ) : Off
用networksetup -setairportpower 就能开关指定的Wi-Fi 设备:
networksetup -setairportpower en1 on
用networksetup -getairportpower 再看一次,果真Wi-Fi 设备已经被打开啰:
testuser@localhost ~ $ networksetup -getairportpower en1 Wi-Fi Power ( en1 ) : On
参考资料:Managing WIFI connections using the Mac OSX terminal command line
How to pull the "en0" from this: 'networksetup -listallhardwareports'
- Kindly always show us your expected output in CODE TAGS as well as your efforts too in your posts. May 17, 2018 at 17:11
2 Answers
- Thanks....the output from this networksetup -listallhardwareports | awk '/Hardware Port: Wi-Fi/{getline;print "Device id: "$2}' Is: Device id: en0 May 17, 2018 at 17:46
macOS:从网络设备名称获取设备名
先从ifconfig命令说起,这个命令后面可以跟上一个叫做interface的参数,就可以查看该interface所代表的网络设备的网络情况,比如:
ifconfig en0; ifconfig en1
一般来说,从这里很难看出这个en0和en1那个是无线网卡、那个是有线网。尤其是新版的laptop没有有线网卡,那么en0就是无线网卡。
为了动态获得WiFi对应的是哪个设备,我们需要使用networksetup命令的帮助。
networksetup命令有一个-listallhardwareports的命令,给出下面形式的系统网络配置信息: