jborean93 / pypsrp

PowerShell Remoting Protocol for Python
MIT License
324 stars 49 forks source link

No connection could be made because the target machine actively refused it #118

Closed bhumitra closed 3 years ago

bhumitra commented 3 years ago

wsman = WSMan("localhost", ssl=False, auth="basic", encryption="never",username="bhumitra", password="*****") with wsman, WinRS(wsman) as shell: ... process = Process(shell, "dir") ... process.invoke() ... process.signal(SignalCode.CTRL_C) ... Traceback (most recent call last): File "C:\python3.9\lib\site-packages\urllib3\connection.py", line 169, in _new_conn conn = connection.create_connection( File "C:\python3.9\lib\site-packages\urllib3\util\connection.py", line 96, in create_connection raise err File "C:\python3.9\lib\site-packages\urllib3\util\connection.py", line 86, in create_connection sock.connect(sa) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\python3.9\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen httplib_response = self._make_request( File "C:\python3.9\lib\site-packages\urllib3\connectionpool.py", line 394, in _make_request conn.request(method, url, **httplib_request_kw) File "C:\python3.9\lib\site-packages\urllib3\connection.py", line 234, in request super(HTTPConnection, self).request(method, url, body=body, headers=headers) File "C:\python3.9\lib\http\client.py", line 1255, in request self._send_request(method, url, body, headers, encode_chunked) File "C:\python3.9\lib\http\client.py", line 1301, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "C:\python3.9\lib\http\client.py", line 1250, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "C:\python3.9\lib\http\client.py", line 1010, in _send_output self.send(msg) File "C:\python3.9\lib\http\client.py", line 950, in send self.connect() File "C:\python3.9\lib\site-packages\urllib3\connection.py", line 200, in connect conn = self._new_conn() File "C:\python3.9\lib\site-packages\urllib3\connection.py", line 181, in _new_conn raise NewConnectionError( urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x0000022B1DB447C0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\python3.9\lib\site-packages\requests\adapters.py", line 439, in send resp = conn.urlopen( File "C:\python3.9\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen retries = retries.increment( File "C:\python3.9\lib\site-packages\urllib3\util\retry.py", line 573, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000022B1DB447C0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "C:\python3.9\lib\site-packages\pypsrp\shell.py", line 98, in enter self.open() File "C:\python3.9\lib\site-packages\pypsrp\shell.py", line 203, in open response = self.wsman.create(self.resource_uri, shell, File "C:\python3.9\lib\site-packages\pypsrp\wsman.py", line 269, in create res = self.invoke(WSManAction.CREATE, resource_uri, resource, File "C:\python3.9\lib\site-packages\pypsrp\wsman.py", line 389, in invoke response = self.transport.send(xml) File "C:\python3.9\lib\site-packages\pypsrp\wsman.py", line 761, in send return self._send_request(prep_request) File "C:\python3.9\lib\site-packages\pypsrp\wsman.py", line 764, in _send_request response = self.session.send(request, timeout=( File "C:\python3.9\lib\site-packages\requests\sessions.py", line 655, in send r = adapter.send(request, **kwargs) File "C:\python3.9\lib\site-packages\requests\adapters.py", line 516, in send raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000022B1DB447C0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

How do I get past this?

jonebeabout commented 3 years ago

WinRM is likely not configured on the server. Try running winrm qc from an Administrator command prompt, then try again.

jborean93 commented 3 years ago

Closing as per the above. The requests library is unable to connect to the host which could be due to a myriad of reasons. Make sure you've enabled WinRM and opened the firewall.