1

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)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
gradole
  • 89
  • 11
  • @MartinPrikry l I've mentioned I'm using the ssh server coming as part of Windows RS4 (you install it by demand in "apps and features"). I use paramiko frequently, most commands I've tried works, even other powershell commands. I didn't have success with commands that requesting to run as administrator. I interested to know why this specific command works with ssh but not with paramiko. – gradole Sep 26 '18 at 09:07
  • @MartinPrikryl I don't see it asking. There's a part in my python script that works only with admin privileges and it seems to work when executed using the terminal. I also tired to disable UAC, but it's behave the same. – gradole Sep 26 '18 at 10:08

1 Answers1

1

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".

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992