which user's privileges python program uses to execute command via subprocess? - python

I have stabled password less login between two ubuntu system then I run following command
$scp file user#hostname2:/tmp/
it works fine and I got file transferred without asking for password.
however when I try to execute the same through following python statement-
subprocess.check_output(['scp', file, r'user#hostname2:/tmp/'])
I am not able to copy files to remote computer, so I am bit curious about which user's privileges python program uses to execute command via subprocess ?
How can I debug such programs ?

Related

Can't run Python Script on remote VM via SSH

I have an Azure VM into which I SSH in. The VM is a W10 host.
I can create files with touch, change directories and so on, but whenever I try to run a python script that is hosted on the VM I get the following error:
The system cannot execute the specified program.
At first glance I thought that there was a problem related to my pyhton alias and the PATH variable, so I decided to use RDP to log into the machine, open a CMD and try the same command, which worked just fine. The python program executed flawlessly.
I used where to find where is my python.exe located at, so whenever I run the script on my remote terminal I can do something like:
C:\Users\User01\AppData\Local\Microsoft\WindowsApps\python.exe test.py
This does result in the same error message as the one stated above.
Can I get some help?
Hi try to execute your command as below.
ssh user#machine python < script.py

run python script on local machine stored on remote server

I would like to execute a python script stored on a remote server on a local machine. This is so I can keep the code for the script on the server without the user having a copy. Is this possible using python?
I am basically trying to secure the code, possibly behind a username and/or password so that way I can easily update the codebase. (Much like using ssh - but the python script is executed on the local machine instead of the server.)
Edit:
Using curl and process substitution this may be achievable:
execute bash script from URL
so that to execute the python script the command is:
python <(curl "http://example.com/test.py" -s -N)
curl also supports password protection which is ideal.
When I execute the script the root path for the script is /dev/fd. When I navigate to this directory and list directory only contains numbers.
Using the above command as an example, is the script downloaded? (and where to). I notice that if I execute the script in a directory such as Desktop it is not downloaded to that location (the working directory).
You can compile the Python code into compiled Python .pyc files and distribute these files, though the local machine should have the same Python environment as the server.
However it is also not impossible for this code to be decompiled.
https://python-compiler.com/post/how-to-distribute-python-program

Python subprocess: Windows command line safety

The idea is to execute code on Windows via Python's subprocess.Popen, but, in the command I'm executing, there's also a password which I need to keep secure.
Does Windows store (somewhere, e.g. logs) commands executed this way via Python?
Normally, I would execute such a command via standard Windows command line like net use, but now I moved it to my Python script, and because it contains a password, I want to keep it as secure as possible.

Python Run as different user

On windows server I need via python code to run an executable/process (such as a notepad \ ps1 script) with other domain user privilages. The credntials are hardcoded in the python script. The script should run remotelly on the server so the password promt is problematic. Is there any way I can open a proccess with known cerentials (user + password) without code interuption?

Python application permission denied

My Python program uses the terminal(system) command to perform the task on the file and script. I am going to convert this Python program into Mac OS application and Linux application using pyinstaller. I am going to pass the application installer file to my friends. However, I have following questions.
If script or file doesn't have proper permission which my program is trying to access, will Python get an error?
Running some script or opening file will require the permission of root. So is there a possible option that will prompt the user for root(admin) password or run my application with root privilege?
Thanks
Try chmod 777 filname.py, this will give the file all rights for execution and editing. There are also other modes for chmod like 755, which will also work for your case.

Categories