1

I want to debug root-run python scripts via PyDev + eclipse. I also would like to avoid pip entirely. Via python subprocess I would like to send some commands to postgres, but that actually would require running python script as root (then it works).

example: consider this working script, which tries to interact with postgres + avoiding pip packages (as non-root it hangs indefinately):

ok that was in 2022 https://arstechnica.com/information-technology/2022/08/10-malicious-python-packages-exposed-in-latest-repository-attack/ and the those devs are probably trustworthy https://pypi.org/user/jerickso/ https://pypi.org/user/piro/ but still X-D

Is it so hard?

hostnamectl |grep Op; # tested on, Debian is more minimalistic faster!!! :D

Operating System: Ubuntu 22.04.4 LTS

vim /home/user/git/test/test_db.py
import subprocess

print("=== python: how to interact with postgres without 3rd party pip packages v1.0 ===")

# Define the command to be executed
command = 'sudo -u postgres psql -c "SELECT version();"'

# Execute the command using subprocess
try:
    result = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
    print("Output:")
    print(result.stdout)
except subprocess.CalledProcessError as e:
    print(f"Error: {e}")
    print(f"Command output (stderr): {e.stderr}")

How to elegantly go about it?

PS: also already meddled with sudo

currently the only working solution :D

start eclipse as root without sudo

vim /script/start_eclipse_as_root.sh

#!/bin/bash

export DISPLAY=':0.0'

cp -v /home/user/.Xauthority /root/.Xauthority

/home/user/eclipse/eclipse/eclipse

Also: it would probably be better to use pure (root run) bash for the task of viewing hardware info that only root can access (logical volumes)

Wondering if there is a more elegant way.

PS: please keep stackoverflow.com as simple as possible.

1
  • Why not authenticate at Postgres? Then you can send commands directly, without sudo or psql. Of course you'll need a driver; psycopg2 is the best but there're also PyGreSQL, py-postgresql and pg8000. Commented Jun 27, 2024 at 18:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.