Executing a curl command on the client side - python

Let us say that I am running on a Debian machine a server with sudo python3 -m http.server. Are there possibilities to execute some curl command on the client-side if he opens the website?
My idea is to provide an installation page for my program, so if the user opens it (http://127.0.0.1/install), the commands would be automatically executed in the background. I am aware that there are security reasons why this shouldn't be possible, but let's say the user is okay with it. :)
I have found online something about cgi python scripts, but I never found any example to do what I want and that is why I am not really sure if it is the right thing or not.

Related

What is the terminal script to run CoAP server

Im trying to run this CoAP server from https://github.com/Tanganelli/CoAPthon on a raspberry pi. i cant seem to find much instructions. This was one of the CoAP that my instructor wanted us to use, my instructor pretty much left us hanging with no help at all but this link.
i followed the install instructions on the github page, the only thing that wouldnt install is the section
Install instructions for CoRE Resource Directory. Mongod wouldnt install for this section so i gave up. so i dont know if this section is important or not
What are the commands for the terminal to get it running and doing something.
it says to type in to run the server coapserver.py.
cd CoAPthon
python coapserver.py
and from what it looks like its running
but nothing happens, so im not sure if its just not working or if theres just nothing there for it to do, or if there is another file i need to run with the coapserver.py.
Im very new to using CoAP and will eventually need to add a sensor to it do record temp but i want to make sure i know the server is running properly before i add that part in
any input would be great
For future individuals that might need help with this, use aioCoAP, its much easier then CoAPthon
get it from here https://aiocoap.readthedocs.io/en/latest/installation.htmlg
all you need to do is mess with three files labeled server.py, clientPUT.py and clientGET.py
Add resources and classes for sensors and what not to server.py
ClientPUT.py is where you would add your code for the sensors or whatever you need.
ClinetGET.py you alter the uri
run it by type in the command in the terminal (go to directory first that has the server) type in python server.py
in another terminal do the same thing but instead run python clientPUT.py
and the server should be running
then all you need is a client for the get request. If you were like me and needed to use Copper go here to install it for chrome https://github.com/mkovatsc/Copper4Cr

Is there a way of sending commands to a terminal application in Python

I am running linux as you might have already figured out.
So I wrote a gameserver panel in PHP and Python that can install, start and stop any game I write a script for. After some testing with mumble, I am now adding games to the list and my first game to be supported will be quakelive. What I want to achieve is that I can send commands to any application that is running in a terminal and also read the output to the console window.. My next steps will be Minecraft and some other games. I know that these 2 games have a query port, but a) I don't know how to send a query from php to this and b) I want to potentially support every game there is.
Just for the record, I can extract a process id from a running process already, if that helps.
So I ran ps aux | grep qzero and it says that the server is opened on pts/2. So the command I am testing with is pause, when I type it in manually, it echos back some stuff.
Online I found the command
echo pause > /dev/pts/2
But all it really does is echoing pause into the console window.
I have been searching online but I think I don't really know what to look for. Maybe it has something to do with this whole stdin and stdout stuff.
I know this is possible in other languages since I've fiddled around in PufferPanel and they have this feature, too. But it has some flaws so I decided to code my own panel.
So actually it was not that hard. I did not find a solution to send it directly to a terminal, but with the help of tmux, it could be done.
You basically start a session and then
tmux send-keys -t sessionname -l command
tmux send-keys -t sessionname Enter
And this is no problem integrating into an application in Python.
see further https://serverfault.com/questions/178457/can-i-send-some-text-to-the-stdin-of-an-active-process-running-in-a-screen-sessi
Can also be done in screen apparently.

using dbus and polkit to run a root privilege python service that calls a root script

