如何给一个在bash提示命令提供密码? [英] How to provide password to a command that prompts for one in bash?

查看:124
本文介绍了如何给一个在bash提示命令提供密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写将会执行命令,将提示用户输入密码在UNIX shell函数。我想硬code中的密码到脚本并将其提供给该命令。我试过管道密码进入这样的命令:

I'm writing a UNIX shell function that is going to execute a command that will prompt the user for a password. I want to hard-code the password into the script and provide it to the command. I've tried piping the password into the command like this:

function() {
    echo "password" | command
}

这可能不适用于某些命令的工作的命令可能会提示输入密码之前刷新输入缓冲区。

This may not work for some commands as the command may flush the input buffer before prompting for the password.

我也试过重定向标准输入到包含这样的密码的文件,但是,这并不工作,要么:

I've also tried redirecting standard input to a file containing the password like this, but that doesn't work either:

function() {
    echo "password" > pass.tmp
    command < pass.tmp
    rm pass.tmp
}

我知道一些命令允许密码作为参数提供,但我宁愿去通过标准输入。

I know that some commands allow for the password to be provided as an argument, but I'd rather go through standard input.

我正在寻找在bash管道密码到命令的快速和肮脏的方式。

I'm looking for a quick and dirty way of piping a password into a command in bash.

推荐答案

看看 autoexpect (体面教程的 这里 )。这是关于作为快速和肮脏的,你可以不诉诸弄虚作假得到的。

Take a look at autoexpect (decent tutorial HERE). It's about as quick-and-dirty as you can get without resorting to trickery.

这篇关于如何给一个在bash提示命令提供密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