根据标题中存储的域生成 URL 字段

根据标题中存储的域生成 URL 字段

我的 kdbx 数据库中有很多条目,其中的标题是密码所属的域。在大多数(但不是全部)条目中,URL 字段为空:

| Title           | Username | Password | URL                       | ... |
|-----------------+----------+----------+---------------------------+-----|
| example.com     |          | ***      |                           | ... |
| foo.example.net | foo      | ***      |                           | ... |
| bar.example.net | bar      | ***      |                           | ... |
| example.org     | bla      | ***      | https://example.org/login | ... |
| ...             | ...      | ...      | ...                       | ... |

如何根据标题字段填充 URL 字段,以便数据库看起来像下面的数据库?

| Title           | Username | Password | URL                       | ... |
|-----------------+----------+----------+---------------------------+-----|
| example.com     |          | ***      | https://example.com/      | ... |
| foo.example.net | foo      | ***      | https://foo.example.net   | ... |
| bar.example.net | bar      | ***      | https://bar.example.net   | ... |
| example.org     | bla      | ***      | https://example.org/login | ... |
| ...             | ...      | ...      | ...                       | ... |

答案1

此 bash 脚本使用 KeepassXC(使用 v2.4.3 测试)完成该作业。用法为./fill_urls.sh DATABASE

数据库必须用密码保护,而不是用密钥文件保护。

#!/bin/bash

do_keepass () {
    echo $password | keepassxc-cli $@ --quiet
}

is_group () {
    local path=$1
    [[ $path =~ /$ ]]
}

all_entry_paths () {
    local root=$1
    is_group $root || (echo "$root is not a group"; exit 1)
    do_keepass ls $db $root | while read line; do
        local path="${root}${line}"
        if is_group $path; then
            all_entry_paths $path
        else
            echo $path
        fi
    done
}

is_domain () {
    local string=$1
    [[ $string =~ [a-zA-Z0-9-]+\.[a-zA-Z]{2,} ]]
}

has_url () {
    local path=$1
    [ ! -z $(do_keepass show $db $path --attributes=URL) ]
}

make_url () {
    local title=$1
    echo "https://${title}"
}

main () {
    all_entry_paths / | while read path; do
        ! is_group $path || (echo "$path is not an entry"; exit 1)
        local title=$(basename $path)
        if is_domain $title && ! has_url $path; then
            local new_url=$(make_url $title)
            echo "Setting URL of $path to $new_url" > /dev/stderr
            do_keepass edit --url $new_url $db $path
        fi
    done
}

echo -n "Password: " > /dev/stderr
read -s password
echo > /dev/stderr
db=$1

main

相关内容