Remote Windows command execution over WinRM using python and Kerberos authentication

Hello experts
I am successful at running a command in a remote Windows server. I achieve this by installing WinRM in the remote Windows server, and also executing the following python script inside a Linux container which has Python installed:

from winrm.protocol import Protocol
winrm_url = ''
win_connect = Protocol(endpoint=winrm_url,transport='kerberos')
shell_id = win_connect.open_shell()
ps_script = """dir"""
command_id = win_connect.run_command(shell_id, ps_script)
output,error_value,exit_status = win_connect.get_command_output(shell_id, command_id)

By the way, and of course, I have already initiated a Kerberos ticket for the computer which is creating the remote session, otherwise this wouldn't work. The issue that I'm facing is: if I run my script the first time I get the following error:

kerberos.GSSError: (('Unspecified GSS failure.  Minor code may provide more information', 851968), ('Server not found in Kerberos database', -1765328377))

If I run it again it just works: it displays the contents of the remote directory.

Here are my results - captured in the second run attempt.

>>> output
' Volume in drive C is SYSTEM\r\n Volume Serial Number is XXXX-XXXX\r\n\r\n Directory of C:\\Users\\xxxx_xxxx\r\n\r\n06/1                                                                                0/2015  05:56 PM    <DIR>          .\r\n06/10/2015  05:56 PM    <DIR>          ..\r\n08/07/2014  11:28 AM    <DIR>                                                                                          Desktop\r\n06/10/2015  05:56 PM    <DIR>          Documents\r\n08/22/2013  09:39 AM    <DIR>          Downloads\r\n08/22                                                                                /2013  09:39 AM    <DIR>          Favorites\r\n08/22/2013  09:39 AM    <DIR>          Links\r\n08/22/2013  09:39 AM    <DI                                                                                R>          Music\r\n08/22/2013  09:39 AM    <DIR>          Pictures\r\n08/22/2013  09:39 AM    <DIR>          Saved Games                                                                                \r\n08/22/2013  09:39 AM    <DIR>          Videos\r\n               0 File(s)              0 bytes\r\n              11 Dir                                                                                (s)  562,262,183,936 bytes free\r\n'

If I went to run this again, this would again fail (third attempt) but if I do a fourth try, then it works again.

I'd like it to just work every single time I run it. (Best case scenario) Another option would be to understand where the problem is originating and if possible fix this root cause. I could go with a try/catch approach but only if there was no way to fix this error.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mhas01Author Commented:
No need to worry anymore about this question. I have already found a solution which was tested to work correctly.

I used an IP address instead of a remote computer name, and everything works like a charm now.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.