管道 openssl 命令 [英] Pipeline openssl commands

查看:103
本文介绍了管道 openssl 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 shell 脚本,它以设定的时间间隔登录到 Gmail,然后删除一些文件.我可以通过终端使用 openssl 但不能通过 shell script 文件.

I'm trying to create a shell script which logs in to Gmail at a set interval of time and then delete some files. I'm able to do it via terminal using openssl but not via shell script file.

这是我的shell文件

Here is my shell file

openssl s_client -connect imap.gmail.com:993 -crlf
a001 login USER_NAME PASSWORD
a002 SELECT [Gmail]/Trash
a003 STORE 1:* +FLAGS (\Deleted)
a004 EXPUNGE
a005 logout

现在这里的问题是在第一个命令执行后,shell 连接到服务器并且第二个命令没有被执行(这是有道理的).

Now the problem here is after first command executes shell connects to the server and second command does not get executed (which makes sense).

如何为连接的服务器制作 shell 脚本运行命令?

How can i make shell script run command for connected server?

PS:我认为管道在这里不是正确的词,但我不知道这里将使用的行话,所以请随时编辑问题.

PS: I think pipeline is not the correct word here, but i'm not aware of the jargon that will be used here so please feel free to edit the question.

推荐答案

这里的混淆在于如何处理 shell 和 openssl 程序的输入.当您以交互方式执行此操作时,shell 正在从终端(即您)读取命令,并且 openssl 从终端读取,因此您输入的任何内容都会转到当时正在侦听的任何内容:>openssl ... 命令被 shell 读取,然后 openssl 从终端开始读取,所以当你输入 a001 login ... 时,即被 openssl 程序读取并发送到远程服务器.

The confusion here is in how input to the shell and the openssl program are handled. When you do this interactively, the shell is reading commands from the terminal (i.e. you), and openssl also reads from the terminal, so whatever you type goes to whatever' listening at the time: the openssl ... command is read by the shell, and then openssl starts reading from the terminal, so when you type a001 login ..., that gets read by the openssl program and sent to the remote server.

在脚本中,情况有所不同.shell 从脚本中读取命令,但其他程序(如 openssl)从所谓的标准输入(或 stdin)中读取,这通常仍然是终端.所以 shell 读取 openssl ... 行,运行 openssl 程序,它试图从你的终端读取输入.如果当 openssl 退出时,shell 将读取 a001 login ... 并尝试将其作为 shell 命令执行.

In a script, it's different. The shell reads commands from the script, but other programs (like openssl) read from what's called standard input (or stdin), which is usually still the terminal. So the shell reads the line openssl ..., runs the openssl program, which tries to read input from your terminal. If and when openssl exits, the shell will then read a001 login ... and try to execute it as a shell command.

您可以做的是将 a001 login ... 作为输入提供给 openssl 程序.最简单的方法通常是使用 here-doument,由 <<somedelimiter 引入:

What you could to do is supply that a001 login ... as input to the openssl program. The easiest way to do that is usually with a here-doument, introduced by <<somedelimiter:

openssl s_client -connect imap.gmail.com:993 -crlf <<EOF
a001 login USER_NAME PASSWORD
EOF

这实质上是告诉 shell运行这个 openssl ... 命令,并将以下几行(直到 'EOF')输入到它的标准输入中.

That essentially tells the shell "run this openssl ... command, and feed the following lines (up to 'EOF') into its stdin".

但这并不能解决更深层次的问题,因为它所做的只是发送 login 命令,然后用完输入,然后关闭连接.您必须在 here-document 中包含其他命令才能使其实际执行任何操作:

But that doesn't solve a deeper problem, because all it does is send the login command, then run out of input, and close the connection. You'd have to include additional commands within the here-document to get it to actually do anything:

openssl s_client -connect imap.gmail.com:993 -crlf <<EOF
a001 login USER_NAME PASSWORD
a001 STATUS INBOX (MESSAGES UNSEEN RECENT)
a001 LIST "INBOX" "*"
EOF

...然后从中捕获输出(可能通过将 >sometempfile 添加到命令行),并解析输出以找出服务器上的内容.但是您可能真正想要做的是能够与服务器交互(例如获取新消息列表然后FETCH它们),而这不会真的允许 - 您的脚本发送一个固定的命令列表,而不是一次发送一个命令,查看结果,并根据它返回的内容发送更多命令.这确实需要除 shell 脚本和 openssl 之外的其他东西——具有不错的 IMAP 库的东西(如评论中建议的三元组).

...and then capture the output from it (probably by adding >sometempfile to the command line), and parse through the output to figure out what's on the server. But what you probably really want to do is to be able to interact with the server (e.g. get a list of new messages and then FETCH them), and this doesn't really allow that -- your script is sending a fixed command list, rather than sending commands one at a time, looking at the results, and sending more commands based on what it gets back. And that really requires something other than a shell script and openssl -- something with a decent IMAP library (as triplee suggested in a comment).

这篇关于管道 openssl 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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