How to get output from a shell script called with kdesu

I wrote a Python GUI as a GUI wrapper for a bash shell script. It should work with gnome and with kde on openSuSE 12.1 and should optionally call the shell script as root. I works fine on gnome with gksudo but I have problems with kdesu. I have to get the output to the console in order to display it in a window but fail to the stdout with kdesu. I don’t know whether it’s a kde or python issue. That’s why *(http://www.python-forum.org/pythonforum/viewtopic.php?f=3&t=36559).

The following code is

#!/usr/bin/env python 
#-*- coding: utf-8 -*-

import subprocess
from subprocess import call
from subprocess import Popen

def execute():
    
    commandToExecute= 'kdesu', '-c','ls -la /']
#    commandToExecute= 'gksudo', 'ls -la /']
                                    
    process = subprocess.Popen(commandToExecute, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                
    (stdoutData,stderrData) = process.communicate()
    
    print "Stdout: %s" % (stdoutData)
    print "Stderr: %s" % (stderrData)
    
    exitCode = process.returncode

    print "exitCode: " + str(exitCode)
            
if __name__ == "__main__":
    execute()

Would be glad if somebody can give me a hint why it does’nt work with kde.*

On 2012-09-13 20:26, framp wrote:
> I wrote a Python GUI as a GUI wrapper for a bash shell script.

Maybe you should ask in the programming forum instead.


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)

Not sure it’s a programming issue. Which programming forum do you think of?

We have Programming/scritping under Development under Other Forums On the main page (English part).

Thank you very much for your reply. I’m sorry. I missed this forum and it seems to be a much better forum for my issue. Can you move this thread for me into the other forum or do I have to create a new one in the development forum?

This thread will be moved. Please do not post until moved.

Moved and open for posting.

So, normally, you use sudo to execute a terminal command as** root** and you use kdesu or gnomesu to run a graphic program as root from a bash script. For the ls command as root, try sudo instead, but I am not sure I know what you are trying to do. I have a bash script that can substitute the password, when using sudo you should look at:

asroot version 1.14 allows you to run a program (with options) as a root user - Blogs - openSUSE Forums

I would pick the script apart to see how it is done. I have a bash function that can determine the desktop used and create different sudoish and editor commands based on the desktop found. You should also pick this function apart. This bit of code was created with the generous help from please_try_again.

#
# Determine which desktop that we are using ***********************************
#

function find_desktop {

  let de=0
  ps nc -C plasma-desktop >/dev/null && de=1     # For kde
  ps nc -C xfce4-panel >/dev/null && de=2     # For xfce
  ps nc -C gnome-panel >/dev/null && de=3     # For gnome2
  ps nc -C gnome-shell >/dev/null && de=4     # For gnome3
  ps nc -C lxsession >/dev/null && de=5         # lxde
  

#
# Insert the Name Of your Favorite KDE or GNOME File Editor *******************
#
  case "$de" in

    "0") EDITOR="vi"                    ;  SU_CMD="sudo"        ; SHUTDOWN=""                                           ;; # No Desktop Loaded
    "1") EDITOR="kwrite"         ;  SU_CMD="kdesu"     ; SHUTDOWN="kbuildsycoca4 && kquitapp plasma-desktop"     ;; # For KDE
    "2") EDITOR="mousepad"         ;  SU_CMD="xdg-su -c"     ; SHUTDOWN=""                         ;; # For xfce
    "3") EDITOR="gedit"         ;  SU_CMD="gnomesu"     ; SHUTDOWN="killall gnome-panel"            ;; # For gnome2
    "4") EDITOR="gedit"         ;  SU_CMD="gnomesu"     ; SHUTDOWN="killall gnome-panel"            ;; # For gnome3
    "5") EDITOR="leafpad"         ;  SU_CMD="xdg-su -c"     ; SHUTDOWN=""                        ;; # For lxde 
     *)  exit 1 ;;

  esac
  return $de
}

This is good stuff, so you really need to look through when writing bash scripts.

Thank You,*

@hcvv: Thx for moving my thread.

@jdmcdaniel3: Nice code snippets! That’s what I initially though I should start with: Create a GUI window which prompts the user for the root PWD and then invoke the script with

echo "pwd" | sudo -S myScriptInvokedAsRoot

But then I detected that existing GUI programs use kdesu and gksudo to prompt the user for root PWD. For example applications which install packages go this way and I thought that’s the right way to go.

You suggested to use sudo instead. Actually I didn’t finish writing my code with the PWD prompt window and to call sudo because I thought that’s the wrong way. But I think you are right. I’ll try to use sudo instead. Will keep you posted.

Sure, give it a try. You can’t figure out what’s right until you have done a few things wrong it would seem, so go for it and ask for more help if you need it.

Thank You,

I just ran my GUI and script successfully with Gnome and KDE. Works like a charm. No struggling with stdout and stderr in kde any more.

Actually my initial issue is not solved - I still don’t understand why I can get stdout and stderr with gksudo but not with kdesu. But anyhow I found a much better way to solve my original problem to call a bash script as root from a gtk GUI.

Thx James to point me in the right direction.

I love to hear about success here and a great way to start off your weekend.

Thank You,