Alfred Workflow: ipaddress

Alfred 是什么?

Alfred 是一款面向 macOS 的效率工具,它主要用于通过键盘快速启动应用、查找文件/网络搜索、以及执行各种自动化任务,是 macOS 自带 Spotlight 的 “增强版”。

关于 ipaddress

功能

  • 查询本机 Wi-Fi/Ethernet IPv4 地址
  • 查询网络 IPv4 地址(包括直连链路与代理链路)

下载

ipaddress

使用

[!CAUTION]

首先,你需要购买 Alfred Powerpack 授权,否则无法使用 Workflow 功能。

  • Alfred 输入 ip 选择查询模式,包含本机模式与网络模式
  • Alfred 输入 ip local 直接进入本机模式,检测并输出本机 Wi-Fi/Ethernet IPv4 地址及其他全部 IPv4 地址
  • Alfred 输入 ip net 直接进入网络模式,同时检测直连链路与代理链路 IPv4,基于 Cloudflare 进行检测

界面

ip

ip net

[!NOTE]

这里是输入 ip net 回车后进入网络模式后显示为 net ,不是只需要输入 net

ip local

[!NOTE]

这里是输入 ip local 回车后进入网络模式后显示为 local ,不是只需要输入 local

Workflow 结构与配置

graph LR
    LF[List Filter: ip] --> AV[Arg & Vars: 读取关键字]
    AV --> C{Conditional: 判断 arg}
    C -->|net| SN[Script Filter: Get Network IPv4]
    C -->|local| SL[Script Filter: Get Local IPv4]
    SN --> CC[Copy to Clipboard]
    SL --> CC

1. List Filter:ip

  • Keyword: ip,唤起 Workflow。
  • Argument: Optional(考虑要直接通过 ip xxx 进入分支)。
  • Items:
    • Network Mode(net): argnet
    • Local Mode(local): arglocal
  • Match mode: Word matching - Any order (用于通过 ip net 直接进入 Network Mode

2. Arg & Vars:模式参数

  • Argument: 空
  • Variables:
    • mode: {query}

3. Conditional:路由到不同脚本

使用 Conditional 节点读取上一步的 mode

  • if {var:mode} is equal to net then net
  • else if {var:mode} is equal to local then local

4. Script Filter:Get Network IPv4

该节点负责同时检测直连链路与代理链路的外网 IPv4。脚本语言设置为 /bin/zsh,可使用以下脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/zsh

# direct IPv4 (disable Proxy)
direct_raw=$(env -u http_proxy -u https_proxy -u all_proxy \
curl -4 -s https://www.cloudflare.com/cdn-cgi/trace)
direct_ip=$(echo "$direct_raw" | grep "^ip=" | cut -d= -f2)
[[ -z "$direct_ip" ]] && direct_ip=""

# proxy IPv4
proxy_raw=$(curl -4 -s https://www.cloudflare.com/cdn-cgi/trace)
proxy_ip=$(echo "$proxy_raw" | grep "^ip=" | cut -d= -f2)
[[ -z "$proxy_ip" ]] && proxy_ip=""

cat <<EOF
{
"items": [
{
"title": "Direct IPv4: $direct_ip",
"subtitle": "prefer ipv4, disable proxy",
"arg": "$direct_ip"
},
{
"title": "Proxy IPv4: $proxy_ip",
"subtitle": "prefer proxy",
"arg": "$proxy_ip"
}
]
}
EOF

5. Script Filter:Get Local IPv4

本节点检测本机所有可用物理/虚拟网卡 IPv4 地址,脚本同样使用 /bin/zsh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/zsh

wifi_if=$(networksetup -listallhardwareports | awk '/Wi-Fi/{getline; print $2}')
wifi_ip=""
[[ -n "$wifi_if" ]] && wifi_ip=$(ipconfig getifaddr "$wifi_if" 2>/dev/null)

# Ethernet
ethernet_list=("${(@f)$(networksetup -listallhardwareports \
| awk '/Ethernet|LAN|Adapter/{getline; print $2}')}")

eth_ip=""
eth_if=""
for iface in "${ethernet_list[@]}"; do
candidate_ip=$(ipconfig getifaddr "$iface" 2>/dev/null)
if [[ -n "$candidate_ip" ]]; then
eth_ip="$candidate_ip"
eth_if="$iface"
break
fi
done

# all IPv4
all_ip=("${(@f)$(ifconfig | grep "inet " | awk '{print $2}' | grep -v "127.0.0.1")}")

echo '{ "items": ['

first=1
add_item() {
local ip=$1
local name=$2
if [[ -n "$ip" ]]; then
if [[ $first -eq 0 ]]; then echo ','; fi
echo "{\"title\": \"$name: $ip\", \"arg\": \"$ip\", \"subtitle\": \"Copy this IPv4\"}"
first=0
fi
}

add_item "$wifi_ip" "Wi-Fi ($wifi_if)"
add_item "$eth_ip" "Ethernet ($eth_if)"

for ip in "${all_ip[@]}"; do
add_item "$ip" "IPv4"
done

echo '] }'

6. Copy to Clipboard

默认即可