pypsexec: When connecting to Windows Server 2008 R2, connection never closed eventhough the job is done successfully

I tried to create simple psexec script :

from pypsexec.client import Client

server = "windows2008.intra.com"
username = "Administrator"
password = "generic-password"
executable = "powershell.exe"
arguments = 'ls > C:\windows\temp\dir.txt'

def raw_string(s):
    if isinstance(s, str):
        s = s.encode('string-escape')
    elif isinstance(s, unicode):
        s = s.encode('unicode-escape')
    return s

c = Client(server, username=username, password=password,
           encrypt=False)

c.connect()
try:
    c.create_service()
    result = c.run_executable(executable, arguments=raw_string(arguments))
finally:
    c.remove_service()
    c.disconnect()

print("STDOUT:\n%s" % result[0].decode('utf-8') if result[0] else "")
print("STDERR:\n%s" % result[1].decode('utf-8') if result[1] else "")
print("RC: %d" % result[2])`

The code is always working on windows 2012 or later but when connecting to Windows Server 2008 R2, connection never closed eventhough the job is done successfully ( file dir.txt created).

It will always stuck in this line if i run the program with verbose :

...
# /usr/lib/python2.7/site-packages/pypsexec/paexec.pyc matches /usr/lib/python2.7/site-packages/pypsexec/paexec.py
import pypsexec.paexec # precompiled from /usr/lib/python2.7/site-packages/pypsexec/paexec.pyc
# /usr/lib/python2.7/site-packages/pypsexec/pipe.pyc matches /usr/lib/python2.7/site-packages/pypsexec/pipe.py
import pypsexec.pipe # precompiled from /usr/lib/python2.7/site-packages/pypsexec/pipe.pyc
# /usr/lib/python2.7/site-packages/pypsexec/scmr.pyc matches /usr/lib/python2.7/site-packages/pypsexec/scmr.py
import pypsexec.scmr # precompiled from /usr/lib/python2.7/site-packages/pypsexec/scmr.pyc
# /usr/lib/python2.7/site-packages/pypsexec/rpc.pyc matches /usr/lib/python2.7/site-packages/pypsexec/rpc.py
import pypsexec.rpc # precompiled from /usr/lib/python2.7/site-packages/pypsexec/rpc.pyc
# /usr/lib64/python2.7/Queue.pyc matches /usr/lib64/python2.7/Queue.py
import Queue # precompiled from /usr/lib64/python2.7/Queue.pyc
# /usr/lib64/python2.7/encodings/utf_16_le.pyc matches /usr/lib64/python2.7/encodings/utf_16_le.py
import encodings.utf_16_le # precompiled from /usr/lib64/python2.7/encodings/utf_16_le.pyc
# /usr/lib64/python2.7/encodings/ascii.pyc matches /usr/lib64/python2.7/encodings/ascii.py
import encodings.ascii # precompiled from /usr/lib64/python2.7/encodings/ascii.pyc
# /usr/lib64/python2.7/encodings/latin_1.pyc matches /usr/lib64/python2.7/encodings/latin_1.py
import encodings.latin_1 # precompiled from /usr/lib64/python2.7/encodings/latin_1.pyc
# /usr/lib64/python2.7/encodings/string_escape.pyc matches /usr/lib64/python2.7/encodings/string_escape.py
import encodings.string_escape # precompiled from /usr/lib64/python2.7/encodings/string_escape.pyc 

So currently the only workaround that i can think of is putting “timeout_seconds” so the program will close when the task is done, but i need to guess how much time the program is need to complete the task specified in argument before force closing.

Did i do something wrong ?

Thank you in advance

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 17 (7 by maintainers)

Most upvoted comments

I came accross this issue on a couple of servers with psexec and powershell. The problem is that powershell is waiting after the execution for some input like a single Newline to proceed. You can fix this by starting powershell with the parameter -InputFormat none So powershell will no longer wait for input.

@walter-ebner Only the best people come back to a thread with the solution after solving it, mad props to you magnificent gentleman! I was having this exact issue on my Windows 7 and Windows Server 2008 targets and -InputFormat None did the trick!! Thank you!

@jborean93 I think this deserves a place in the docs right beside encrypt=False being necessary for Win7/2008, as this issue got me well and truly hard-stuck until I finally found this thread, and it relates to those same specific OSes. 😅 In any case, thank you for this great piece of software!