Openssl skips the first few lines of a script, then reads from stdin

I want to start openssl and start with the following commands sent to the server:

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

Then from there we take the input from stdin . I really do not understand shell scripts and openssl tools, and of course I donโ€™t see how to do this simply with piping / redirecting stdin , if, perhaps, I did not try to configure a file that I was drawing from stdin or such at the same time.

I'm not quite sure what openssl technologies use to read its input. For example, the following:

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

Doesn't do the same thing

 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) 

I assume that openssl opens a new shell session (I am weak in my understanding here) and it does not pass its arguments from stdin to the inner shell that it creates.

0
source share
5 answers

I would recommend dividing the problem into two scenarios:

Firstly, you have one script that resonates with the original commands you want to send, and then reads from stdin and writes to stdout. Like this (call it script1.sh for example):

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

The second script then simply binds the arguments to openssl, so you do not need to type them (for example, call this script 2.sh. Note that as with script1.sh above, you must have #! / Bin / bash in the first line to tell the OS that this is a bash script.

then you can simply enter:

 script1.sh | script2.sh 

and you will get the first two lines passed to openssl, and then everything you type will be passed after that. If you want to finish several commands, you can add them after the while loop in script1.sh.

You end it all with Ctrl-D

If openssl throws away the input you entered, you will get lines that you enter twice (which is a little annoying). In this case, the argument โ€œ-sโ€ for โ€œreadingโ€ suppresses the first line (useful for entering passwords, for example)

Note that this solution is similar to the solution proposed earlier with a temporary file and -f tail, but it avoids the need for a temporary file and everything is done on one line.

The problem with the solution asked in the question is that the stdin command for the openssl command closes when the "echo" t login ... "'command completes, and this will cause programs to exit the system. With the solution given here, the pipe connects stdout the first script with stdin of the second, and everything entered in read will be passed to openssl

+2
source

You can modify your script to write commands to a file, and then use tee -a to redirect stdin to the same file. Let me show you an example:

 jweyrich@pharao :~$ echo "command1" > cmds jweyrich@pharao :~$ tee -a cmds > /dev/null command2 command3 ^C 

At the same time, I ran tail -f cmds in another tty:

 jweyrich@pharao :~$ tail -f cmds command1 command2 command3 

This will turn this file into a single source that you must read and process.

+1
source

A basic SSL / TLS connection with an SSL-enabled IMAP server can be established via s_client :

 openssl s_client -connect imapserver.example.com:143 -starttls imap 

Pay attention to the final -starttls imap : openssl "knows" how to tell the IMAP server that it would like to switch from a regular text connection (as you would with telnet) to SSL security.

After that, the openssl task is executed, and you need to output the proper IMAP to the server, including authentication!

0
source

I would like to add that you can use Nick's solution as a single-line script:

 $ sh -c 'echo "first command"; echo "second command"; while read x; do echo "$x"; done' | whatever 
0
source

None of these solutions return stdin control to the user. This should pass the first command and the second openssl command, and then read stdin:

 cat <<EOF - | openssl .... first command second command EOF 
0
source

Source: https://habr.com/ru/post/979127/


All Articles