Python Forum

Full Version: [inconsistent output] subprocess.call to run cmd commands to get Kerberos ticket
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
As part of my daily work, I need to run a couple of lines of commands in cmd prompt in order to get Kerberos ticket to a database. Here are the commands:

cd C:\ProgramData\MIT\Kerberos5\
"C:\Program Files\MIT\Kerberos\bin\kinit" -kt ossuser.keytab ossuser

I am now asked to make it more nicely, i.e., by making it as a button on a GUI (which contains many other function) developed using Tkinter. To do this, I try to use subprocess.call. Here are the commands:

Command_piece_1:
import subprocess
command_01='cd C:\ProgramData\MIT\Kerberos5'
command_02='"'+'C:\Program Files\MIT\Kerberos'+'\\'+'bin\kinit'+'"'+' -kt ossuser.keytab ossuser'
conn=subprocess.call(command_01+"&"+command_02, shell=True)


And here is my definition for the button before root = Tk():

Command_piece_2
import subprocess

def connect_impala():
    command_01='cd C:\ProgramData\MIT\Kerberos5'
    command_02='"'+'C:\Program Files\MIT\Kerberos'+'\\'+'bin\kinit'+'"'+' -kt ossuser.keytab ossuser'
    conn=subprocess.call(command_01+"&"+command_02, shell=True)
    if conn==0:
       messagebox.showinfo("INFO","Getting ticket successful.\nYou can close this INFO window.")
    else:
       messagebox.showerror("ERROR","Getting ticket failed!\nPlease check if your VPN is working properly and try again.")
Now strange thing happens. When I click the corresponding button on the GUI, it fails forever (showing the ERROR window I defined). However, when I try Command_piece_1 in IDLE, it succeeds.

Anyone knows how this can happen? Do I miss out something in the function definition?

Python version: 3.6.2
Tk version: 8.6.6
IDLE version: 3.6.2
cd wont work in a subprocess call can take a cwd argument to change directory.
Should not be using shell=True.
Can try.
import subprocess

subprocess.run(['C:\\Program Files\\MIT\\Kerberos\\bin\\kinit', '-kt', 'ossuser.keytab', 'ossuser'], cwd=r'C:\ProgramData\MIT\Kerberos5')
(Jun-07-2018, 03:57 PM)snippsat Wrote: [ -> ]cd wont work in a subprocess call can take a cwd argument to change directory.
Should not be using shell=True.
Can try.
import subprocess

subprocess.run(['C:\\Program Files\\MIT\\Kerberos\\bin\\kinit', '-kt', 'ossuser.keytab', 'ossuser'], cwd=r'C:\ProgramData\MIT\Kerberos5')


This is cool. Thanks a lot. Just need to make a small modification for my case:
import subprocess
command_01='"'+'C:\Program Files\MIT\Kerberos'+'\\'+'bin\kinit'+'"'+' -kt ossuser.keytab ossuser'
subprocess.run(command_01, cwd=r'C:\ProgramData\MIT\Kerberos5')
However, I am still confused on one thing: why my original solution works fine in IDLE?