4

Let's say I have a batch file, which asks me for my name as input. I put my name enter. This is the manual way.

But I wanted to try something with Python. Is it possible that I make a variable in Python script and put my name in the variable and pass that variable to the bat file when it asks for input?

The very first step of my bat file is to ask me my name. Asking name batch file is for test purpose. My main batch file has this code:

@ECHO OFF

:choice
set /P c=Do you want to continue [Y/N]?
if /I "%c%" EQU "Y" goto :yes
if /I "%c%" EQU "N" goto :no
goto :choice


:yes
"scripts\enter.py" yes
goto :continue

:no
"scripts\kick_out.py" no

:continue
pause 
exit

So, I hope this helps. I want to pass about 2 inputs. First Y and other when it calls enter.py it asks my login details. My username and password. So, I need to make 3 inputs. How can I do that?

2
  • Why not just edit the batch file itself so the name is stored in the file rather than asking for user input? Commented Jul 1, 2015 at 5:47
  • well,duh... if that was the case, I wouldn't be asking here,would I? lol... I need to create a python script because I want to parse some words from a feed,... an RSS feed. I got the feed parsed in python... Just need to pass it to the bat file which does the main stuff. Commented Jul 1, 2015 at 9:35

1 Answer 1

1

It is feasible : I use it to inject attack string which use non-printable ascii characters (like null bytes).

Example using Python 3 :

inject_string.py

import ctypes
import time
import subprocess
import binascii


if __name__ =="__main__":
    response = "Y"

    p = subprocess.Popen("echo.bat", stdin = subprocess.PIPE)
    time.sleep(2)

    p.stdin.write(bytes(response, 'ascii')) #Answer the question
    print("injected string :", response)

echo.bat

@ECHO OFF

:choice
set /P c=Do you want to continue [Y/N]?
if /I "%c%" EQU "Y" goto :yes
if /I "%c%" EQU "N" goto :no
goto :choice

:yes
echo "User has typed yes"
goto :continue

:no
echo "User has typed no"

:continue
pause

Output

python inject_string.py
Do you want to continue [Y/N]?injected string : Y
"User has typed yes"
Sign up to request clarification or add additional context in comments.

5 Comments

thanx.. but, you changed the code of the bat file. Look.. in my code.. it'ss calling a python script. which further asks me my username and pass. I already mentioned in the description... how do I enter the logins..? liek I said.. I need to enter 3 commands... and how do I use variable to pass the data..? and what is this line doing ` if name =="main": `
getting this :- ` Do you want to continue [Y/N]?Traceback (most recent call last): File "inject_string.py", line 13, in <module> p.stdin.write(bytes(response, 'ascii')) #Answer the question TypeError: str() takes at most 1 argument (2 given) "User has typed yes" Press any key to continue . . .
I changed the batch script since I don't have access to either enter.py or kick_out.py . You can call p.stdin.write as many times as you need to enter commands.
ohh.. okay.. and thanx.. it works like it is supposed to.. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.