今天给大家介绍的是一款名叫mac-wifi的命令行工具,它可以管理macOS系统的无线网络,并且还提供了交互式Shell。

MacWiFi:一款管理Mac系统WIFI的命令行工具

mac-wifi

mac-wifi脚本可以查询或管理macOS平台上的WiFi配置以及网络环境信息,脚本代码在最小化的类中实现了针对macOS环境的设计逻辑,并且允许开发者轻松地添加针对其他操作系统的支持,但是目前该工具仅支持macOS操作系统。

该工具可以在单一命令行或交互式模式下运行,交互式模式使用了pry gem,并可提供类似Rubyists或其他REPL用户的交互式接口。

在使用这款工具时,你甚至都不需要下载整个代码库,或者进行任何的安装,你只需要运行bin/mac-wifi脚本文件就可以了。

下载地址

mac-wifi

依赖组件

目前,本项目只需要使用下列两个gem:

pry:提供交互式Shell
awesome_print(可选):在非交互模式下提供更优化的输出结果

工具使用

你可以使用“h”或“help”参数来查看所有可使用的命令:


➜  mac-wifi git:(master) ✗  ./mac-wifi h

 

mac-wifi version 1.1.0 -- Availablecommands are:

 

ci                      - connected to Internet(not just wifi on)?

co[nnect] network-name  - turns wifi on, connects to network-name

cy[cle]                 - turns wifi off, then on,preserving network selection

d[isconnect]            - disconnects from current network,does not turn off wifi

h[elp]                  - prints this help

i[nfo]                  - prints wifi-relatedinformation

lsp[referred]           - lists preferred (not necessarilyavailable) networks

lsa[vailable]           - lists available networks

n[etwork_name]          - name (SSID) of currently connectednetwork

on                      - turns wifi on

of[f]                   - turns wifi off

pa[ssword] network-name - shows passwordfor preferred network-name

q[uit]                  - exits this program(interactive shell mode only)

r[m] network-name       - removes network-name from thepreferred networks list

s[hell]                 - opens an interactive pryshell (command line only)

t[ill]                  - returns when the desiredInternet connection state is true. Options:

                          'on'/:on or 'off'/:off

                          wait interval, inseconds (optional, defaults to 0.5 seconds)

w[ifion]                - is the wifi on?

x[it]                   - exits this program(interactive shell mode only)

 

When in interactive shell mode:

    *use quotes for string parameters such as method names.

    *for pry commands, use prefix `%`.

该工具是基于Mac命令行工具实现的,但这并不是最佳的解决方案。本来我更愿意去使用macOS的系统调用,但现在的这种方法可以提高我开发脚本的速度。

更好的输出结果

如果你想在非交互模式下获取更形式化的输出结果(使用info命令),你需要安装awesome_print gem,否则你只能使用“pp”命令了(效果不够好)。因此我建议大家安装awesome_print,安装命令如下:

gem install awesome_print

查看底层操作系统命令和输出

如果你想查看macOS命令以及相应的输出,你可以设置环境变量MAC_WIFI_OPTS为-v(使用info命令进行演示):

export MAC_WIFI_OPTS=-v
./mac-wifi i

或者

MAC_WIFI_OPTS=-v  ./mac-wifi i

使用交互式Shell

如果当你尝试运行shell时程序崩溃的话,你可以尝试更新pry或pry-byebug。操作命令如下:

gem install pry
gem install pry-byebug

使用样例

单一命令行调用

mac-wifi i            # 打印输出WiFi信息 mac-wifi lsa          # 输出可用网络 mac-wifi lsp          # 输出优先选择的网络 mac-wifi co a-network a-password # 与一个需要密码的网络进行连接 mac-wifi co a-network            #与一个不需要密码的网络进行连接 mac-wifi t on && say "Internetconnected" # 当与一个网络连接成功之后播放消息

交互式Shell命令

# Print out wifi info: i
  # Cycle (off/on) the network then connectto the specified network not requiring a password > cycle; connect 'my-network'   # Cycle (off/on) the network, then connectto the same network not requiring a password > @name = network_name; cycle; connect@name
  # Cycle (off/on) the network then connectto the specified network using the specified password > cycle; connect 'my-network','my-password'  
> @i = i; puts "You are connectedon port #{@i[:port]} to #{@i[:network]} on IP address #{@i[:ip_address]}." You are connected on port en0 to .@ AISSUPER WiFi on IP address 172.27.145.225.
 
> puts "There are #{lsp.size}preferred networks." There are 341 preferred networks.
  # Delete all preferred networks whose namesbegin with "TOTTGUEST", the hard way: > lsp.grep(/^TOTTGUEST/).each { |n|rm(n) }
  # Delete all preferred networks whose namesbegin with "TOTTGUEST", the easy way. # rm can take multiple network names, butthey must be specified as separate parameters; thus the '*'. > rm(*lsp.grep(/^TOTTGUEST/))
  # Define a method to wait for the Internetconnection to be active. # (This functionality is included in the`till` command.) # Call it, then output celebration message: [17] pry(#<MacWifiView>)> defwait_for_internet; loop do; break if ci; sleep 0.5; end; end [18] pry(#<MacWifiView>)>wait_for_internet; puts "Connected!" Connected!
  # Same, but using a lambda instead of amethod so we can use a variable name # and not need to worry about method namecollision: @wait_for_internet = -> { loop do; breakif ci; sleep 0.5; end }
@wait_for_internet.() ; puts"Connected!" Connected!

许可证协议

本开源项目遵循 MIT 许可证

* 参考来源:macwifi,FB小编Alpha_h4ck编译