I have written a python script that downloads deb files from launchpad, and then calls out to a shell script to use alien to convert the debs to rpms.
The shell script uses alien, and so it needs to be run as root.
The program uses a thread pool to download the deb files asynchronously, using threadpool.apply_async, and then uses a processing pool to call the shell script asynchronously, so the whole thing happens reasonably quickly.
It all works well, but the shell script that calls alien needs to be root otherwise packages don't get built properly. When I first finished the script, I would use pkexec to call alien, after using sudo. In both cases, I had a couple of problems.
The first was that in starting in root, I lost the environment of the user, and so lost the pip installed python libraries. I could, perhaps, have used sudo -s or similar, but the second problem was that I had to enter my root password for every package that was built.
What I want to do, is to run the python script, qt gui and all, as a normal user, select which files to convert, and then hit the install button and only enter my superuser password once.
I decided to filter out the install parts of the python, which include the threaded download, and threaded call to the shell script, and then try and run those parts as root/superuser.
I created a dbus service, for this install part, and, after a steep dbus learning curve, managed to get the service working. However, I had no joy getting the script authenticated, and raising its privileges.
I have been able to use polkit to show the password dialog and authorise the super user, but I do not know how to use the return value from polkit
`authority.CheckAuthorization(subject, action_id, details, flags, cancellation_id)`
which shows the password dialog, for authorisation, but does not handle elevating the scripts privileges.
I have set the python install service as 0500 perms, so that hopefully, once I have figured out how to elevate privileges, the root user has the ability to read and execute the service, which is currently created on the session bus.
How can I elevate permissions, and, at the same time, keep the environment variables of the user, so that I don't have to install python modules into the root account?
Many thanks for your help in advance...
ps. I have written a polkit action file, and a polkit rule, but in each case I am not sure how the action id relates to the elevation of privileges.
pps. Can I/should I use pam?
I eventually ran the process as root, using pkexec to obtain a password dialog.

Run python script on Google Cloud Compute Engine

I know this is an exact copy of this question, but I've been trying different solutions for a while and didn't come up with anything.
I have this simple script that uses PRAW to find posts on Reddit. It takes a while, so I need it to stay alive when I log out of the shell as well.
I tried to set it up as a start-up script, to use nohup in order to run it in the background, but none of this worked. I followed the quickstart and I can get the hello word app to run, but all these examples are for web applications and all I want is start a process on my VM and keep it running when I'm not connected, without using .yaml configuration files and such. Can somebody please point me in the right direction?
Well, at the end using nohup was the answer. I'm new to the GNU environment and I just assumed it didn't work when I first tried. My program was exiting with an error, but I didn't check the nohup.out file so I was unaware of it..
Anyway here is a detailed guide for future reference (Using Debian Stretch):
Make your script an executable
chmod +x myscript.py
Run the nohup command to execute the script in the background. The & option ensures that the process stays alive after exiting. I've added the shebang line to my python script so there's no need to call python here
nohup /path/to/script/myscript.py &
Logout from the shell if you want
logout
Done! Now your script is up and running. You can login back and make sure that your process is still alive by checking the output of this command:
ps -e | grep myscript.py

How to launch a python process in Windows SYSTEM account

I am writing a test application in python and to test some particular scenario, I need to launch my python child process in windows SYSTEM account.
I can do this by creating exe from my python script and then use that while creating windows service. But this option is not good for me because in future if I change anything in my python script then I have to regenerate exe every-time.
If anybody have any better idea about how to do this then please let me know.
Bishnu
Create a service that runs permanently.
Arrange for the service to have an IPC communications channel.
From your desktop python code, send messages to the service down that IPC channel. These messages specify the action to be taken by the service.
The service receives the message and performs the action. That is, executes the python code that the sender requests.
This allows you to decouple the service from the python code that it executes and so allows you to avoid repeatedly re-installing a service.
If you don't want to run in a service then you can use CreateProcessAsUser or similar APIs.
You could also use Windows Task Scheduler, it can run a script under SYSTEM account and its interface is easy (if you do not test too often :-) )
To run a file with system account privileges, you can use psexec. Download this :
Sysinternals
Then you may use :
os.system
or
subprocess.call
And execute:
PSEXEC -i -s -d CMD "path\to\yourfile"
Just came across this one - I know, a bit late, but anyway. I encountered a similar situation and I solved it with NSSM (Non_Sucking Service Manager). Basically, this program enables you to start any executable as a service, which I did with my Python executable and gave it the Python script I was testing on as a parameter.
So I could run the service and edit the script however I wanted. I just had to restart the service when I made any changes to the script.
One point for productive environments: Try not to rely on third party software like NSSM. You could also achieve this with the standard SC command (see this answer) or PowerShell (see this MS doc).

Categories

Resources