How to run psexec using subprocess.run()? - python

Here's the script snippet:
import subprocess
output = subprocess.run(["psexec.exe",
"\\\\<machine-name>",
"/u", "<machine-name>\\TestAdmin",
"/p", "<password>",
"cmd /c dir"],
capture_output=True,
text=True,
shell=True)
print(output.stdout)
print(output.stderr)
And here's the output I get:
E:\python-scripts>python runpsexec.py
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
The system cannot find the path specified.
Connecting to <machine-name>...
Starting PSEXESVC service on <machine-name>...
Connecting with PsExec service on <machine-name>...
Starting cmd /c dir on <machine-name>...
PsExec could not start cmd /c dir on <machine-name>:
I also tried passing in the -s option to run it as a system account. If I directly run psexec from cmd prompt, I'm able to run the same command and get the right output.
Are there any limitations in running psexec via subprocess.run()? Or am I missing something?

Related

How do I pass inputs to cmd prompt after I have run the os.system command?

I am trying to start kafka producer via cmd prompt (I have kube installed in my local). This is the code that I am using :
os.system('cmd /k "kubectl run kafka-producer152 -ti --image=strimzi/kafka:0.18.0-kafka-2.4.0 --rm=true --restart=Never -- bin/kafka-console-producer.sh --broker-list 17.148.115.9:32033 --topic ship"')
The topic is 'ship'.The command executes by running the producer command given above.
What should be done if I want the command to be executed (which it does now) and fetch all the data (inputs) from a file stored in same directory (values.txt) and enter those values only after executing the above kubectl command int the same cmd window.
I'm not a Windows person (thank goodness) but I think the following should do what you are asking.
import subprocess
with open(r'C:\razy\windows\path\values.txt') as values:
subprocess.run(['kubectl', 'run', 'kafka-producer152',
'-ti', '--image=strimzi/kafka:0.18.0-kafka-2.4.0',
'--rm=true', '--restart=Never',
'--', 'bin/kafka-console-producer.sh',
'--broker-list', '17.148.115.9:32033',
'--topic', 'ship'],
stdin=values,
# probably not
#cwd=r'c:\razy\windows\path',
# probably yes
check=True, text=True)

unable to execute powershell script using python process but the same works in cmd line

