OpenSSL的传递从剧本的前几行,然后从标准输入读取 [英] openssl pass first few lines from script then read from stdin

查看:122
本文介绍了OpenSSL的传递从剧本的前几行,然后从标准输入读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要运行的OpenSSL 并用它向服务器发送下面的命令开始:

I want to run openssl and have it begin with the following commands sent to the server:

t authenticate <dynamically generated base64 string from calling script>
t select Inbox

然后从那里从标准输入输入。我在shell脚本和OpenSSL工具包很无知,我肯定看不到如何做到这一点简单地用管道/重定向标准输入除非也许我试图建立一个文件被同时从标准输入绘画本身,​​或者这样的。

Then from there take input from stdin. I'm very ignorant in shell scripting and the openssl toolkit, and I certainly don't see how to do this simply with piping / redirecting stdin unless perhaps I tried setting up a file that was simultaneously drawing from stdin itself, or such.

我不完全相信这些技术的OpenSSL用来读取它的输入。对于下面的例子:

I'm not exactly sure the technologies openssl uses to read its input. For example the following:

$ echo "t login testacct@yahoo.com password" | openssl s_client -connect imap.mail.yahoo.com:993

不会做同样的事情,

Does not do the same thing as

openssl s_client -connect imap.mail.yahoo.com:993
# openssl dialogue opens...
C: t login testacct@yahoo.com password
S: t NO [AUTHENTICATIONFAILED] Incorrect username or password. (#YSH002)

我想象的OpenSSL 是打开一个新的shell会话(我在这儿我的理解弱),并没有从标准输入传递它的参数来的内壳它创建。

I imagine openssl is opening a new shell session (I'm weak in my understanding here) and it does not pass its arguments from stdin to the inner shell it creates.

推荐答案

我会建议分裂问题分成两个脚本:

I'd recommend splitting the problem into two scripts:

首先,你有一个脚本,呼应你要发送,然后从标准输入读取和写入stdout最初的命令。像这样(称之为script1.sh举例):

Firstly you have one script that echoes the initial commands that you want to send and then reads from stdin and writes to stdout. Like this (call it script1.sh for instance):

#!/bin/bash
echo "first command"
echo "second command"
while read x
do
  echo "$x"
done

第二个脚本然后就捆绑了参数OpenSSL的,所以你不必继续键入他们(这个script2.sh的实例。请注意,上面script1.sh,你应该有#!/ bin中/在第一行的bash告诉,这是一个bash脚本操作系统。

The second script then just bundles the arguments to openssl so you don't have to keep typing them (call this script2.sh for instance. Note that as with script1.sh above, you should have the #!/bin/bash on the first line to tell the OS that it's a bash script.

那么你可以只需键入:

script1.sh | script2.sh

你会得到第一个两行传递到OpenSSL和然后键入一切都将后获得通过。如果你想总是带着几个命令完成就可以在script1.sh while循环之后添加它们。

and you'll get the first two lines passed to openssl and then everything you type will get passed after that. If you want to always finish with a few commands you can add them after the while loop in script1.sh.

您终止整个事情用Ctrl-D

You terminate the whole thing with Ctrl-D

如果OpenSSL的呼应您键入那么你会得到你所示输入线的两倍(这是一个有点刺激性)输入。在这种情况下,-s参数设置为读,将坐席preSS的第一行(用于例如输入密码有用)

If openssl echoes the input you type then you will get the lines you type in shown twice (which is a bit irritating). In that case the "-s" argument to "read" will suppress the first line (useful for typing passwords for instance)

请注意,这个解决方案是类似于溶液较早用临时文​​件和尾-f建议,但避免了一个临时文件的需要,一切都在一个单一的线路进行。

Note that this solution is similar to the solution suggested earlier with the temporary file and the tail -f but it avoids the need for a temporary file and everything is done in a single line.

在的问题给出的解决方案的问题是,当回声T登录...'命令结束,这通常会导致程序退出标准输入到openssl命令关闭。这里给出解决方案中的管道连接第一脚本的第二和一切输入到读取标准输入标准输出将得到转嫁到OpenSSL的

The problem with the solution given in the question is that stdin to the openssl command is closed when the 'echo "t login ..."' command finishes and this will generally cause programs to exit. With the solution given here the pipe connects the stdout of the first script to the stdin of the second and everything typed into read will get passed on to openssl

这篇关于OpenSSL的传递从剧本的前几行,然后从标准输入读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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