Transfer file from local machine to remote windows server using python - python

How can I easily copy a local file to a remote server using python?
I don't want to map the drive to my local machine and the windows server requires a username and password.
The local machine is also a windows machine.
Examples I've seen are with linux and mapping the drive. Unfortunately that's not an option for me.

You can use the subprocess module or os.system to launch a command into a windows shell. Then you can use Powershell or cmd instructions.

Related

PyCharm remote debug using remote container Python interpreter

My situation is that I have set up a container in a remote server, and inside the container, there is a virtual environment. I'm using the python interpreter inside this virtual environment in this container, not the one on the host.
From my local machine, I can open up PyCharm, and use Tools->Deployment->Configuration to easily set up a remote connection. And For a specific project, I can set up the interpreter by clicking Files->Settings->Project Interpreter. However, it seems that I can only select the host Python interpreter(/usr/bin/python) on the remote server, not the one inside the virtual environment in the container. How could I set up using this interpreter?
I googled but can't find exact solution. I don't think I need to install Docker locally because my Docker is on the remote server side, right?
In similar way you are connecting to remote host - you would need to setup container with same capabilities e.g. set ssh server running on there. Then you should expose the port into public world or use nested ssh tunnel, which would be better alternative.
Another interesting approach (maybe recommended) is to forward Docker socket from the remote machine so, that you local Docker CLI uses this socket for sending commands to remote host. Theoretically, then you could add this container directly in PyCharm, when you set correct Docker host address there.
Further, virtual environments on other than local host systems are not supported natively by PyCharm. However, you could try to add path of python and see if it works e.g. venv/bin/python from project directory.

How to run python script at my mac's terminal after being connected to GCP virtual machine?

I followed the instructions from this answer (https://datascience.stackexchange.com/questions/27352/how-to-run-a-python-script-on-gcp-compute-engine) to connect to a virtual machine which I created on GCP Compute Engine and run a python script from the terminal of my laptop on GCP Compute Engine.
This answer suggests that after I have connected to the virtual machine then I must enter the following at the terminal of my laptop: python your_script.py
However, when I point to the exact location of my .py file and enter:
python /Users/Paul/PycharmProjects/Eyeglasses_colour/Main.py
the response is the following:
python: can't open file '/Users/Paul/PycharmProjects/Eyeglasses_colour/Main.py': [Errno 2] No such file or directory
What is wrong? Can't I run my python script (while connected at the GCP virtual machine)?
You will not have access to any /Users folders on a Google Cloud machine because they're running Linux, and that's commonly a Mac path.
That link assumes your files are already on the server. There are several ways that can happen.
Code was initially written there
You checkout the code from version control.
You scp the files using gcloud compute scp
This problem does not apply to Python or Google's services. It's applicable to all remote SSH sessions

How to use ssh with python on windows without paramiko?

On machine A I want to have a small python script to execute a command on machine B. Machine A is Windows with freesshd running on it, and machine B can be either Windows or Linux. I only have a username and a password to log in to machine B, no other way of authentication.
How can I create a python script to run a command on machine B? ssh would be fine, but where to find that command on Windows? And it has to be done without paramiko, since this module does not wok for unknown reasons.
If you can't get paramiko working, just do it manually with Popen: http://python-for-system-administrators.readthedocs.org/en/latest/ssh.html
I haven't tried this on Windows but to handle input and output of a command pexpect is a good candidate. It also has a ssh part http://pexpect.readthedocs.org/en/latest/api/pxssh.html.
Did some more digging. There is a windows port of pexpect: https://gist.github.com/anthonyeden/8488763
Use that along with putty on the command line in windows.
Example of Putty on command line: http://etherealmind.com/putty-command-line/

write python code in one computer and running it on another automatically

I have the following situation.
I want to write python code in my Laptop that will take more than 24 hours to run.I am using UBUNTU 12.04 lts.
Is it possible so that I write python code in my laptop, automatically send it to some remote desktop, run there and send the output result to my laptop when done?
one way suggested to me is to use openssh.
But I want to do this in the following way----
Write and Debug Python Code in my laptop.(Solved)
email the code as attachment to ****#gmail.com(Solved)
Other python program in the desktop will automatically download and run the source code(Unsolved)
and email the output file back to my gmail id.(Solved)
what is the python code to download the attachment from the latest email from a specific gmail folder?
If your remote system is windows, a good option would be to use PsExec from SysInternals.
Ex. If your script is long_running.py a typical usage would be
PsExec \\remote-server -c long_running.py
If your remote system is *nix, and your local system is Windows, you can use ssh for remote execution via Plink (part of PuTTY).
plink remote-server#user -m long_running.py
Finally if both remote and local machine are *nix systems, you can simply use ssh
ssh remote-server#user 'bash -s' < long_running.py
Note This is just some possible options, but the idea is remote execution is possible either via ssh or a similar option (like PsExec) for Windows
If both systems are running *nix, you can easily do all your dev work and debugging locally, while still executing remotely:
One time set up:
Mount a folder from the remote box locally
On your laptop, save your project/script to that (now local) folder, or set the mounted folder as your project's save path in your IDE.
Publishing:
Do work
Click the save button
Executing:
SSH into the remote box and open a new screen
Navigate to the folder you'd previously mounted, and run your script.
You can then safely detach and close ssh if necessary (ctrl+a d), and re-attach later:
3a. screen -ls (to find the screen name)
3b. screen -x screen_name
The advantage of this solution is that if you've got an ongoing project requiring frequent edits/changes, you can do all your dev work/debugging locally, and the only work required to "publish" is clicking the save button, starting the screen, and running.

Python: run a process inside a windows host

I need to run a process inside a windows host from a linux host using python.
I saw WMI python library but seems to work only from a windows host, not linux, how can I do?
I neeed something like PSEXEC, but it works only for windows.
Since you cannot install anything on the windows host, does it have openoffice, or libreoffice installed?
You coult then use the Python interpreter bundled with it to create a python script that listens to xmlrpc and get you job done, controling this script remotely.
If there is no Python installed on the windows side, and yur remote host is Linux, there is this recipe here: http://code.activestate.com/recipes/577945-execute-remote-commands-on-windows-like-psexec/
It does depend on windows - as it imports win32wnet - in my experience, these windows specific modules will run fine in a Windows python installed on a Wine configuration on a Linux machinne.
An easy solution is to run a SSH server on the windows box and use the paramiko library on the client side.

Categories

Resources