PHP - Execute python script on remote IP address and get result - python

here is what I am trying to do:
I have a PHP application running on a web server. There is also an other virtual machine (in the same network with the appropriate permissions) where there is a python script. Now, what I want to do is from the PHP to run the python script on the remote machine and get back the result.
The following code runs correctly on the cmd on the server.
cd C:\pstools
psexec.exe \\192.168.0.ip cmd
cd "path of the python script on the remote machine"
python main.py
This works great and returns the result.
Now... how do I run this from PHP? I already tried with exec commands and I also tried with .bat file. I mean, to right the code on a batch file and execute it with the exec command through PHP.
The batch files looks like this:
cd /d C:\pstools
psexec.exe -h -accepteula \\192.168.0.ip cmd
cd "path of the python script on the remote machine"
python main.py
This finishes without an error, but doesn't seem to actually run the cmd on the remote machine. I've also tried some variations of the psexec command, like adding the path of the file on the same command, but still no luck.
Any idea on how to address this would be appreciated!
Thank you all in advance :)

Related

which python function to use to run bash script on remote server? subprocess.Popen, call? new to python

I am running on python 2.7.5. I am converting some bash script to python (learning as I coding) and I'm confused on which is the better solution? I need to copy files to a remote server and then run a script on that remote server using the file. I was using Popen
ssh = subprocess.Popen(["ssh", desthost, "cd {}; {} {}".format(remote_dir, scriptname, filename)]
But it doesn't read my cd. I see that I can use call but I am not clear on if I can get error output from it like I could if I used popen. I keep seeing fabric being mentioned but I didn't know it this could really be done in a one liner. Any advice is appreciated.

Run python script right after RPi boot

I'm relatively new to raspberry pi (5 days using it) and I've just finished to run my python script succesfully (called dogcare.py). Now I'm trying to execute this script right after my raspberry is turned on. I've been doing some research and I find different ways to do it:
using /etc/profile
using /etc/rc.local
using crontab
using /etc/init.d
using systemd
But none of these ways are working for me.
Setup enviroment:
Hardware: RaspberryPi 2 Model B
Software: Raspbian or NOOBs (not sure)
Context:
Since for my project I need to run meet.jit.si, I followed this guide http://www.instructables.com/id/Video-Calling-on-Raspberry-Pi-3/?ALLSTEPS and It has a step where sets chromium website to start right after RPi is turned on. (Currently this is working fine)
My python script is using request library in order to use HTTP GET with an external website application I've been working on.
Main problem:
I need to run both events: chromium website with meet.jit.si and my python script when my raspberry is turned on.
Current situation: chromium website is running after my RPi is turned on but my script doesn't.
I'd appreciate any help !
I have done a similar thing with my Raspi 2 as well which involved sending myself an email with the ip address of the pi so I could easily ssh/vnc to it.
My steps involved making a shell script which ran the python program.
#!/bin/sh
cd pythonfiledirectory
sudo python pythonfile.py
cd /
Then I made it executable with the following command:
chmod 777 file.sh
Now edit your crontab to run the file on startup.
In your terminal, type:
sudo crontab -e
Inside of the crontab write:
#reboot sh file.sh
You could add a log file if you wanted to debug and see why it's not working by making a log directory and changing the text you wrote in the crontab to:
#reboot sh file.sh >/logdirectoy/ 2>&1
This is what made it work for me and if it doesn't work try and make sure you can run your .sh file and try the crontab with some other files to debug the problem.

SSH to a server and exit Python script (in the same shell)

I'm running a script that's determining login information for me, and in the end is outputting the login information that I need to use.
I am running the script in a terminal, and now I want it to SSH me with the credentials it has, exit the Python script on my computer and connect my current terminal to the new server.
Say I already have my sshHost, sshUser and sshPass as variables in the script. How do I run an SSH command in the current terminal and connect to that server?
I tried subprocess and spur, however I didn't really manage to get that going.
I would really appreciate your help and thanks in advance.
assuming the python prints the settings to stdout;
#!/bin/sh
export $(credentials.py)
exec ssh hostname

How do I execute a remote Python file from a .sh executable?

I am using a Mac.
I have a Python file on a server. Let's say it's here: http://jake.com/python_file.py. I want to make a script that I can run in Terminal by double-clicking and will run the Python file in Terminal on the local machine. Any ideas?
Would I have to use SSH to connect to the server and download the file to a temporary location on the hard drive (/tmp?) and then delete it when I'm done? But there's another problem with this. It would have to download the Python file to a location in the user's home folder because I don't think users have the necessary permissions to write to the folder /tmp or /var or something like that.
I was looking around for a solution to my problem and found this. It talks about how to execute a remote script using SSH on unix but I tried doing this with my Python file and it didn't work.
In case you didn't realize, the main reason why I am looking to do this is so that a user can run the file locally but they are unable to read/edit the actual Python file which is stored on the server.
If anyone has any ideas on how to accomplish this (whether using the ideas mentioned above or not) please let me know I'd really appreciate it!
Thanks,
Jake
How about a python script to download and execute the code like so?
import requests
py1 = requests.get('https://example.com/file.py').content
exec(py1, globals(), locals())
note: I'm using the requests library, but you could just as easily use the built in httplib's HTTPSConnection. It's just more verbose.
note 2: When you say you don't want the user to be able to "read/edit the actual Python file", they will be able to read it if they open the url themselves and view the content. they are just less likely to edit the file locally and mess something up. You can also deploy updates to your python script to the URL rather than having to copy them locally to every machine. This could be a security risk depending on the scope of what you are using it for.
Assuming that the remote Python script is a single file with no dependencies other than those of the Python standard library, and that a compatible version of Python is installed on the user's local machine, there are a few obvious ways to do it.
Use ssh:
#!/usr/bin/env sh
ssh user#host cat /path/to/python/script.py | python
Or use scp:
#!/usr/bin/env sh
TMPFILE=/tmp/$$
scp user#host:/path/to/python/script.py $TMPFILE
python $TMPFILE
rm $TMPFILE
The first one has the obvious advantage of not requiring any mucking about with copying files and cleaning up afterwards.
If you wanted to execute the python script on the remote server, this can also be done with ssh:
ssh user#host python /path/to/python/script.py
Standard input and output will be a terminal on the user's local machine.

Error setting env thru subprocess.call to run a python script on a remote linux machine

I am running a python script on a windows machine to invoke another python script on a remote linux machine. I am using subprocess.call with ssh to do this, like below:
subprocess.call('ssh -i <identify file> username#hostname python <script_on_linux_machine>') and this works fine.
However, if I want to set some environment variables, like below:
subprocess.call('ssh -i <identify file> username#hostname python <script_on_linux_machine>', env={key1:value1}) it fails.
I get the following error:
ssh_connect: getnameinfo failed
ssh: connect to host <hostname> port 22: Operation not permitted
255
I've tried splitting the ssh commands into list and passing. Didn't help.
I've tried to run other 'local'(windows) commands thru subprocess.call() and tried setting the env. It works fine.
I've tried to run other commands(such as ls) on the remote linux machine. Again, subprocess.call() works fine, as long as I don't try to set the environment.
What am I doing wrong? Would I be able to set the environment for a python script on a remote machine? Any help will be appreciated.
To set the environment on the remote side, you will need to do calls on the remote side. Try writing and uploading a wrapper script which does this env setting, e.g.
import subprocess
import sys
args = sys.argv[1:]
env = dict(zip(args[::2], args[1::2]))
subprocess.call(['python', 'script.py'], env=env)
Now you just have to pass this information in your original call, e.g.
subprocess.call('ssh -i <identify file> username#hostname '
'python <script_on_linux_machine> %s %s' % (key, value))
Or some more extensible method of converting a dict to the required format.

Categories

Resources