i have a powershell script i'm trying to execute via python subprocess. i use check_output imported from subprocess for other cmd line processes, so i am using it for the powershell script. inside my powershell script i'm loading some 3rd party modules and then executing a cmdlet from those modules. here is my python code..
from subprocess import Popen, STDOUT, check_output
cmd = 'C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe -nologo -file c:\\test.ps1'
cmd_output = check_output(cmd, stderr=STDOUT)
output = cmd_output.decode("utf-8").split('\n')
When the python code runs it takes some time where i assume it's loading the modules via the code i have in the powershell ps1 script , BUT i get an error that the cmdlet i was referencing inside my powershell script is "not recognized as the name of a cmdlet", which happens when the modules don't get loaded properly in the powershell script, so i don't think my modules are loading which is weird.. i also noticed when the ps1 script loads via python it runs for about 10 secs then returns, if i run the ps1 script (see further below) manually from command line it usually takes 60 secs to load the modules inside the ps1 script.
i also tried another method for sub proccess instead of check_output, see below, with no differences
cmd = Popen(['powershell.exe', '-nologo', '-file', 'c:\\test.ps1'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
BUT i can open a cmd window and execute it manually perfectly fine using
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -nologo -file C:\test\test.ps1
which should be the same as i'm already doing in python :( so i'm at a loss why it's not working in python??
also my execution policy is as follows
PS C:\> Get-ExecutionPolicy -list
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser Unrestricted
LocalMachine Unrestricted
Try using unrestricted ExecutionPolicy parameter :
# python 2.7 and 3.4
import subprocess
fname = "C:\\Demo\\test.ps1"
p = subprocess.Popen(["powershell", "-ExecutionPolicy", "unrestricted", "-nologo", "-file", "%s"%(fname)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode == 0:
print("buffer :")
print(out)
else:
print("error :")
print(err)
The content of 'C:\Demo\test.ps1'
write-host $env:USERPROFILE;
Get-ExecutionPolicy -list;
try{
Get-Module -Name VMware.VimAutomation.Core -ListAvailable | Import-Module;
write-host 'ok import VMware';
}catch{
write-host 'error import VMware';
write-host $_;
}
The output after script execution (note the Process policy)
buffer :
C:\Users\MyUserName
ok import VMware
Scope ExecutionPolicy
----- ---------------
... ...
... ...
Process Unrestricted
... ...
... ...
On system32 Module path is here:
C:\WINDOWS\system32\WindowsPowerShell\v1.0\
Try using:
p = subprocess.Popen(["powershell", "-ExecutionPolicy", "unrestricted", "-nologo", "-command", "&{ Get-Module –ListAvailable }"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for showing all available modules

Open Developer Command Promt from Python and write commands

I tried to open "Developer Command Prompt for VS 2017" from Python, write some command within like cd .., and then run an exe.
However it doesn't let me to write commands at all.
I have tried:
process = subprocess.Popen('C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\Common7\\Tools\\VsDevCmd.bat',
shell=False, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=None)
process.stdin.write('1.txt'.encode(encoding='utf_8', errors='strict'));
output = process.stdout.readlines()
print(output)
When I open the developer command promtpt on my own it is directed to c:\>.
So, I saved c:\ in a text file called 1.txt to know if it's run the command within Python. However, in the Python console I only get the welcome text:
[b'**********************************************************************\r\n',
b'** Visual Studio 2017 Developer Command Prompt v15.5.2\r\n',
b'** Copyright (c) 2017 Microsoft Corporation\r\n',
b'**********************************************************************\r\n']
The issue is with VsDevCmd.bat closing before you can communicate with the generated subprocess as i believe
Your method works but it is better if you use communicate instead of directly sending input to stdin:
output = communicate('cd c: \n')[0]
Check the documentation on communicate
You can run the same code above using communicate and calling CMD instead :
process = subprocess.Popen('cmd',shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None)
output = process.communicate('dir\n')[0]
print output

how to give multiple command to cmd.exe after opening it using python [duplicate]

I'm trying to open a new command window in a BAT file:
start %windir%\system32\cmd.exe
After it opens, I'd like to execute a BAT command in the new window:
echo "test in new window"
How can I do this?
You may already find your answer because it was some time ago you asked. But I tried to do something similar when coding ror. I wanted to run "rails server" in a new cmd window so I don't have to open a new cmd and then find my path again.
What I found out was to use the K switch like this:
start cmd /k echo Hello, World!
start before "cmd" will open the application in a new window and "/K" will execute "echo Hello, World!" after the new cmd is up.
You can also use the /C switch for something similar.
start cmd /C pause
This will then execute "pause" but close the window when the command is done. In this case after you pressed a button. I found this useful for "rails server", then when I shutdown my dev server I don't have to close the window after.
Use the following in your batch file:
start cmd.exe /c "more-batch-commands-here"
or
start cmd.exe /k "more-batch-commands-here"
/c Carries out the command
specified by string and then
terminates
/k Carries out the
command specified by string but
remains
Consult the cmd.exe documentation using cmd /? for more details.
The proper formatting of the command string becomes more complicated when using arguments with spaces. See the examples below. Note the nested double quotes in some examples.
Examples:
Run a program and pass a filename parameter:
CMD /c write.exe c:\docs\sample.txt
Run a program and pass a long filename:
CMD /c write.exe "c:\sample documents\sample.txt"
Spaces in program path:
CMD /c ""c:\Program Files\Microsoft Office\Office\Winword.exe""
Spaces in program path + parameters:
CMD /c ""c:\Program Files\demo.cmd"" Parameter1 Param2
CMD /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""
Launch demo1 and demo2:
CMD /c ""c:\Program Files\demo1.cmd" & "c:\Program Files\demo2.cmd""
Source: http://ss64.com/nt/cmd.html
The above answers helped me. But still required some figuring out. Here is an example script I use to start 3 processes for web development. It results in 3 windows staying open, as they need to run continously.
Mongo is globally added to my path, so I don't need to cd like I do for the other two programs. Of course the path to your files will vary, but hopefully this will help.
:: Start MongoDB
start cmd.exe /k "mongod"
:: cd app directory, and start it
cd my-app
start cmd.exe /k "npm run dev"
:: cd to api server, and start that
cd ../my-app-api
start cmd.exe /k "npm run dev"
This is not very easy.
The best approach is to have the part of your script that you want to be executed in a "new window" to be in a separate .bat file. This might be impractical if e.g. you need a lot of state from the rest of your script (variables, etc). One option is to pass any values you need (e.g. dir to operate in) to the batch file:
start cmd.exe stuff.bat %this_dir%
If you have a large amount of state to transmit you might consider generating a batch file at runtime:
set foo=Hello, World
set list_me=%userprofile%
set tmpdir=c:\windows\temp
set tmp=%tmpdir%\tmp.foo
del /q /f "%tmp%"
echo.echo %foo%>>"%tmp%"
echo.dir "%list_me%">>>"%tmp"
start cmd.exe "%tmp%"
del /q /f "%tmp%"
Obviously this is a trivial example.
Thanks to all here in Stack Overflow; this solution solves the above question but is extended to automatically run these tasks:
I want to run my rails server
Run a rake jobs:worker for my delayed_job gem too
and Open default internet browser to show my page
finally, to leave a cmd window open for any extra commands during my session.
I guess my project is called "antiquorum."
Create an "init.bat" file in your %USERPROFILE% directory (open a cmd window and take a look at the path to the left of the cursor to know what %USERPROFILE% is)
#echo off
cd C:/projects/rails3/antiquorum
if "%1" == "antiquorum" GOTO start
if "%1" == "worker" GOTO worker
if "%1" == "server" GOTO server
if "%1" == "" GOTO end
:start
start cmd /k %USERPROFILE%\init.bat worker
start cmd /k %USERPROFILE%\init.bat server
TIMEOUT 30
start "" "http://localhost:3000/"
GOTO end
:server
rails s
GOTO end
:worker
rake jobs:work
:end
In a new command line window type: C:> init antiquorum
The code opens two more cmd windows and a browser. TIMEOUT avoids errors in the browser.
The :start section does the work.
You can run tasks 1,2 or 4 separately by typing params as: server, worker, or none to leave a cmd opened in root of "antiquorum" project.
Enjoy.
Adding /k between two commands executes both command in order.
Example:
cmd /k echo "hello"
this command will first open command prompt then execute echo "hello" command
If I understand you correctly doing this in side your bat file will open Command prompt and print your message to screen.
cmd.exe hello world
hope this helps.
to run a python file in a new cmd window with spaces in the file name:
start cmd.exe /k python "C:\Program Files\HelloWorld.py"
Extending answer from #Dan Zuzevich. Below is CD "Change Directory" if your target file is on different drive. For example it have to call file from drive C and drive D.
:: Start MongoDB
start cmd.exe /k "mongod"
:: cd app directory, and start it
cd C:\project\dev
C:
start cmd.exe /k "npm run dev"
:: cd to api server, and start that
cd D:\api\server
D:
start cmd.exe /k "npm run dev"
I wanted my window to remain open after I killed or restarted Firebase so I used two batch files.
Desktop file for easy access: run_firebase.bat:
--------------START FILE CONTENTS--------------
start cmd /k C:\dev\init_firebase_batch.bat
---------------END FILE CONTENTS---------------
Batch file to run the intended results: C:\dev\init_firebase_batch.bat
--------------START FILE CONTENTS--------------
cd C:\dev\aptr_datasync\aperture-customer-hub
firebase emulators:start
---------------END FILE CONTENTS---------------
So ... double click run_firebase.bat and it runs the second batch. After Ctrl+C out of my process the window remains open. Seems trivial but I don't like distractions while I'm working on code.
I only spent time solving because I thought it would be simple. Hopefully this creates that simplicity for others.
I think this checks off the questions in the initial post:
[x] I'm trying to open a new command window in a BAT file
[x] After it opens, I'd like to execute a BAT command in the new window
I needed to run an environment and then use python while in that environment.
If my folder structure is:
C:\environments>
C:\environments\encrypted_stuff>
C:\environments\encrypted_stuff\p1>
C:\environments\encrypted_stuff\p2>
And I wanted to activate the "encrypted_stuff" environment then call a script from p2, I might type this in a bat that is stored in C:\environments\p2.bat
start cmd /k "Scripts\activate && cd p2 && python p2_script.py"
Then I can have a bat file for any of the scripts and all would use my "encrypted_stuff" environment. Now I can click the bat file from the desktop and it will load my specific environment and then run the script in that environment.

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