如何将密码传递给bash中的命令

jin*_*ang 5 passwords bash

我想编写一个 bash 脚本,该脚本将执行脚本中的一个命令,并且该命令需要读取一些内容作为密码。那么如何将密码传递给脚本中的命令呢?

$ota_gen -k $ota_key -i $1 -p $ota_tools $2 $ota_out_file
Run Code Online (Sandbox Code Playgroud)

ota_key是私钥,需要密码访问,那么怎么办呢?谢谢。

PS:感谢赫洛夫达尔的帮助。期待也许有什么可以帮助的。但不知道是否可以和bash脚本交互,比如如何将脚本中的参数传递给expect。

hlo*_*dal 3

一个非常常见的以非交互方式为程序提供正确输入(例如密码)的工具是expect工具。维基百科页面上给出了以下示例:

# Assume $remote_server, $my_user_id, $my_password, and $my_command were read in earlier 
# in the script.
# Open a telnet session to a remote server, and wait for a username prompt.
spawn telnet $remote_server
expect "username:"
# Send the username, and then wait for a password prompt.
send "$my_user_id\r"
expect "password:"
# Send the password, and then wait for a shell prompt.
send "$my_password\r"
expect "%"
# Send the prebuilt command, and then wait for another shell prompt.
send "$my_command\r"
expect "%"
# Capture the results of the command into a variable. This can be displayed, or written to disk.
set results $expect_out(buffer)
# Exit the telnet session, and wait for a special end-of-file character.
send "exit\r"
expect eof
Run Code Online (Sandbox Code Playgroud)