Run commands in openssl via batch

I am trying to set up a custom script to monitor runtime and run this openssl command, and I have arguments that I pass to it.

openssl s_client -CAfile C:\apcerts\certs\ -quiet -connect ${HOST}:${PORT} > ${TMPF} 2>&1 < EOF <TF80DOC><XPLN/></TF80DOC> EOF if (Select-String "Update Level" ${TMPF} > /dev/null) { exitstatus=$STATE_OK Select-String "Update Level" ${TMPF} | sort | uniq} elseif (Select-String "Regulatory" ${TMPF} > /dev/null) { exitstatus=$STATE_OK Select-String "Regulatory" ${TMPF} | sort | uniq} else{ echo `date` >> /tmp/caught_errs.out cat ${TMPF} >> /tmp/caught_errs.out echo " " >> /tmp/caught_errs.out exitstatus=$STATE_CRITICAL } rm -f ${TMPF} 2> /dev/null exit ${exitstatus} 

I want the variables $ {host}: $ {port} to remain empty, and I want to have an argument in which I manually put the information, and the fields are filled with this information.

for example, I need to connect to blank-xml.myinfo.comhaps0011.

The problem I am facing is when I install this on user monitors. I have a .bat that opens openssl but cannot open a .txt file to run the above commands.

what i need to do for this to work.

Update:

I made a batch file that passes information to openssl, which is much smaller.

 @echo off c:\OpenSSL-Win64\bin\openssl s_client -connect help-xml.helpme.com:443 

This section works fine, shows the necessary information on the screen. I need to send another command to the window, but get an error message <command is not an executable or batch process.

this command <TF80DOC><XPLN/></TF80DOC> I tried to use the and symbol and used the echo before that, but I still get the same error or the screen will pop up and close instantly without any information.

the if then statement works after I run <TF80DOC><XPLN/></TF80DOC> , since it has the information that is displayed that the operator is looking for. But if I can’t get <TF80DOC><XPLN/></TF80DOC> to send to openssl after running s_client -connect help-xml.helpme.com:443 , then the if statement will never work.

Update:

I changed the powershell command to pipe in the command after s_client -connect help-xml.helpme.com:443

new code looks like

 @' <TF90DOC><XPLN/></TF90DOC> '@ | C:\OpenSSL-Win64\bin\openssl s_client -quiet -connects_client -connect help-xml.helpme.com:443 > test1.txt 2>&1 

the if statement is not a problem, since I know how to fix this part. The powershell part of the code works, but requires me to press the enter button, which is not what I need. I need it to execute the command automatically without user input

For the batch command, I made some minor changes to it:

 @echo off setlocal enabledelayedexpansion set "var=<TF90DOC><XPLN/></TF90DOC>" echo echo !var! | C:\OpenSSL-Win64\bin\openssl s_client -connect tf90-xml.bsi.com:443> test1.txt 2>&1 

this command still gives me an error

<was unexpected at the moment.

+6
source share
2 answers

I completely misunderstood your question and did not understand that you need to send a command to a newly opened instance of openssl. To do this, you need to execute the command you want to open.

 @echo off echo ^<TF80DOC^>^<XPLN/^>^</TF80DOC^>|c:\OpenSSL-Win64\bin\openssl s_client -connect help-xml.helpme.com:443 

Note that this has not been verified, and you may also need to avoid escape characters:

 echo ^^^<TF80DOC^^^>^^^<XPLN/^^^>^^^</TF80DOC^^^>|c:\OpenSSL-Win64\bin\openssl s_client -connect help-xml.helpme.com:443 

If you need to send more than one command, put them in a separate batch file with each command preceded by echo and pass it to the openssl command, for example:

commands.bat

 @echo off echo echo This is one command. echo echo This is another command. 

main.bat

 @echo off commands.bat|C:\OpenSSL-Win64\bin\openssl s-client -connect help-xml.helpme.com:443 
+1
source

Try the following: (see below).

 @echo off ( echo(^^^<TF90DOC^^^>^^^<XPLN/^^^>^^^</TF90DOC^^^> echo; ) | C:\OpenSSL-Win64\bin\openssl s_client -connect tf90-xml.bsi.com:443 > test1.txt 2>&1 

Edit:

From what you have described, it seems that openssl is not ready for input immediately after entering this XML and binds the new line passed to stdin in black. Therefore, instead of dumping everything directly to stdin, you need to enter some kind of dream and listen to this stdout program for the corresponding invitation.

Here's a proof of concept in JScript, which I think can be applied to you as soon as I find out what openssl text is used to query XML and then press Enter . This is a similar method, described on the WSHScriptExec StdOut Property documentation page, and is more flexible than any solution made in this way.

test.bat

 @if (@ a==@b ) @end /* begin multiline JScript comment :: batch portion @echo off setlocal cscript /nologo /e:JScript "%~f0" goto :EOF :: end batch portion / begin JScript */ var osh = new ActiveXObject('wscript.shell'), fso = new ActiveXObject('scripting.filesystemobject'), log = fso.CreateTextFile('output.txt', true), read; var exe = osh.Exec('cmd /c test2.bat'); while (1) { if (!exe.StdOut.AtEndOfStream) { read += exe.StdOut.Read(1); // read 1 char at a time if (/Arg 1:/.test(read)) { WSH.Sleep(50); exe.StdIn.Write('<TF80DOC><XPLN/></TF80DOC>\n'); WSH.Sleep(100); // This is the output we wish to capture. read = exe.StdOut.ReadLine(); WSH.Echo(read); log.WriteLine(read); read = ''; } else if (/Press any key/.test(read)) { WSH.Sleep(100); exe.StdIn.Write('\n'); break; } } else WSH.Sleep(100); } log.Close(); while (!exe.Status) WSH.Sleep(100); WSH.Echo('Done.'); 

test2.bat

 @echo off setlocal enabledelayedexpansion set /P "input=Arg 1: " echo(Input: !input! pause 
+1
source

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


All Articles