Python Forum
[inconsistent output] subprocess.call to run cmd commands to get Kerberos ticket
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[inconsistent output] subprocess.call to run cmd commands to get Kerberos ticket
#1
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
Reply
#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')
Reply
#3
(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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  why is yfinance returning an ellipsis in the middle of ticket history db042190 3 977 Jun-12-2023, 06:03 PM
Last Post: db042190
  Read csv file with inconsistent delimiter gracenz 2 1,201 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  merge two csv files into output.csv using Subprocess mg24 9 1,788 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  Inconsistent loop iteration behavior JonWayn 2 1,003 Dec-10-2022, 06:49 AM
Last Post: JonWayn
  ValueError: Found input variables with inconsistent numbers of samples saoko 0 2,476 Jun-16-2022, 06:59 PM
Last Post: saoko
  Loop Dict with inconsistent Keys Personne 1 1,610 Feb-05-2022, 03:19 AM
Last Post: Larz60+
  Inconsistent counting / timing with threading rantwhy 1 1,770 Nov-24-2021, 04:04 AM
Last Post: deanhystad
  Inconsistent behaviour in output - web scraping Steve 6 2,553 Sep-20-2021, 01:54 AM
Last Post: Larz60+
  Found input variables with inconsistent numbers of samples: [1000, 200] jenya56 2 2,899 Sep-15-2021, 12:48 PM
Last Post: jenya56
  Packages inconsistent warning during hdbscan install Led_Zeppelin 0 1,930 Aug-31-2021, 04:10 PM
Last Post: Led_Zeppelin

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020