[Share Experiences] 通过shell脚本简易实现win动态锁
Tofloor
poster avatar
HuaGCS
deepin
2024-01-17 09:06
Author

前景

因办公场地要求人离开必须锁屏,但我总是会忘,所以参照win的动态锁通过脚本来实现了一个简易版

环境

  • 20.9

实现

脚本

#!/bin/bash

# 获取脚本所在的绝对路径
script_dir="$(dirname "$(realpath "$0")")"

# 获取当前日期作为日志文件名
get_log_file() {
    echo "$log_path/$(date '+%Y-%m-%d').log"
}

# 转换相对路径为基于脚本所在目录的绝对路径
convert_to_absolute_path() {
    local path=$1
    # 检查是否已经是绝对路径
    if [[ "$path" = /* ]]; then
        echo "$path"
    else
        echo "$(realpath -m "$script_dir/$path")"
    fi
}

# 日志记录
log_event() {
    echo "$1"
    if [[ $enable_logging == "true" ]]; then
        local log_file=$(get_log_file)
        echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$log_file"
    fi
}

# 获取状态
check_connection() {
    local device_mac=$1
    bluetoothctl info "$device_mac" | grep "Connected:" | awk '{print $2}'
}

# 构建配置文件的完整路径
config_file="$script_dir/bluetooth_device.conf"
if [ -f "$config_file" ]; then
    echo "尝试读取配置文件: $config_file"
    source "$config_file"
    echo "MAC地址: $mac_address"
    echo "设备名称: $device_name"
    log_path=$(convert_to_absolute_path "$log_path")
    echo "日志路径: $log_path"
else
    echo "配置文件不存在: $config_file"
    exit 1
fi

# 创建日志目录
if [ ! -d "$log_path" ]; then
    mkdir -p "$log_path"
fi

# 信号处理
trap 'log_event "脚本被终止"; exit 0' SIGINT SIGTERM

# 主循环
connected="no"
retry_interval=5
while true; do
    if [[ $(systemctl is-active bluetooth) != "active" ]]; then
        log_event "蓝牙服务未激活。"
        connected="no"
        sleep $retry_interval
        continue
    fi

    status=$(check_connection $mac_address)
    if [[ $? -ne 0 ]]; then
        sleep $retry_interval
        continue
    fi

    if [[ "$status" == "yes" && "$connected" == "no" ]]; then
        log_event "蓝牙设备 $device_name 已连接。"
        connected="yes"
        retry_interval=5
    elif [[ "$status" == "no" && "$connected" == "yes" ]]; then
        log_event "蓝牙设备 $device_name 已断开连接。"
        loginctl lock-session
        connected="no"
        retry_interval=30
    fi

    sleep $retry_interval
done


配置文件

  • bluetooth_device.conf
# 获取mac地址及名称:bluetoothctl devices
# 配置蓝牙的mac地址
mac_address=""
# 配置蓝牙的名称
device_name=""
# 配置日志的存储位置 相对路径为脚本所在目录
log_path="./log"
# 是否输出日志到文件 true/false 默认为false
enable_logging="false"

使用

赋予对应执行权限后,直接在终端运行

注意

运行脚本的终端不可关闭,建议在终端雷神模式中运行,或者通过 screen 等其他方式运行

Reply Favorite View the author
All Replies
蔡EEPIN
deepin
2024-01-17 09:49
#1

人走锁屏的习惯还是养成比较好,不然早晚泄漏资料

Reply View the author
jjcui8595
Moderator
2024-01-17 10:09
#2

super+ L即可锁屏

Reply View the author
liwl
deepin
2024-01-17 10:19
#3
手动锁屏的习惯要养成
Reply View the author
liwl
deepin
2024-01-17 10:19
#4
不过脚本实现,更高大上,更专业
Reply View the author
雪域一线天
deepin
2024-01-17 10:23
#5

v23要用dde-lock

Reply View the author
安洛
deepin
2024-01-17 11:36
#6

有一个类似的项目:https://github.com/LethalEthan/Bluetooth-Unlock

实现的是蓝牙离开自动锁屏、接近自动解锁。

Reply View the author
HuaGCS
deepin
2024-01-17 14:06
#7
蔡EEPIN

人走锁屏的习惯还是养成比较好,不然早晚泄漏资料

有这个习惯 但有时候太忙就忘了 算是个保险

Reply View the author
HuaGCS
deepin
2024-01-17 14:07
#8
雪域一线天

v23要用dde-lock

v23 我还没有实装 毕竟是办公环境 还是多少稳定一些

Reply View the author
HuaGCS
deepin
2024-01-17 14:08
#9
liwl
手动锁屏的习惯要养成

算是个保险吧 有时候是真忘了

Reply View the author
HuaGCS
deepin
2024-01-17 14:09
#10
安洛

有一个类似的项目:https://github.com/LethalEthan/Bluetooth-Unlock

实现的是蓝牙离开自动锁屏、接近自动解锁。

我去康一康 不过我这不需要解锁 只需要锁屏

Reply View the author
HuaGCS
deepin
2024-01-17 14:09
#11
jjcui8595

super+ L即可锁屏

主要是有时候忙的总忘

Reply View the author
jjcui8595
Moderator
2024-01-17 14:22
#12
HuaGCS

主要是有时候忙的总忘

电源管理可以设定自动锁屏

Reply View the author
HuaGCS
deepin
2024-01-17 14:29
#13
jjcui8595

电源管理可以设定自动锁屏

电源管理最短时间也需要1分钟 这个时间达不到我的需求

Reply View the author
jjcui8595
Moderator
2024-01-17 15:33
#14
HuaGCS

电源管理最短时间也需要1分钟 这个时间达不到我的需求

like

Reply View the author
晚秋(lateautumn)
Moderator
2024-01-17 16:46
#15

谢谢分享。

Reply View the author