I need some help to understand the windows cmd/ps behavior and how to handle it on python.
If I run "klist" on a command prompt (CMD) or PowerShell (PS) on any folder, I get this response:
C:\>klist
A Identificação de Logon atual é 0:0x249a0c1
Tíquetes em Cache: (0)
If I run klist under the MIT folder (C:\Program Files\MIT\Kerberos\bin), I have this response (the response I am expecting in any scenario)
C:\Program Files\MIT\Kerberos\bin>klist
Ticket cache: FILE:C:\Users\XXX\XXXXX\krb5\KRB5CCNAME
Default principal: XXXXX@XXXX.COM.BR
Valid starting Expires Service principal
08/02/22 22:38:34 08/03/22 19:53:30 krbtgt/XXX.COM.BR@XXX.COM.BR
renew until 08/03/22 22:38:34
If I run klist in PS at MIT folder, I got a totally different response:
PS C:\Program Files\MIT\Kerberos\bin> klist
A Identificação de Logon atual é 0:0x249a112
Tíquetes em Cache: (0)
At first : Why does CMD and PS behave diffently on running the klist executable at MIT folder?
At Second : How do I set my windows "default" klist to MIT klist (instead of the other that I don´t even know about)?
I have a Python scrip that runs a python subprocess run on "klist" to get status of an active(or not) ticket. Due to the issue showed above, as it "run" the klist, they get wrong answers.
# checa se há tickets e se estão expirados
klist = sp.run(["klist", "-s"], timeout=5, encoding="iso8859-1")
if klist.returncode == 1:
return False
# checa se os tickets existentes pertencem ao usuário e ao cluster correto
klist = sp.run(
["klist"], stdout=sp.PIPE, stderr=sp.PIPE, encoding="iso8859-1", timeout=5
)
if klist.returncode == 1:
return False
At Third : How do I set my python scrip above to run the "right" klist (supposing that I couldn't change windows default behavior)?
At first: Why does CMD and PS behave diffently on running the klist executable at MIT folder?
CMD, traditionally for Windows (and MS-DOS), looks for a klist.exe
in the current directory first – therefore it runs C:\Program Files\MIT\Kerberos\bin\klist.exe
when you're in that directory.
PowerShell does not do this – it only looks in $env:PATH
, unless it was explicitly given a full path to run. Similarly to what you'd do in Unix shells such as Bash, only .\klist
would run klist.exe in the current directory, while a bare klist
is only checked against locations in PATH (ignoring the current directoy).
At Second: How do I set my windows "default" klist to MIT klist (instead of the other that I don´t even know about)?
Edit your Windows %PATH%
environment variable to put the MIT Kerberos 'bin' directory at the beginning.
At Third: How do I set my python scrip above to run the "right" klist (supposing that I couldn't change windows default behavior)?
Either specify the full path to the.exe file:
sp.run([r"C:/Program Files/MIT/Kerberos/bin/klist", "-s"], ...)
or update PATH for that process only:
os.environ["PATH"] = r"C:\Program Files\MIT\Kerberos\bin;" + os.environ["PATH"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.