I'm required to start a series of python scripts and/or other windows executables. Some of these require a Windows system, others require a Linux machine.
Currently there are designated machines to run the OS-dependent programs. So I know where I want to start which program.
Is there a way to start a python script (or a windows executable) from a python script, on the local network, on another computer (e.g. run 192.168.0.101:/dir/python_script_123.py?
The script, which should then run various programs may then look something like this in pseudo code..
linuxip = 192.168.0.101
linuxparam = "required parameter"
winip = 192.168.0.201
winparam = "required parameter"
#option 1 (run all), 2(run linux only), 3(run windows only), 4(run local only)
option = 1
if option == 1:
magic_things.run("linuxip:/dir/linux_script.py" + linuxparam)
magic_things.run("winip:C:\\dir\\windows_prog.exe" + winparam)
subprocess.call(["/dir/local_script.py","parameter"])
subprocess.call(["/dir/another_local_script.py","parameter"])
elif option ==2:
[...]
You need to connect to your server machine from your client. In case of the linux machine you could use SSH.
see http://en.wikipedia.org/wiki/Secure_Shell
Assuming you have a ssh server on the linux server running you could use the package paramiko (http://docs.paramiko.org/en/1.15/api/client.html) to connect to the machine and run your script there.
This could look something like this:
from paramiko.client import SSHClient
client = SSHClient()
client.load_system_host_keys()
client.connect('linuxip', username='your_user', password='very_secret')
stdin, stdout, stderr = client.exec_command('python /home/your_user/your/path/to/scripty.py')
However please note that it's not very secure to store passwords in scripts and it's probably better to use a public/private key authentication (See the wiki article).
The paramiko package also offers the option for an ssh server, so this might be a solution for your windows machine, but I am not very sure as I don't run any windows machines any more.
Hope this was helpful!
David
install ipython and ipython kernel on the remote server, and ipython and ipython kernel on the local machine. Then you can connect to the remote server using the settings here: https://stackoverflow.com/a/48332182/4752883
and run any program that would run on the remote machine using subprocess or the os builtin libraries. Further this is OS independent, so it will work whether your client/server is linux or Windows or Mac
Related
Background:
Through VMWare Fusion installed on my MacBook, I have Windows installed virtually in the VMWare Fusion environment. On the Windows, I have Pycharm IDE through which I run automated python program to control bench instruments from Keysight and Techroniks. No issues.
PS- The instrument drivers are available only for Windows, thats the reason I am using Windows virtually on MacBook
Question:
From Pycharm (installed on virtual Windows), I would like to send any command (say, print Hello World) to the Terminal of the MacBook.
How to do this and what would be the command syntax (or package needed)?
There is no single package to do this.
At a minimum, your Mac host would need to run a server process. Then the VM would need to be on a host network bridge such that it is remotely addressable. Then, you can write a client that sends RPC requests to the host's server process.
At a low-level, you can use socket library, but you may want something higher level like httpserver.
Related - VMWare fusion: connecting to host's web server from guest
The other option without external dependencies would be to communicate over a file-system share.
If you want to install external software, then you can introduce a remote message queue or database.
Trying to have a python script open an app and then continue on. I have the following setup:
Windows PowerShell (PS) running on PC.
Mac Terminal running bash. ssh... into the PC.
In both windows I get the same "Windows PowerShell, Copyright (C) Microsoft..." etc. info and the same PS C:\... prompt. So it seems I'm in the right place.
Tried various things. In all cases, anything I do directly on the PC that successfully opens the app, when done at the same prompt in the ssh session on the Mac, gives all the same feedback, but doesn't actually open the app.
Things I've tried:
py .\open_mt5.py at the PS prompt, where that script is:
import subprocess
result = subprocess.Popen(['C:\\Program Files\\MetaTrader 5\\terminal64.exe'])
print(result)
py to start python REPL, then enter the above lines one by one. Notably, in both setups, the python REPL prompt becomes available immediately (as expected) and the output is the same, but again, the app doesn't open on the PC.
Just execute the app from the PS prompt: C:\'Program Files'\'MetaTrader 5'\terminal64.exe
...and a few other things.
I think this should work, but it doesn't. What am I missing?
Or, speaking to the end goal specifically: how can I have my python script, run via ssh, open that app on the PC (and of course not have that block the script from continuing)?
Thanks!
Couple of other details if it helps:
It seems the hiccup is somewhere in the ssh, since that's the difference in all cases. But it also seems to be a Windows thing, because if I ssh into another Mac and enter commands to open Mac apps, they open on the remote Mac, (as expected).
Everything else I've ever tried via ssh from the Mac (to a Mac or a PC) works the same as it does typing directly into the same prompt on the remote machine, including running an entire python based REST API server on the PC. So I'm pretty sure everything is configured more or less right.
The app in question needs to be running for my REST API to work as it includes a service that provides some of the functionality. My end goal is have the python script open the app at the start of everything it's doing to set up and run the REST API server, and do so without blocking the rest of the script of course.
New to python. Self-teaching. Desire to learn it (basics at least). New to Windows command line (cmd, PowerShell). Self-teaching. No desire to learn any more than required for specific project. (Mac/unix user normally). Hoping people can reply at that level. 😊
Mac: macOS Monterey 12.4. PC: Windows 10 Home 21H2, python 3.10.5, and the default Win 10 OpenSSH Server.
I want to do a python script that is able to copy log files from a remote windows 10 virtual machine to the script's machine (Windows) as well as deleting files. A developer in my work place uses WMI with C# to do these kind of stuff but I haven't been able to find anything for Python regarding this topic.
You can use SSH for that.
Paramiko is an awesome library that can run SSH in python: http://www.paramiko.org/
I want to write a script that executes command on remote windows machine, and get output from that command. The problem is, I want to use built-in software in windows, so I can't unfortunately use for example SSH for that. I found "wmi" library, and I can log in to the machine and execute command, but i don't know how to recieve output from that command.
import wmi
c = wmi.WMI("10.0.0.2", user="administrator", password="admin")
process_startup = c.Win32_ProcessStartup.new()
process_id, result = c.Win32_Process.Create (r'cmd /c ping 10.0.0.1 -n 1 > c:\temp\temp.txt')
if result == 0:
print("Process started successfully: %d" % process_id)
print(result)
I tried to redirect output to file, but I can't find any way to get text file content either.
Is there any possible way to get output or text file content using wmi, or other python libraries?
For the application you're describing you may find RPyC is a good fit. It's a python remoting server you can connect to and issue commands; it will do anything that Python can do on the target machine. So you could, for example, use the subprocess module to run a windows command and capture the output then return the result.
The safe thing to do is to expose your remote functionality as a service -- basically, it's just a python script that RPyC can run for you. However you can also use RPyC classic mode, which is pretty much like running a python session directly on the remote machine. You should use it only when you are not worried about security, since classic mode can do anything on the remote machine -- but it's useful for prototyping.
I have made a connection between my openssh client and server. Both sides are window machine. I fail to do the followings:
Enter the python command line interface on my SSH client side, which can be done by entering "python" directly through a command line.
I want to run a python script on a server side by calling it on my client side. This script calls a commercial library to download data from a commercial database.
I can do both perfectly through remote desktop on the server side, but when I use openssh to connect, all fail. Any way to solve, especially item (2)?
If Python is installed on the remote server as C:\Python27 and the script is C:\Foo\bar.py, then get PuTTY. It contains the program plink.exe which can execute remote commands.
The command for you should be
plink.exe user#remote C:\Python27\python C:\Foo\bar.py
as explained in http://the.earth.li/~sgtatham/putty/0.64/htmldoc/Chapter7.html#plink-usage