openssl 从脚本中传递前几行,然后从 stdin 中读取 [英] openssl pass first few lines from script then read from stdin

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

问题描述

我想运行 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

然后从那里获取来自 stdin 的输入.我对 shell 脚本和 openssl 工具包非常无知,而且我当然不知道如何简单地通过管道/重定向 stdin 来做到这一点,除非我尝试设置一个同时从stdin 本身,或诸如此类.

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

不做同样的事情

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 会话(我在这里的理解很弱)并且它没有将它的参数从 stdin 传递给它的内壳创建.

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:

首先,您有一个脚本可以回显您要发送的初始命令,然后从标准输入读取并写入标准输出.像这样(例如将其称为 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 回显您输入的输入,那么您输入的行将显示两次(这有点烦人).在这种情况下,read"的-s"参数将抑制第一行(例如用于输入密码)

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)

请注意,此解决方案类似于之前使用临时文件和 tail -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.

问题中给出的解决方案的问题是当'echo "t login ..."'命令完成时openssl命令的stdin被关闭,这通常会导致程序退出.使用此处给出的解决方案,管道将第一个脚本的 stdout 连接到第二个脚本的 stdin,输入 read 的所有内容都将传递给 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 从脚本中传递前几行,然后从 stdin 中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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