【问题标题】:Use perl to replace some string in file with complicated password使用 perl 用复杂的密码替换文件中的某些字符串
【发布时间】:2020-06-11 14:48:25
【问题描述】:

我想在 bash 脚本中使用 perl 将文本文件中的某些字符串替换为可能包含各种特殊字符(密码)的其他字符串。包含特殊字符的变量来自环境,所以我不知道。

例如

# pw comes from the environment and I cannot be sure about it's content
pw='%$&/|some!\!smart%(]password' 
pw=${pw//:/\\:}
perl -p -e "s:PASSWORD:$pw:" <<< "my pw is: PASSWORD" # this would come from a text file
# yields: my pw is: %PASSWORD/|some!!smart%(]password

这里我使用: 作为分隔符并转义之前可能出现的情况,这应该可以防止一些错误。但是执行此操作确实表明这甚至没有按预期远程工作。 bash 扩展仍然在弄乱密码。

现在我的问题是:如何安全地获取一些环境变量并将其放置在其他地方?什么可能是更好的方法?我当然可以替换和转义未知变量中的更多字符,但我怎么能确定这是否足够?

【问题讨论】:

    标签: perl passwords


    【解决方案1】:

    使用正确的ENV special variable:

    pw='%$&/|some!\!smart%(]password' 
    export pw=${pw//:/\\:}
    perl -pe 's:PASSWORD:$ENV{pw}:' <<< "my pw is: PASSWORD" 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-17
      • 2019-08-27
      • 1970-01-01
      • 2017-08-30
      • 2016-12-03
      • 2016-08-02
      • 2020-07-23
      • 1970-01-01
      相关资源
      最近更新 更多