regex - 使用正则表达式提取 OTP key

标签 regex bash sed google-authenticator

我有一个脚本,可以在 Linux 系统上通过 SSH key 和 OTP 启用 OTP/Google Auth。启用 OTP 后,我会向用户发送一 strip 有 otpauth URL 的短信。我需要从 otpauth URL 中提取(通常为 16 位数字 key )。

如果他们想要可见的 QR 代码,URL 示例如下:

https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5a0a6b0a795bdbaa6a1fbb6bab8" rel="noreferrer noopener nofollow">[email protected]</a>%3Fsecret%3DWFHGHUFAUYXZFA44%26issuer%3DCOMPANY

如果他们想要一个 otpauth url,他们可以点击导入到 token /密码管理器中,我使用以下内容:

otpauth://totp/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1c4c2d4c3f1d9dec2c59fd2dedc" rel="noreferrer noopener nofollow">[email protected]</a>%3Fsecret%3DWFHGHUFAUYXZFA44%26issuer%3DCOMPANY

如何从 URL 中提取 key ?

最佳答案

使用grep -oP:

url='https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e297918790a28a8d9196cc818d8f" rel="noreferrer noopener nofollow">[email protected]</a>%3Fsecret%3DWFHGHUFAUYXZFA44%26issuer%3DCOMPANY'

grep -ioP '[?&]chl=.+?%3Fsecret%3D\K[^%]+' <<< "$url"
WFHGHUFAUYXZFA44

关于regex - 使用正则表达式提取 OTP key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30874780/

相关文章:

linux - 使用 UNIX BASH 将文件复制到其他虚拟机

匹配短语的正则表达式,除非它已经加前缀

用于 javascript 的正则表达式来计算单词(不包括数字)

linux - 导出变量,这些变量是 shell 中的变量值

linux - 如何在远程机器上运行多个脚本

linux - sed 不像其他 linux 命令那样显示退出状态

regex - 使用 sed 提取 git diff 的插入和删除次数

python - 正则表达式拆分字符串并去除重复字符

regex - 如何在 golang 中使用正则表达式获取 url 模式?

匹配 5'2"、 6', and 2' - 4", but not 3' '(两个撇号)的正则表达式