Skip to content

Commit 72d602c

Browse files
committed
avahi
1 parent 3f1163b commit 72d602c

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

02-fdiwa.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Fixing Dynamic IP with Avahi
2+
3+
macbook貌似没法通过wifi开启移动热点,所以我用我的手机开了移动热点,然后通过串口将板子连接到macbook,然后板子自动连接手机热点。但问题出现,手机每次开热点,会给连接到热点的设备重新分配逻辑ip地址,就导致我每次重新开热点,不知道licheepi4a的ip是多少,自然也没法ssh到板子。所以我上网查资料,找到了一种方法。
4+
5+
就是在macbook上查看自己的ip地址,比如是192.168.8.60,那就从192.168.8.1到192.168.8.254都ping一遍,除了macbook和网关,只剩下两个可以ping通,那就是板子和手机。于是我把我的需求告诉LLM,它给我写了一个shell脚本:
6+
7+
```shell
8+
#!/bin/bash
9+
10+
# 获取 en0 接口的 IP 地址(如 192.168.8.60)
11+
en0_ip=$(ifconfig en0 | grep 'inet ' | awk '{print $2}')
12+
if [ -z "$en0_ip" ]; then
13+
echo "Error: Could not find en0 interface IP address."
14+
exit 1
15+
fi
16+
17+
# 提取网络前缀(如 192.168.8)
18+
network_prefix=$(echo $en0_ip | cut -d'.' -f1-3)
19+
gateway_ip="$network_prefix.1" # 假设网关是 x.x.x.1
20+
21+
echo "Your IP: $en0_ip"
22+
echo "Gateway IP: $gateway_ip"
23+
echo "Scanning network: $network_prefix.1-254..."
24+
25+
# 扫描网络并找出活跃设备
26+
active_ips=()
27+
for i in {1..254}; do
28+
ip="$network_prefix.$i"
29+
30+
# 跳过本机 IP 和网关
31+
if [ "$ip" == "$en0_ip" ] || [ "$ip" == "$gateway_ip" ]; then
32+
continue
33+
fi
34+
35+
# Ping 检测(快速 ping,超时 1 秒)
36+
if ping -c 1 -W 1 "$ip" &> /dev/null; then
37+
echo "Found active device: $ip"
38+
active_ips+=("$ip")
39+
fi
40+
done
41+
42+
# 输出结果
43+
echo ""
44+
echo "Scan completed."
45+
if [ ${#active_ips[@]} -eq 0 ]; then
46+
echo "No other active devices found in the network."
47+
elif [ ${#active_ips[@]} -eq 1 ]; then
48+
echo "The active device is likely your LicheePi 4A: ${active_ips[0]}"
49+
echo "You can try to SSH to it:"
50+
echo " ssh username@${active_ips[0]}"
51+
else
52+
echo "Multiple active devices found:"
53+
for ip in "${active_ips[@]}"; do
54+
echo " $ip"
55+
done
56+
echo "One of these is likely your LicheePi 4A. Try to SSH to each."
57+
fi
58+
```
59+
60+
尝试过后发现,的确可行,但是这样有点慢啊,整个过程需要四分多钟,然后我就给LLM说了,它就给了我一个更快的方法,的确可行:
61+
62+
```shell
63+
#!/bin/bash
64+
65+
# 获取本机 IP 和网关
66+
en0_ip=$(ifconfig en0 | grep 'inet ' | awk '{print $2}')
67+
network_prefix=$(echo $en0_ip | cut -d'.' -f1-3)
68+
gateway_ip="$network_prefix.1"
69+
70+
echo "Scanning $network_prefix.0/24 (parallel ping, wait 2-5 seconds)..."
71+
72+
# 并行 ping 所有 IP,超时 0.2 秒
73+
seq 1 254 | xargs -P 50 -I {} bash -c "
74+
ip='$network_prefix.{}'
75+
[ \"\$ip\" == \"$en0_ip\" ] || [ \"\$ip\" == \"$gateway_ip\" ] && exit
76+
ping -c 1 -W 0.2 \"\$ip\" &>/dev/null && echo \"Active: \$ip\"
77+
"
78+
```
79+
80+
但这还有一个问题,就是每次查到新的ip之后,我还要去改~/.ssh/config中的ip,那咋办呢?LLM告诉我了一个很好的方法:
81+
82+
```shell
83+
# 在licheepi4a上操作一下shell命令
84+
85+
# 安装avahi
86+
sudo dnf install avahi
87+
88+
# 找到[server]下的host-name,改为lp4a
89+
vi /etc/avahi/avahi-daemon.conf
90+
91+
# 设置成开机自启
92+
sudo systemctl enable avahi-daemon
93+
94+
# 启动服务
95+
sudo systemctl start avahi-daemon
96+
97+
# 查看服务是否启动
98+
sudo systemctl status avahi-daemon
99+
```
100+
101+
然后把~/.ssh/config中的ip改成lp4a.local就行了

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
[00-LicheePi 4A Startup](00-startup.md)
44

55
[01-oeAware](01-oeAware.md)
6+
7+
[02-Fixing Dynamic IP with Avahi](02-fdiwa.md)

0 commit comments

Comments
 (0)