2

I'm creating a python script (cmd_exec.py) to open another python script (entername.py). How do I nest it so the script inputs string and execute the enter button all automatically? Trying to remember the question about ASCII input code for enter, but can't find it. So that when I run the cmd_exec.py in powershell, it would display "Hello foo":

cmd_exec.py:

import subprocess, sys

p = subprocess.Popen(["powershell.exe", "py C:\\Users\\uname\\PProjects\\cmdauto\\entername.py"], stdout=sys.stdout)
p.communicate()

I want maname variable to get inserted in the entername.py script and the script to execute/press enter button. So that when I run the cmd_exec.py script, I would see it doing all by itself and prints out "Hello foo"

entername.py:

maname = "foo"
person = input('Enter your name: ')
print('Hello', person)
SuperKogito
  • 2,998
  • 3
  • 16
  • 37
Antonow297296
  • 317
  • 1
  • 4
  • 12
  • I'm not sure if I understood you question correctly. Why do you need 2 scripts? Would `subprocess.getoutput(cmd)` help you? https://docs.python.org/3/library/subprocess.html#subprocess.getoutput. – Pedro Lobito Apr 05 '19 at 20:44
  • @PedroLobito I am actually going to run sqlmap.py, but cant get it working on this machine right now. So I put these two simple scripts for question, but principle is the same. When I run sqlmap.py, it would ask me yes or no on some question. And I want to automate the process with a script so that when running sqlmap.py it would input "y" automatically for me. – Antonow297296 Apr 05 '19 at 20:47

2 Answers2

0

Not sure if this helps or if this will work for you but in Bash you can specify input with the '<' operator.

For instance if you have a python file program.py and your inputs are in input.txt you can run

$ python3 program.py < input.txt

You may want to take this approach unless have other constraints

VicVer
  • 143
  • 6
0

You may be looking for sys.argv and subprocess.getstatusoutput()

import subprocess
maname = "foo"
person = input('Enter your name: ')
print('Hello', person)
# not  sure why powershell is here, normally would be only `python C:\\Users\\uname\\PProjects\\cmdauto\\entername.py {person}`
x = subprocess.getstatusoutput(f"powershell.exe 'py C:\\Users\\uname\\PProjects\\cmdauto\\entername.py {person}'") 
print(x)
# (0, 'python f:\\entername.py')

# entername.py 
import sys
person = int(sys.argv[1]) # get the 2nd item on `argv` list, the 1st is the script name
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • It still wants me to type my name. `PS C:\Users\uname\PProjects\cmdauto> py .\cmd_exec.py Enter your name: Hello (0, 'py entername.py ') PS C:\Users\uname\PProjects\cmdauto>` – Antonow297296 Apr 07 '19 at 10:36