How to run an script remotely

Hello,

As an example, I have this code:

import os
os.system(‘calc.exe’)

If I run it locally, it works (Pycharm and CMD). But I tried to run from an external tool, it does not work. Someone said that I need to run it as a SYSTEM in interactive mode with PSEXEC. He sent me this sentence:

psexec -i -s calc.exe

But when I run my script with that sentence in the ("") it does not work.

Does anyone know how can I use it? Thanks a lot.

Regards,

Hi Indira.

We need more information to even begin to diagnose this.

I assume that the local machine where the command works is using
Windows as the operating system. What about the remote machine where
the command doesn’t work?

What does “it does not work” mean? Do you get a Python traceback, some
other form of error message, a segfault or blue screen, something else?

If you run os.system("calc.exe") locally on the remote machine, does
it still fail?

What is the output of os.system? What is it’s return result? (The return
result should be an error code, 0 for no error.)

On my machine, running os.system('calc.exe') prints

sh: calc.exe: command not found

to stderr, and returns an error code of 32512.

How are you running the script remotely? Are you using telnet, ssh,
something else?

I am not a Windows expert, but if this was Linux, I would say you need
to set up X forwarding. calc.exe is a GUI app, and to run GUI apps
remotely over X, you can’t just telnet or ssh into the remote system and
try to run them. If you do, you get an error like this:

[steve@ando ~]$ python3 -c “import os; os.system(’/usr/bin/kwrite’)”
kwrite: cannot connect to X server

The solution is to setup remote access to X first.

If you are using Windows, I expect that there is some equivalent issue
that you need to solve, but I have no idea how. Possibly the easiest way
is to use something like Remote Desktop:

  • use Remote Desktop to connect to the other machine;

  • run your Python script on the remote machine, using Remote Desktop.

If I run it locally it works. I used PyCharm and CMD. With both works.

I used another system (XDR Consoles from Palo Alto Networks), in where I establish a “Live Terminal” session. In there I can run CMD and Python scripts. But when I run it from there, it does nothing.

Regards,

Have you tried using Remote Desktop like I said?

“Live Terminal” sounds like it is probably using telnet or sshto connect
to the remote machine. So when you try to run calc.exe on the remote
machine, either:

  • the attempt to run the application fails, in which case I would expect
    that os.system hopefully will return an error code.

  • or it succeeds, and the calculator application runs on the remote
    machine, where you cannot see it running.

(You can’t see the Windows desktop and the GUI in a terminal.)

In that second case, it is likely that the calculator opens and then
instantly quits.

As I asked earlier, you need to check the return result of os.system. If
it returns an error code, that might help you diagnose the error. If the
error code is 0 (“success”), unfortunately that means nothing.

What effect are you trying to get? Is calc.exe supposed to run on the
remote machine? How do you expect to click the buttons from a terminal?
Or are you hoping to get the calculator to run on your machine?

I think the best you can hope for is to use Remote Desktop, which will
give you a remote connection to the other machine. You can then run the
calculator application on that machine, and still see the GUI and click
on the buttons. But I’m not a Windows expect.

Hello, Steven. It is because I can’t run the script from RDP. I need to run it in Live Terminal.

As you can see in the image I establish a Python terminal. When I run the script is successful, but it does nothing. If I run the same script on the laptop it works. So the script is fine. But I need to run the sentence with PSEXEC.

Regards,

If you can run calc.exe using Python on the remote machine, you can run
calc.exe using Python on the remote machine over RDP. That’s how RDP
works.

  1. Make sure Python is installed on the remote machine. Can you run
    Python on that machine? If yes, go to the second step.

  2. Make sure you can call os.system(‘calc.exe’) on the remote machine
    from a script. If you can do that, go to the third step.

  3. Change computers. Go to another machine, use RDP to connect to the
    first machine, and run the script exactly the same way you ran it in
    step 2, except you are using the RDP.

If steps 1 and 2 work, but step 3 doesn’t, then you don’t have a Python
problem, you have a Windows problem. You need a Windows Remote Desktop
expert to help you.

If you want to run a GUI application like calc.exe, you cannot do it
with LiveTerminal you need a connection to the Windows desktop, and that
means RDP or something equivalent.

Quote: “On Windows endpoints, you cannot run GUI-based cmd commands”

https://docs.paloaltonetworks.com/cortex/cortex-xdr/cortex-xdr-pro-admin/investigation-and-response/response-actions/initiate-a-live-terminal-session.html

Using LiveTerminal will not help you. It doesn’t matter what command you
run using os.system, LiveTerminal cannot run GUI applications on the
remote machine. Putting Python in between the LiveTerminal and Windows
won’t change that. You need to go back to Remote Desktop and use it.

Hello, Steven. Thanks for your answer.

Ok. I did the script of “calc.exe” as a test. But my issue is that no command is working. The idea of using Cortex XDR is because we need to uninstall many McAfee agents. And the commandos are not working. If a do an RDP, I have to uninstall it manually one by one. Below is the script:

import os
import sys

os.system(‘copy “\endpoint\EndPoint\Cortex\McAfeeEndpointProductRemoval_21.8.0.99.exe” E:\Instaladores’)
os.system(“E:\Instaladores\McAfeeEndpointProductRemoval_21.8.0.99.exe --all –-accepteula --noreboot”)

Regards,