mhas01
asked on
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 = 'http://xxxxxxxxx.xx.xxxxx.com:5985/wsman'
win_connect = Protocol(endpoint=winrm_ur l,transpor t='kerbero s')
shell_id = win_connect.open_shell()
ps_script = """dir"""
command_id = win_connect.run_command(sh ell_id, ps_script)
output,error_value,exit_st atus = win_connect.get_command_ou tput(shell _id, command_id)
output
error_value
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.
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 = 'http://xxxxxxxxx.xx.xxxxx.com:5985/wsman'
win_connect = Protocol(endpoint=winrm_ur
shell_id = win_connect.open_shell()
ps_script = """dir"""
command_id = win_connect.run_command(sh
output,error_value,exit_st
output
error_value
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
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.