1
@ECHO OFF

:Start
      SET /P Enter user name:
      SET /P Enter Password:

      SET /P Enter age:
      SET /p Enter Passeword:

For the given above .bat file . How do I pass arguments using PYTHON

I want to write Python automation script which call the batch file and pass the required argument in the batch file

2 Answers 2

0

Though I'm not familiar with python but you need to check how command line and then use python.exe.

You need to rework your batch like:

@ECHO OFF

:Start
      SET /P user_name=Enter user name:
      SET /P password=Enter Password:

      SET /P user_age=Enter age:
      SET /p passEword=Enter Passeword:

and then:

call python.exe %user_name% %password% %user_age% %passEword%
Sign up to request clarification or add additional context in comments.

1 Comment

I want to write python automation script which opens the batch file and pass the value to it
0

There are two approaches that would work:

  • pass the values on the command-line (requires the .CMD script to parse the command line—tedious, IMHO)
  • pass the values via the environment (requires the .CMD script to see if the vars are defined before asking for them)

A sample implementation of the latter could be:

from subprocess import Popen
import os

command = [
    'mybatch.cmd'
]

params_by_env = {}

# "inherit" the current environment
for k,v in os.environ.items(): params_by_env[k] = v

# Specify the envvars for mybatch.cmd
params_by_env['USER_NAME'] = 'billg'
params_by_env['PASSWORD'] = 'killfree'
params_by_env['AGE'] = '60'
params_by_env['PASSEWORD'] = 'pa$$ew0rd'

proc = Popen(command,env=params_by_env)
proc.wait()

print "Finished"

A modified version of your .CMD script could look like this:

@SETLOCAL
@ECHO OFF

:Start
      IF "%USER_NAME%"=="" SET /P USER_NAME=Enter user name:
      IF "%PASSWORD%"=="" SET /P PASSWORD=Enter Password:

      IF "%AGE%"=="" SET /P AGE=Enter age:
      IF "%PASSEWORD%"=="" SET /p PASSEWORD=Enter Passeword:

@ECHO Username: %USER_NAME%
@ECHO Password: %PASSWORD%
@ECHO Age: %AGE%
@ECHO Passeword: %PASSEWORD%

Note the alterations to the SET /P commands, only executing them if the environment variable isn't set and assigning the value to the environment variable. Also note that USERNAME is a variable that already exists in Windows environments, so you wouldn't want to use it for this purpose. That's why I chose USER_NAME instead.

Security Caveat: It is generally considered to be a Bad Thing™ to put passwords on the command line (or in the environment—both of which are easily accessible to other programs running on the machine). Sometimes it must be done, but it should be avoided if possible. In the case of this program, you could have the Python script send input to the cmd script (proc.communicate(), I think, and SET /P in the .CMD script) to provide the password. There are other, even better, ways, but they are sometimes cumbersome to implement and not perfectly secure either.

Comments

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.