Python os.system enter commands to execute in CLI application - python

I am currently writing a program to help me with some database administration. It's supposed to execute commands in MariaDB. I thought since this is a CLI application I can just do that with os.system, but I'm having problems doing so. So lets say I have the following code
import os
os.system('mysql --user=%s --password=%s' %(user, password)
os.system('USE database;')
This code logs me into MariaDB, but it does not execute the second command to choose the database. Is this possible with os.system, and if not, what are my alternatives? Thanks.
Edit: I tried using subprocess instead, which gives me another problem: It immediately exits MariaDB after logging in.

I recomand you to use Subprocess instead of os python module, using Popen
try to check this link out :
Calling an external command in Python

Related

Python exec user code on server - os command injection

I have a project where a user can provide a python script that will be execute on the server to verify if the script is valid according to some criteria. I use the exec function to execute the code but it's vulnerable. User can use the os module in the script.
Is there a way to prevent command injection ? By preventing some modules to be used in exec ? Or another way to execute python code without risking injections ?
Thanks !

Opening third-party console and running commands

I am trying to write a script to open a third-party console application and run commands on Windows. The third-party console requires the two commands; 'connect' and 'run'. A typical input/output is shown below. This connects to the host server then runs a process indexed by three parameters (p1,p2,p3).
>connect server
Successfully connected to service.
>run p1 p2 p3
Successfully started.
FINISHED
The app does not allow me to execute both commands in one line using & as with cmd.
Despite reading the subprocess documentation I can't figure out how to pass my two commands into the executable.
I am using Python 3.5, so I believe subprocess.run should be suitable for this task. The snippet below simply opens the third-party console. I have tried other code, linked at the bottom of the post, but I am unsure how to implement it for my purpose.
import subprocess
exe = r'C:\...\third_party_app.exe'
subprocess.run(exe)
Below are some of the SO resources that could be helpful that I have tried, and failed, to interpret.
https://stackoverflow.com/tags/subprocess/info
Python - How do I pass a string into subprocess.Popen (using the stdin argument)?

How to Use Python to execute initial commands over the terminal and then continue use

I have looked into using pxssh ,subprocess and paramiko but have found no success. What I am ultimately trying to do is figure out a way to not only use SSH to access a server and execute commands using a python script and finish there, but also have it open an instance of the terminal after executing all the commands for continued use.
Currently the server has modules that clients have to manually activate using commands after they have established an SSH connection.
For example:
module python
This command would give the user access to python.
Following this the user would then be able to use python and all its commands through the ssh connection in the terminal.
The issue I have with the methods listed earlier for executing these commands is that it does not display an instance of the terminal. It successfully executes the commands but since these commands have to be executed every time a new SSH connection is established it's worthless unless I can get essentially a copy of the terminal that the Python script executed and loaded up all the modules with.
Does any one have a solution to this? I've scoured the web for hours to no success.
This is a very difficult issue to explain so if anything is unclear please ask me and I will try my best to rephrase things. I am very new to all this.

How to interact with the running process in python?

For example, I opened power-shell manually in windows.Now I know process id of it. So I want to run commands on the process using python.
Open power-shell manually.
Find its process id.
Execute commands using its process id.
Please mention code to do it **
You can do all that with the subprocess python standard library https://docs.python.org/2/library/subprocess.html

Run and managment python script

I have web application on Flask and user can send some request which my script is processing and then, running another script in python in the console with some parameters like this:
import sys
os.system('python start.py -u 100 -p 122224')
All works good, but now I want controlling all running copies of my script like start, stop and pause.
How i can do this without crutches?
Check subprocess and multiprocessing modules. The first one allows you to execute external application. To use the second one, you'll be required to call some python code, but the management capabilities should be much wider.

Categories