Search code examples
pythonwindowsexeopenssh

Running a .exe file on a Windows machine over ssh not working


I have to run a .exe file which takes 2 arguments like this-

File.exe Arg1 Arg2

I have openSSH setup and running on both the machines. Now I try to run the File using os.system("ssh remote@ip cd C:/path/to/File;\"./File.exe Arg1 Arg2\"")

However nothing happens, even though there is no error received. Everything works fine. I tried running ls, pwd, etc. and they work fine. Moreover I have also tried running a .bat file and it also works fine. What am I missing?


Solution

  • Probably (but it depends upon the command interpreter, e.g. your Unix shell if your local machine is a Unix system, or cmd.exe if your local machine runs Windows) your argument to os.system, that is the string ssh remote@ip cd C:/path/to/File;"./File.exe Arg1 Arg2" is parsed as a composite command, that is a sequence of two sub-commands separated by a semi-colon:

    ssh remote@ip cd C:/path/to/File
    

    which justs runs a (useless) cd on the ip host as user remote and nothing else

    then

    "./File.exe Arg1 Arg2"
    

    which tries (and fails) to run (on your local system) an executable named ./File.exe Arg1 Arg2 (files can have spaces, but that is ugly and you should avoid that)

    Perhaps you should do

    os.system("ssh remote@ip 'cd C:/path/to/File; ./File.exe Arg1 Arg2'")
    

    and you should test the result code of os.system

    Actually, if you are coding that ./File.exe, I would recommend having some program flag to change the working directory (the -C flag of make or of tar could be inspirational) in its code.

    Remember that each process (including your shell or your command interpreter) has its own working directory. Details are operating system specific.