User input: Problems with pasting multi-line strings
Moderator: DosItHelp
User input: Problems with pasting multi-line strings
Hello everyone,
First post here and fairly new to putting batch files together and up front, thank you for any help I receive. I work for an ESP and regularly set up DKIM records for clients. In an attempt to automate some of the tedious stuff I want to put together a batch. I have the entire file working just fine minus one piece that sort-of works.
This is an example of the broken section:
@echo off
cd C:\Test_Dir\DKIMtest
set /p key= [Enter the private key here:]
set /p domain= [Enter the domain name here:]
echo %key%>>key1.%domain%.pem
If I just type some test text in the first prompt, everything goes smoothly, but DKIM private keys aren't just one line. They also have character limits per line (64). When I attempt to paste the private key data into the user prompt, the prompt attempts to create multiple entries for each line which in-turn breaks the batch file. So the question is: How can I force the batch file to accept the entire private key as a single string?
Below is an example 1024-bit private key (generated for a non-existent domain):
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQC4iPzo8rqSU/OynpjHYDYleL6Xukbc2fDDYJVOp8umxI8cS+0n
QaUVHYF2xdgSo4wjtkZMJthbuUGtRP5Jb+xJ51eLk+3V28XAVW3nqgHjs8AnHrlj
hgi4yrH2UZhuvhdjjiOzHfkhCFOxJi3ItKOwWP79rF5U5kh77YepjqFojQIDAQAB
AoGBAKSGxm7vEvcp0Io9qtt/5WbNW6itlUaNLYMUlqo1InaCo1HIRB4PAh9sWiRK
StZ7aJPijJSSwa4t4ueYLtvM5S6ORhwaC+eyFxHADNsEPED0BFn9F0Dwq6jruyFi
u5Frz3q9N2jj2MWGwCcKxvl4qnjjftTWfab6PYviaPUqmNphAkEA6x/j+wZK9/AV
xoyGLcU54pd+F3eCGysJBow1OKtTLXsjISUkoCkN1RaK8lGwnr/YdAgxm8ejiOOp
SdyWxPseaQJBAMjrQeY7tir4K08Zn1f0a5ovpffjDG2ptrntAyvgEGZbr/bv2kti
4Tv3ur1r9asIK4zDIdN6HcmpIC/5QQd6PIUCQATsGf27V/5wzEPn1TdIFbokqcMc
AWFIOVwlu0eAbrFZDpc3C9k+gsefPTFtfGEWaxWixkcYmtfWzEh7kbx19WECQFZq
uaFpNWUt8JLJFnwiKNXcgKoqAgJ46Jr3klGgwe0Eqp6vc1jRYscEOlGWyceScAjP
NRFciMtf7vQzZQDuVnECQQCdZRaBVnO/MFrFAPpJy/06mx5rnkjodU3bQh3BYq4x
g0z3KrChK5r1S4vWAsYZcqvCVbFB5RW3a/LHMQxK0hPF
-----END RSA PRIVATE KEY-----
First post here and fairly new to putting batch files together and up front, thank you for any help I receive. I work for an ESP and regularly set up DKIM records for clients. In an attempt to automate some of the tedious stuff I want to put together a batch. I have the entire file working just fine minus one piece that sort-of works.
This is an example of the broken section:
@echo off
cd C:\Test_Dir\DKIMtest
set /p key= [Enter the private key here:]
set /p domain= [Enter the domain name here:]
echo %key%>>key1.%domain%.pem
If I just type some test text in the first prompt, everything goes smoothly, but DKIM private keys aren't just one line. They also have character limits per line (64). When I attempt to paste the private key data into the user prompt, the prompt attempts to create multiple entries for each line which in-turn breaks the batch file. So the question is: How can I force the batch file to accept the entire private key as a single string?
Below is an example 1024-bit private key (generated for a non-existent domain):
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQC4iPzo8rqSU/OynpjHYDYleL6Xukbc2fDDYJVOp8umxI8cS+0n
QaUVHYF2xdgSo4wjtkZMJthbuUGtRP5Jb+xJ51eLk+3V28XAVW3nqgHjs8AnHrlj
hgi4yrH2UZhuvhdjjiOzHfkhCFOxJi3ItKOwWP79rF5U5kh77YepjqFojQIDAQAB
AoGBAKSGxm7vEvcp0Io9qtt/5WbNW6itlUaNLYMUlqo1InaCo1HIRB4PAh9sWiRK
StZ7aJPijJSSwa4t4ueYLtvM5S6ORhwaC+eyFxHADNsEPED0BFn9F0Dwq6jruyFi
u5Frz3q9N2jj2MWGwCcKxvl4qnjjftTWfab6PYviaPUqmNphAkEA6x/j+wZK9/AV
xoyGLcU54pd+F3eCGysJBow1OKtTLXsjISUkoCkN1RaK8lGwnr/YdAgxm8ejiOOp
SdyWxPseaQJBAMjrQeY7tir4K08Zn1f0a5ovpffjDG2ptrntAyvgEGZbr/bv2kti
4Tv3ur1r9asIK4zDIdN6HcmpIC/5QQd6PIUCQATsGf27V/5wzEPn1TdIFbokqcMc
AWFIOVwlu0eAbrFZDpc3C9k+gsefPTFtfGEWaxWixkcYmtfWzEh7kbx19WECQFZq
uaFpNWUt8JLJFnwiKNXcgKoqAgJ46Jr3klGgwe0Eqp6vc1jRYscEOlGWyceScAjP
NRFciMtf7vQzZQDuVnECQQCdZRaBVnO/MFrFAPpJy/06mx5rnkjodU3bQh3BYq4x
g0z3KrChK5r1S4vWAsYZcqvCVbFB5RW3a/LHMQxK0hPF
-----END RSA PRIVATE KEY-----
Re: User input: Problems with pasting multi-line strings
Your example has multiple lines... and each line will be terminated by a carriage return and line feed.
Maybe you can ask for the filename that contains the key and parse that for the information.
Maybe you can ask for the filename that contains the key and parse that for the information.
Re: User input: Problems with pasting multi-line strings
I'm aware that it contains multiple lines. What I want to know is if there is a way to wrap an entire entry in something or a command to say "all these lines are one entry".
For example when working in some DNS servers if you type key1=rsa; p=;lsfja98233lndsfaj\s;fjj without explicitly wrapping the whole thing in quotes, it will recognize it as two entries but if you wrap the whole thing in quotes it reads as one entry.
For example when working in some DNS servers if you type key1=rsa; p=;lsfja98233lndsfaj\s;fjj without explicitly wrapping the whole thing in quotes, it will recognize it as two entries but if you wrap the whole thing in quotes it reads as one entry.
Re: User input: Problems with pasting multi-line strings
You don't want the key typed in, you want it pasted in. But it has multiple lines.
You will have to use a different technique than set /p as a CRLF ends the input.
We can suggest different ways to do something if the task is clear and limitations spelled out.
You will have to use a different technique than set /p as a CRLF ends the input.
We can suggest different ways to do something if the task is clear and limitations spelled out.
Re: User input: Problems with pasting multi-line strings
Does the key always have the same number of lines?
Re: User input: Problems with pasting multi-line strings
foxidrive wrote:We can suggest different ways to do something if the task is clear and limitations spelled out.
I feel like I pretty much covered that with
dutch wrote:If I just type some test text in the first prompt, everything goes smoothly, but DKIM private keys aren't just one line. They also have character limits per line (64). When I attempt to paste the private key data into the user prompt, the prompt attempts to create multiple entries for each line which in-turn breaks the batch file. So the question is: How can I force the batch file to accept the entire private key as a single string?
But based on a quick sample test of 6 fake generations they appear to be 15 lines although I would want to test that out tomorrow as well to see if line count varies.
Is there any way to force the prompt to disregard the carriage returns for an entry?
Re: User input: Problems with pasting multi-line strings
Assuming the line count never changes for that bit-length I suppose I could utilize multiple set statements but If there is a way to force the carriage returns to be ignored for the user entry that would be preferable to stream-line this a bit.
Re: User input: Problems with pasting multi-line strings
Code: Select all
@echo off
cd /d C:\Test_Dir\DKIMtest
set /p domain= [Enter the domain name here:]
echo [paste the private key here and press Enter if neccessary:]
@echo off
(echo do until wsh.stdin.atendofstream
echo a=wsh.stdin.readline
echo Wscript.echo a
echo if a = "-----END RSA PRIVATE KEY-----" then exit do
echo loop
) > %temp%.\tmp.vbs
cscript /nologo %temp%.\tmp.vbs >"key1.%domain%.pem"
del %temp%.\tmp.vbs
Re: User input: Problems with pasting multi-line strings
Here is the plain batch version for 15 line keys.
Code: Select all
@echo off
cd /d C:\Test_Dir\DKIMtest
set /p domain= [Enter the domain name here:]
echo [paste the private key here and press Enter if necessary:]
set /p var=
>"key1.%domain%.pem" echo %var%
set /p var=
>>"key1.%domain%.pem" echo %var%
set /p var=
>>"key1.%domain%.pem" echo %var%
set /p var=
>>"key1.%domain%.pem" echo %var%
set /p var=
>>"key1.%domain%.pem" echo %var%
set /p var=
>>"key1.%domain%.pem" echo %var%
set /p var=
>>"key1.%domain%.pem" echo %var%
set /p var=
>>"key1.%domain%.pem" echo %var%
set /p var=
>>"key1.%domain%.pem" echo %var%
set /p var=
>>"key1.%domain%.pem" echo %var%
set /p var=
>>"key1.%domain%.pem" echo %var%
set /p var=
>>"key1.%domain%.pem" echo %var%
set /p var=
>>"key1.%domain%.pem" echo %var%
set /p var=
>>"key1.%domain%.pem" echo %var%
set /p var=
>>"key1.%domain%.pem" echo %var%
Re: User input: Problems with pasting multi-line strings
Thank you for your input, it is appreciated. To be honest I am not very familiar with windows scripting. Your initial script you provided, what is the premise behind it and do you know of any good online resources I could use to better understand it? Now that I know it can be done via a script I am compelled to learn how to do it as well as what makes it work rather than just copying and pasting the provided script.
Re: User input: Problems with pasting multi-line strings
Here's a version using delayed expansion - 15 line keys
EDIT: If the keys can contain ! characters then don't use this technique.
EDIT: If the keys can contain ! characters then don't use this technique.
Code: Select all
@echo off
setlocal enabledelayedexpansion
cd /d C:\Test_Dir\DKIMtest
set /p domain= [Enter the domain name here:]
echo [paste the private key here and press Enter if necessary:]
del "key1.%domain%.pem" 2>nul
for /L %%a in (1,1,15) do set /p var=&>>"key1.%domain%.pem" echo !var!
Re: User input: Problems with pasting multi-line strings
dutch wrote:Thank you for your input, it is appreciated. To be honest I am not very familiar with windows scripting. Your initial script you provided, what is the premise behind it and do you know of any good online resources I could use to better understand it? Now that I know it can be done via a script I am compelled to learn how to do it as well as what makes it work rather than just copying and pasting the provided script.
For seamless operation the scripts rely on the last line of the paste having a CRLF at the end, otherwise enter needs to be pressed as you'd expect.
The first script using .VBS runs a script that accepts the key from STDIN and tests for the last line being a certain string, when it exits. This will handle any length of key.
Windows Scripting Host documentation V5.6 is a document describing WSH.
The other two versions use SET /P and accept a simple line with trailing CRLF for each line of the key being pasted.
Re: User input: Problems with pasting multi-line strings
This plain batch version will handle any length of key - assuming the last line is common to all keys
Code: Select all
@echo off
setlocal enabledelayedexpansion
cd /d C:\Test_Dir\DKIMtest
set /p domain= [Enter the domain name here:]
echo [paste the private key here and press Enter if necessary:]
del "key1.%domain%.pem" 2>nul
:loop
set /p var=
>>"key1.%domain%.pem" echo %var%
if /i not "%var%"=="-----END RSA PRIVATE KEY-----" goto :loop
Re: User input: Problems with pasting multi-line strings
Right now I'm focusing on trying to research what each line of the script is doing (I majored in Networking with a focus on routing and switching so I'm mostly self taught in terms of coding) but let me see if I got this straight (and please correct me if and when I am wrong as I truly do want to learn this:
echo do until wsh.stdin.atendofstream :is basically saying, keep looping until the end of the text stream that is pasted
echo a=wsh.stdin.readline :I'm assuming this is setting the variable "a" but I don't know what's going on with readline
echo Wscript.echo a :If i'm sticking with my above logic this should be acting on the variable you set
echo if a = "-----END RSA PRIVATE KEY-----" then exit do :I'm guessing this states "when you reach this line you are done"
> %temp%.\tmp.vbs
cscript /nologo %temp%.\tmp.vbs >"key1.%domain%.pem"
del %temp%.\tmp.vbs
It looks like this creates the script in your temporary folder and then runs the script via cscript, outputting the data to my pem file, finally deleting the temporary script from the temp folder.
I understand I am asking a lot to have this laid out in baby steps for me but I would rather understand what is going on then blindly take someones work without some effort towards understanding it.
echo do until wsh.stdin.atendofstream :is basically saying, keep looping until the end of the text stream that is pasted
echo a=wsh.stdin.readline :I'm assuming this is setting the variable "a" but I don't know what's going on with readline
echo Wscript.echo a :If i'm sticking with my above logic this should be acting on the variable you set
echo if a = "-----END RSA PRIVATE KEY-----" then exit do :I'm guessing this states "when you reach this line you are done"
> %temp%.\tmp.vbs
cscript /nologo %temp%.\tmp.vbs >"key1.%domain%.pem"
del %temp%.\tmp.vbs
It looks like this creates the script in your temporary folder and then runs the script via cscript, outputting the data to my pem file, finally deleting the temporary script from the temp folder.
I understand I am asking a lot to have this laid out in baby steps for me but I would rather understand what is going on then blindly take someones work without some effort towards understanding it.
Re: User input: Problems with pasting multi-line strings
dutch wrote:Right now I'm focusing on trying to research what each line of the script is doing (I majored in Networking with a focus on routing and switching so I'm mostly self taught in terms of coding) but let me see if I got this straight (and please correct me if and when I am wrong as I truly do want to learn this:
echo do until wsh.stdin.atendofstream :is basically saying, keep looping until the end of the text stream that is pasted
echo a=wsh.stdin.readline :I'm assuming this is setting the variable "a" but I don't know what's going on with readline
Yes, it sets the variable a to each line that is being received from STDIN (standard in) which is coming from the console.
echo Wscript.echo a :If i'm sticking with my above logic this should be acting on the variable you set
echo if a = "-----END RSA PRIVATE KEY-----" then exit do :I'm guessing this states "when you reach this line you are done"
> %temp%.\tmp.vbs
cscript /nologo %temp%.\tmp.vbs >"key1.%domain%.pem"
del %temp%.\tmp.vbs
It looks like this creates the script in your temporary folder and then runs the script via cscript, outputting the data to my pem file, finally deleting the temporary script from the temp folder.
Everything you say is correct. Lines are terminated by CRLF and that means the end of the paste needs to include one, or else it will wait for enter to be pressed.