I want to execute a Python script as Administrator. I'm using the following command to do so:
powershell Start-Process python -ArgumentList "C:\Users\myuser\python_script.py","-param1", "param1","-param2","param2" -Verb "runAs"
This command works fine if I'm using traditional SSH using a terminal. Target machine is Windows RS4 and I'm using the new native SSH server available since RS3.
My client's Python code is:
import paramiko
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=myhost, username=myuser, password='trio_012')
stdin, stdout, stderror = ssh_client.exec_command("powershell Start-Process python -ArgumentList \"C:\Users\myuser\python_script.py\",\"-param1\", \"param1\",\"-param2\",\"param2\" -Verb \"runAs\"")
print 'stdout:', stdout.readlines()
print 'stderror:', stderror.readlines()
The output I'm getting:
stdout: []
stderror: []
I don't see the script is running on the other side and it seems like nothing happens. I don't know what is the problem cause I'm getting no output.
I'm using Paramiko 1.18.5 (I can't use the new v2, I'm having issues with known_hosts
file and Windows when using paramiko.AutoAddPolicy()
policy)
The process, started with Start-Process
, closes, when an SSH channel that started it is closed.
The "exec" channel (exec_command
) closes immediately after the powershell
process finishes, what is almost instantaneous. So I believe that your python
process is actually started, but is taken down almost immediately.
It should help, if you add -Wait
switch to Start-Process
.
stdin, stdout, stderror =
ssh_client.exec_command("powershell Start-Process python -ArgumentList \"C:\Users\myuser\python_script.py\",\"-param1\", \"param1\",\"-param2\",\"param2\" -Verb \"runAs\" -Wait")
It "works" from an SSH terminal, because the terminal (SSH "shell" channel) stays open after powershell
process finishes. But if you close the terminal before the python
process finishes, it would terminate it too.
Obligatory warning: Do not use AutoAddPolicy
– You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".