How do I specify input to a batch file using a Python script?

Let's say I have a batch file that asks me what my name is. I entered my name. This is a manual way.

But I wanted to try something with Python. Is it possible that I am creating a variable in a Python script and putting my name in a variable and passing 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. Request a batch file name for testing. 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 transmit about 2 inputs. First Yand others, when it calls enter.py

, it asks for my login details. My username and password. So I need to make 3 inputs. How can i do this?

+3


source to share


1 answer


This is possible: I am using it to input an attack string that uses 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"

      

+1


source







All Articles