#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0

    Killing subprocess?


    Hello there fellow reader,

    I'm Yegers and new to this forum, but I like it already ^^'

    I'm learning python and I created this script, to see if it is possible to show the "hidden" password in linux.

    I have this little issue, with the subprocess to be exact.
    I found no other way then brutally kill my whole program to stop the subprocess, because this subprocess is reallly stuborn...

    This is the code:
    ( I used "killpg" to kill the whole program and it's subprocess)

    Code:
    # password.py
    # created by Yegers
    # shows your "hidden" password in linux
    
    from subprocess import Popen, PIPE, STDOUT
    from os import killpg
    from signal import SIGKILL
    
    
    def MAIN():
        PASSWORD = input() 
        print(PASSWORD)
        killpg(0, SIGKILL) # kill everything (haven't found a better way to kill the subprocess...)
    
    BASH = 'sudo -s' 
    EVENT = Popen(BASH, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) 
    OUTPUT = EVENT.communicate(MAIN())

    I'm searching for a more "elegant" way to kill the subprocess.

    Many thanks already for your help,
    Yegers

    P.S. I'm using python 3.2
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    The object returned by Popen has a pid attribute. Useful?

    I'm stronger on the user aspects of unix, gawk, flex, bison, tar, tr, du, emacs, make, bash, etceteras than with administrative notions.

    To kill the super user process do you also need root privilege?

    What is your program supposed to do?
    main returns None whereas I expected communicate to need a bytes object.

    Do you have an example of the program that doesn't work? If I remove your killpg command ^C terminates the thing. Sorry, I don't understand your expectation, the purpose or the symptoms.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    Fistly, thanks for your quick reply!

    I know the the object has a pid atribut, but how to find out what it is, and how to stop/kill it without having to kill the whole program/script?

    So my program first prints:
    Code:
    [sudo]  password for yourusername:
    "

    Then it awaits an input (then my program goes to the "MAIN()" part.)
    Let's pretend my password is "12345678":
    So I give in: "12345678" (which is hidden) and I press enter.

    What you normally should see is:
    Code:
    [sudo]  password for yourusername: 12345678
    Killed

    So you can actually SEE your password,

    But you don't get administrator rights because you give your password in at the
    Code:
    PASSWORD = input()
    part, and not a trough the bash command "sudo -s" (in subprocess)
    Which is perfectly fine but,

    now I don't need this subprocess anymore but it keeps going on, even if I kill the program with ^C, so I use "killpg" but that just kills everything, what should I do if I'd want to go on with the main program and stop the subprocess, just the subprocess.

    My program is for studying subprocesses in python, which I find a pain to work with..

    Does communicate really need a bytes object?

    (PS: may I know what linux distribution you are using, just to know it it works on different versions)
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    I use current ubuntu linux distribution. Yes, communicate uses bytes objects. Running the code in an xterm causes plenty of trouble, depending on how I choose to enter my password or interrupt I sometimes need to command `stty sane'. Of course when I get the program to work it's only because I didn't wait 15 minutes for my automatic sudo password expiration. Maybe a linux forum would help?

    Code:
    #open.communicate(input=None, timeout=None)
    #
    #... The type  of input must be bytes  or, if universal_newlines was
    #True, a string.
    
    from subprocess import Popen, PIPE, STDOUT
    
    def MAIN():
        PASSWORD = input()
        print(PASSWORD)
        return PASSWORD.encode()
    
    BASH = 'sudo -s ls /root'  # privilege required
    EVENT = Popen(BASH, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
    print(EVENT.pid)
    (OUT,ERR) = EVENT.communicate(MAIN())
    print(OUT.decode('ASCII'))
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    Well, I'll let it be then

    I tried many things to just kill the pid attribute, but, well it doesn't work

    Is there any way to hide the output "Killed" at the end?

    Many thanks and appreciations for looking and helping ^^'

    Yegers

    (mint user)
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    I presume one of the processes writes "killed" to stderr. I tried a couple redirections to /dev/null and haven't solved this. Sorry.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    No matter, your comments were extremely helpful ^^'
    I'll play a bit further with the subprocess

IMN logo majestic logo threadwatch logo seochat tools logo