Python executing one command inside another DOS .exe command - python

In python I know how to launch a command using subprocess.Popen
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
what I have to do is: 1) call an executable .exe command, 2) then after that command is running within it call another command.
So when I do 1) like this:
process = subprocess.Popen("C:\Automation\helpFiles\topCmd.exe", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
once that is running I need to run another command i.e state
how can I do it?

Related

input commands after opening a shortcut?

I am using Python 3.10.6 and Miniconda 3
What I'm trying to do is write a python script which will open a .cmd file. simple enough, but the problem is it has to be opened in anaconda. I've been able to have my script open the .cmd through the standard command line, and I've been able to have the script open anaconda. But I'm unable to do both in sequence, open the anaconda terminal and then pass a command to it, instructing it to open the .cmd
opening anaconda:
path = 'C:\\Users\...\shortcuts\\conda.lnk'
cmds = 'C:\\Users\\...test.cmd'
cmd_result = subprocess.run(path, shell=True, check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
text=True )
print(cmd_result.args)
print(cmd_result.stdout)
this works properly when i open either anaconda path or test.cmd directly using the subprocess.run function. but when I attempt to open test.cmd as a command argument in the anaconda terminal, it doesn't work
path = 'C:\\Users\...\shortcuts\\conda.lnk'
cmds = [path,'/C','C:\\Users\\...test.cmd']
cmd_result = subprocess.run(cmds, shell=True, check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
text=True )
I believe this is the relevant part of the error code it returns
File "C:\Users\...dream_restart_module.py", line 17, in dream_restart
cmd_result = subprocess.run(cmds, shell=True, check=True,
File "C:\Users\...Python310\lib\subprocess.py", line 524, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['C:\\Users\\...shortcuts\\conda.lnk', '/C', 'C:\\Users\\...test.cmd']' returned non-zero exit status 1.
I feel like I am missing something obvious here, perhaps about the syntax or limitations of subprocess. Any guidance would be appreciated
When you use subprocess.run() with shell=True, the arguments you give it as part of your cmds list are passed directly to the shell, rather than as arguments to the initial command.
So if you intend to pass '/C' and 'C:\\Users\\...test.cmd' as arguments to path, you should either not declare shell=True (preferred) or provide your cmds as a single, space-separated string (less preferred), like path+' /C C:\\Users\\...test.cmd' or ' '.join(cmds).

Run jar file as Administrator

I am running a jar file from python.
This jar must be run as administrator in order to work.
the python script is run in a jenkins job.
Is there a way to run the jar/python script as and administrator?
either from the jenkins job - or modify the python script.
with subprocess.Popen(command,
cwd=tool_dir,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True) as proc:
try:
outs, errs = proc.communicate(timeout=1000)
Thank you!

Getting output of command execution on windowed app

I have created a Python script and compiled it into an exe file with PyInstaller. In the process, I have specified the -w option to get an app that doesn't have any console.
Everything works fine except the execution of commands using popen:
mout = subprocess.Popen(['ls','C:\'])
This line generates an exception [Error 6] The handle is invalid.
I have tried adding the parameters
stdout=subprocess.PIPE, stderr=subprocess.PIPE but it stills not working. I think that is because the main process doesn't have any console assigned. I want to execute the command but without opening any shell, it has to be transparent to the user.
Is there any option?
This should solve your issue.
proc = subprocess.Popen(['ls','C:\\'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, creationflags=CREATE_NO_WINDOW)

Get output and Return code from external shell command in python

Suppose I run a command cmd="start python Testscript.py" using subprocess in python.
subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
Is there any way to get the output of the new command console ?
Is there any way to get the return code from the TestScript.py into the calling python function.

Running rsync from python subprocess in windows

I need to run rsync from Python 2.7 app in windows 7 x64 (using cwRsync 5.5.0).
Everything works fine from command line:
set CWRSYNCHOME in env to cwrsync binaries and run following command
rsync.exe "/cygdrive/e/test" test1#192.168.1.14:
But when trying to run same command as python subprocess:
process = subprocess.Popen(['rsync.exe', '/cygdrive/e/test', 'test1#192.168.1.14:'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
env={'CWRSYNCHOME': './bin'})
stdout, stderr = process.communicate()
print 'STDOUT:{}\nSTDERR:{}'.format(stdout, stderr)
I get following error in stderr:
rsync: pipe: Operation not permitted (1)
rsync error: error in IPC code (code 14) at pipe.c(59) [sender=3.1.2]
Here is verbose rsync stdout:
FILE_STRUCT_LEN=16, EXTRA_LEN=4
cmd=<NULL> machine=192.168.1.14 user=test1 path=.
cmd[0]=ssh cmd[1]=-l cmd[2]=test1 cmd[3]=192.168.1.14 cmd[4]=rsync cmd[5]=--server cmd[6]=-vvvvve.LsfxC cmd[7]=. cmd[8]=.
opening connection using: ssh -l test1 192.168.1.14 rsync --server -vvvvve.LsfxC . . (9 args)
[sender] _exit_cleanup(code=14, file=pipe.c, line=59): entered
[sender] _exit_cleanup(code=14, file=pipe.c, line=59): about to call exit(14)
Tryed set shell=False and pass command as single line (not cmd and args) - error stil repeats.
What am i doing wrong ?
To get it work, rsync needs to be runned under cygwin's shell:
process = subprocess.Popen(['sh.exe', '-c',
'rsync /cygdrive/e/test test1#192.168.1.14:'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
env={'CWRSYNCHOME': '/bin/',
'PATH': '/bin/'})
It's working (there is no ssh athorization in example above).

Categories

Resources