Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run commands into openssl through batch

i am trying to set up a custom script in uptime monitoring and have this command run openssl and have arguments that i pass into it run.

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 to have the variables ${host}:${port} are left blank and i want to have an argument that i manually put information in and the fields populate with that information.

for example i need to connect to blank-xml.myinfo.com:30011.

the problem i am running into is when i set this up on the custom monitors i have a .bat that opens openssl but cannot open the .txt file to run commands given.

what do i need to do in order for this to work.

Update:

I have made a batch file that passes in information to openssl that is a lot smaller.

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

this section works great shows information on the screen that is needed. I need to send in another command to the window also but get a error stating the the < command isn't an executable or batch process.

that command is <TF80DOC><XPLN/></TF80DOC> i have tried using the & symbol and have used echo before it but still am getting the same error or the screen will pop up and close instantly with no information.

the if then statement works after i run <TF80DOC><XPLN/></TF80DOC> since this has information that is displayed the statement is looking for. But if i can't get the <TF80DOC><XPLN/></TF80DOC> to be sent to openssl after the s_client -connect help-xml.helpme.com:443 runs then the if statement will never work.

Update:

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

the 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 then statement isn't an issue since i know how to fix that part of it. the powershell part of the code works but requires me to press enter which is not what i need it to do. i need it to execute the command automatically without user input

For the batch command i have made some slight changes to it, which are

@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 the error

< was unexpected at this time.

like image 984
bgrif Avatar asked Dec 04 '14 15:12

bgrif


1 Answers

I completely misunderstood your question at first and didn't realize you needed to send the command to the newly-opened openssl instance. In order to do this, you need to pipe the command you want to openssl.

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

Note that this is untested, and you may have to also escape the 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, place them in a separate batch file with each command preceded by an echo and pipe that to the openssl command, like this:

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
like image 194
SomethingDark Avatar answered Oct 11 '22 16:10

SomethingDark