I am writing an application with a server client type of framework. To
implement the logic,
- I need to transfer a zipped folder to a Windows server,
- unzip the folder and
- then run a certain python file within the folder.
I am trying to recreate the steps manually, so that I can then redo it using
python libraries (such as paramiko
and zipfile
or other python libraries)
So far I have tried the following tests to check whether I can reliably run a
python script using ssh:
-
Using OpenSSH from Windows 10:
https://github.com/PowerShell/Win32-OpenSSH/wiki/Install-Win32-OpenSSH
After some configuration, I was able to copy files from the client side to
the server usingscp
. The next plan was to use ssh to remotely run a
specific file usingpython filename.py
Then cd'ed into the folder and tried running python filename.py. However,
there was no output frompython
. -
Next I tried MobaSSH, and the problem was the same as the test with OpenSSH
from Microsoft.
`Fatal Python error: Py_Initialize: can't initialize sys standard streams
LookupError: unknown encoding: cp28591Current thread 0x00000874 (most recent call first):`
-
From the above 2 experiments, I initially thought there was a problem with
my python installation. So I directly logged into the machine, and use
cmd
to run the same file as ,python filename.py
, it does run and
produce the expected outputs. So python does run fine on the Windows
machine -
My next hypothesis was that its impossible to run python if run through
SSH. To test this I logged into my ubuntu VM, and did the same experiment.
In this case, python ran fine, with no errors.
From these experiments, it seems that if I log into Windows using ssh
, and try
to run python, I cant run it maybe because of a quirk of python or of Windows.
But if I am directly logged into Windows, I can run the same python script
fine. Further, it seems ssh does work flawlessly in a linux environement.
I do need to run the python script in a Windows environment, not a linux
environment because of some other considerations. So even though running the
test file worked fine in the ubuntu VM, I cant use that environment. .
Is there a way to run a Python script when logged in via SSH in a Windows
environment?
Best Answer
I had some success with the following:
1.After logging in start cmd.exe
2.Set a codepage, for example 850 (from within cmd.exe)
3.Set the PATH and other environment variables (from within cmd.exe)
You can put the chcp and set commands into a batch file that you run after logging in.
To find out the desired values you could start a lokal cmd shell on the target machine or on another windows machine that is similar enough and run:
To write all variables directly to a file run on the target machine from a local cmd shell:
This has to be edited manually to prepend each line with a ´set ´ to create a script you can then run after logging in remotely.
It is even possible to login, start ssh and execute the script in one line or in a bash script:
The /K switch of cmd will run the following command (without exit after running it).