djechlin djechlin - 1 year ago 110
Bash Question

openssl pass first few lines from script then read from stdin

I want to run

and have it begin with the following commands sent to the server:

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

Then from there take input from
. 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
unless perhaps I tried setting up a file that was simultaneously drawing from
itself, or such.

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

$ echo "t login password" | openssl s_client -connect

Does not do the same thing as

openssl s_client -connect
# openssl dialogue opens...
C: t login password
S: t NO [AUTHENTICATIONFAILED] Incorrect username or password. (#YSH002)

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

Answer Source

I'd recommend splitting the problem into two scripts:

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 for instance):

echo "first command"
echo "second command"
while read x
  echo "$x"

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

then you can just type: |

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

You terminate the whole thing with Ctrl-D

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)

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.

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download