描述
树莓派等小设备的IP地址总需要去路由器查看,有点麻烦,能不能让她们自己报告?
利用板载LED将IP的摩尔斯电码闪出来就可以了!
写了一个小脚本
root@raspberrypi:/home/tkhw3061/# cat showip.sh
#!/bin/bash
function showmorsecode()
{
dottime=0.2 ### dot
dashtime=`echo $dottime \* 3 | bc` ### dash = dot * 3
spacetime=`echo $dottime \* 2 | bc` ### spacetime = dot * 2
spacetimebetweenwords=`echo $dottime \* 5 | bc` ### spacetimebetwteenwords = dot * 5 //5=7-2
morsecode=$1
for ((j=0; j<${#morsecode}; j++));
do
asc=${morsecode:$j:1}
if [[ $asc == "-" ]];
then
echo -n "-"
echo 1 > /sys/class/leds/led0/brightness
sleep $dashtime
echo 0 > /sys/class/leds/led0/brightness
sleep $spacetime
else
echo -n "."
echo 1 > /sys/class/leds/led0/brightness
sleep $dottime
echo 0 > /sys/class/leds/led0/brightness
sleep $spacetime
fi
done
sleep $spacetimebetweenwords
}
function showdigit()
{
case $1 in
0) showmorsecode "-----" ;;
1) showmorsecode ".----" ;;
2) showmorsecode "..---" ;;
3) showmorsecode "...--" ;;
4) showmorsecode "....-" ;;
5) showmorsecode "....." ;;
6) showmorsecode "-...." ;;
7) showmorsecode "--..." ;;
8) showmorsecode "---.." ;;
9) showmorsecode "----." ;;
A) showmorsecode ".-" ;;
B) showmorsecode "-..." ;;
C) showmorsecode "-.-." ;;
D) showmorsecode "-.." ;;
E) showmorsecode "." ;;
F) showmorsecode "..-." ;;
G) showmorsecode "--." ;;
H) showmorsecode "...." ;;
I) showmorsecode ".." ;;
J) showmorsecode ".---" ;;
K) showmorsecode "-.-" ;;
L) showmorsecode ".-.." ;;
M) showmorsecode "--" ;;
N) showmorsecode "-." ;;
O) showmorsecode "---" ;;
P) showmorsecode ".--." ;;
Q) showmorsecode "--.-" ;;
R) showmorsecode ".-." ;;
S) showmorsecode "..." ;;
T) showmorsecode "-" ;;
U) showmorsecode "..-" ;;
V) showmorsecode "...-" ;;
W) showmorsecode ".--" ;;
X) showmorsecode "-..-" ;;
Y) showmorsecode "-.--" ;;
Z) showmorsecode "--.." ;;
esac
echo " "
}
function showip()
{
sshconnections=`ss|grep -i ssh|wc -l`
if [ $sshconnections == "0" ];
then
fullip=`hostname -I` #### fullip=[192.168.10.170 ]
lastnumberspace=${fullip##*.} #### lastnumberspace=[170 ]
lastnumber=${lastnumberspace%% *} #### lastnumber=[170]
for ((i=0; i<${#lastnumber}; i++));
do
sc=${lastnumber:$i:1}
showdigit $sc
done
fi
}
typeset -u keyword
keyword=$1
keywordsize=${#keyword}
if [[ $keywordsize > 0 ]];
then
for ((i=0; i<${#keyword}; i++));
do
ksc=${keyword:$i:1}
echo $ksc
showdigit $ksc
done
else
showip
fi
设置成可执行
root@raspberrypi:/home/tkhw3061/# chmod a+x /home/tkhw3061/showip.sh
设置成每分钟执行一次
root@raspberrypi:/home/tkhw3061/# crontab -e
# m h dom mon dow command
* * * * * /home/tkhw3061/showip.sh
这个脚本会在开机后自动执行, 每分钟执行一次,报告ip地址最后一个数字的摩尔斯电码,一般局域网中前3个数字都是一样的, 直到有ssh进来为止。
我在zero上测试通过了