【问题标题】:How can I pass a password to bash script如何将密码传递给 bash 脚本
【发布时间】:2020-11-02 04:23:34
【问题描述】:

我想编写一个 bash 脚本来自动化特定过程。 它通过传递标识符启动分析 cms 工具。 传递标识符后,该工具要求输入用户密码。 该脚本应通读此类标识符的列表,并在每个转发的标识符之后输入用户密码。
以下简单示例显示了带有名为“eventDatePicker”的标识符的流程:

jmm@workstation:/opt/media/tools/bin$ ./cm old-viewtypes-usage -u admin -s "/test/" -t "/home/media/transfer" -vt eventDatePicker

password:

到目前为止,这是我创建的 bash 脚本,但我不知道如何实现传递用户密码的函数:

#!/bin/bash
# list with identifiers
input="/opt/media/tools/bin/technical_identifier"
#path to analyzing tool
cd /opt/media/tools/bin || exit
while IFS= read -r line
do
       ./cm old-viewtypes-usage -u admin -s "/test/" -t "/home/media/transfer" -vt "$line"
        # command for passing the user password 
       
done < "$input"

我使用readexpect 进行了尝试,但没有成功。 我很乐意为您提供任何帮助。

【问题讨论】:

  • 答案取决于./cm 命令的内部结构。例如,如果它从标准输入读取密码,则以下内容将起作用:./cm your_args &lt;&lt;&lt;"$password" 其中 shell 变量 password 包含密码。 (当然,对于 your_args,请替换您的参数列表。)
  • 查看expect
  • 你可以用这个cm来读取密码:while IFS= read -r line do ./cm your_args &lt;&lt;EOF # command for passing the user password EOF done &lt; "$input"

标签: linux bash shell passwords parameter-passing


【解决方案1】:

您可能想学习 Tcl 的“期望”方言。从运行“autoexpect”开始,然后更改输出脚本以获取参数并发送您的密码。

这确实是编写交互式脚本的唯一明智的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 2023-02-02
    • 2019-12-20
    • 1970-01-01
    • 2012-01-23
    • 2015-12-19
    相关资源
    最近更新 更多