0

I am trying to generate certificate request and submit to CA through bat file.

I am able to do it for just 1 hostname so I am doing a bat file to generate all these automatically. Here's what I've tried. It is supposed to loop through line by line in the hostnames.txt file and replace all the variables.

Referencing from How do you loop through each line in a text file using a windows batch file?

set CA_SERVER=xxxx
set CA_NAME=xxxx
set LOGFILE=.\RequestCert.log
set LIST=hostnames.txt

if not exist %LIST% (
  echo %DATE% : %TIME% : ERROR : The list of hostnames file %LIST% is 
  missing. Cannot request certificate. Exiting! >> %LOGFILE% )

for /f "tokens=*" %%a in ("%LIST%") do (

  set HOSTNAME=%%a
  echo HOSTNAME
)

I want to test if a simple echo will work but the error came up as: The syntax of the command is incorrect. I saw many references from stackoverflow but it can't figure out what is wrong.

Below is what I will implement after the above is fixed.

openssl req -nodes -newkey rsa:2048 -nodes -keyout %HOSTNAME%.key -out 
%HOSTNAME%.csr -subj "/C=SG/ST=/L=xxx/O=xxx/OU=xxx 
/CN=%HOSTNAME%"

certreq -submit -f -config "%CA_SERVER%\%CA_NAME%" -attrib 
CertificateTemplate:SSLCert" %HOSTNAME%.csr

echo Generating certificate request and key for %HOSTNAME%

I know this has been asked many times but mine doesn't seem to work.. Thank you in advance for your help!

UPDATE: Current configuration which works after help from Gerhard Barnard. enter image description here

Salt
  • 59
  • 2
  • 12
  • Did not generate that error for me, just `echo`ed `HOSTNAME` as expected. Since `list` is a filename, quoting it in the `for/f` will simply assign the filename to `%%a`. If you want the *contents* of the file, thenn you need to use the `usebackq` option to alter the meaning of the quotes. Having assigned the value to `hostname`, you would need to `echo` the *contents* of `hostname` - not the constant-string `HOSTNAME` - and even there, since the `echo` command is within a parenthesised sequence of lines) (a "`code block`") you need to take other measures - see SO items on `delayed expansion`. – Magoo May 26 '17 at 04:34

1 Answers1

0

The main reason why your read of file does not work is due to the "" in the for loop.

This will work, do you really need to reassign %%a to hostname if you know that %%a is hostname?

@echo off
set CA_SERVER=xxxx
set CA_NAME=xxxx
set LOGFILE=.\RequestCert.log
set LIST=hostnames.txt

if not exist %LIST% (
       echo %DATE% : %TIME% : ERROR : The list of hostnames file %LIST% is missing. Cannot request certificate. Exiting! >> %LOGFILE% )

FOR /F "tokens=*" %%a in (%LIST%) do (
                               echo %%a
                              )

have a look at setlocal DELAYEDEXPANSION else the variables inside the loop is expanded only once.

so if we want to assign %%a to HOSTNAME we do this:

@echo off
setlocal ENABLEDELAYEDEXPANSION
set CA_SERVER=xxxx
set CA_NAME=xxxx
set LOGFILE=.\RequestCert.log
set LIST=hostnames.txt

if not exist %LIST% (
  echo %DATE% : %TIME% : ERROR : The list of hostnames file %LIST% is missing. Cannot request certificate. Exiting! >> %LOGFILE% )

for /f "tokens=*" %%a in (%LIST%) do (
set "HOSTNAME=%%a"
echo !HOSTNAME!
)

Complete script.

@echo off
setlocal ENABLEDELAYEDEXPANSION
set CA_SERVER=xxxx
set CA_NAME=xxxx
set LOGFILE=.\RequestCert.log
set LIST=hostnames.txt

if not exist %LIST% (
echo %DATE% : %TIME% : ERROR : The list of hostnames file %LIST% is missing. Cannot request certificate. Exiting! >> %LOGFILE% )

for /F "tokens=*" %%a in (%LIST%) do (
set "HOSTNAME=%%a"
echo !HOSTNAME!
openssl "req -nodes -newkey rsa:2048 -nodes -keyout !HOSTNAME!.key -out !HOSTNAME!.csr -subj "/C=SG/ST=/L=xxx/O=xxx/OU=xxx /CN=!HOSTNAME!"

certreq -submit -f -config "%CA_SERVER%\%CA_NAME%" -attrib CertificateTemplate:SSLCert" !HOSTNAME!.csr

echo Generating certificate request and key for !HOSTNAME!
)
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • I followed exactly but it still says: The syntax of the command is incorrect /: – Salt May 26 '17 at 08:09
  • I am executing the bat file in the cmd. There is no issue on that right? – Salt May 26 '17 at 08:10
  • Did you copy the last portion and run as is? you can run it from cmd yes, but you have to CD to the directory where the batch is in and make sure the hostnames.txt is in the same folder. can you send a screenshot of the exact error? PS!! the logfile line must be in the same line. Let me edit. cannot be new line – Gerhard May 26 '17 at 08:11
  • Updated my question with the current configuration! – Salt May 26 '17 at 10:22
  • ok, but that is wrong. see how I used hostnames in my code? `!HOSTNAME!` ad not `%HOSTNAME%` Let me edit my answer and add the entire script at the bottom – Gerhard May 26 '17 at 10:32
  • I actually managed to make it work before seeing your solution! Slightly different but works too! Thank you anyways! – Salt May 26 '17 at 13:48
  • So that is it? not saying what you have done to fix it after I tried to help? If you could do that then it would great. – Gerhard May 26 '17 at 13:50
  • Basically is what you mentioned but I didn't set the HOSTNAME in the end. Thought that it is redundant after you mentioned it haha – Salt May 26 '17 at 13:56
  • Thanks for the tag! Yes, I mentioned right in the beginning that it is not needed to re-assign %%a to HOSTNAME, you know it is HOSTNAME so no need for that :) – Gerhard May 26 '17 at 14:00