def test_tunnel_channel_failure(self): remote_host = '127.0.0.8' remote_server = OpenSSHServer(listen_ip=remote_host, port=self.port) remote_server.start_server() in_q, out_q = deque(), deque() try: tunnel = Tunnel(self.proxy_host, in_q, out_q, port=self.port, pkey=self.user_key, num_retries=1) tunnel.daemon = True tunnel.start() in_q.append((remote_host, self.port)) while not tunnel.tunnel_open.is_set(): sleep(.1) if not tunnel.is_alive(): raise ProxyError self.assertTrue(tunnel.tunnel_open.is_set()) self.assertIsNotNone(tunnel.client) while True: try: _port = out_q.pop() except IndexError: sleep(.5) else: break proxy_client = SSHClient( '127.0.0.1', pkey=self.user_key, port=_port, num_retries=1, _auth_thread_pool=False) tunnel.cleanup() spawn(proxy_client.execute, 'echo me') proxy_client.disconnect() self.assertTrue(proxy_client.sock.closed) finally: remote_server.stop()
def test_identity_auth(self): class _SSHClient(SSHClient): IDENTITIES = (self.user_key, ) client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1, allow_agent=False) client.disconnect() client.pkey = None del client.session del client.sock client._connect(self.host, self.port) client._init_session() client.IDENTITIES = (self.user_key, ) # Default identities auth only should succeed client._identity_auth() client.disconnect() client._connect(self.host, self.port) client._init_session() # Auth should succeed self.assertIsNone(client.auth()) # Standard init with custom identities client = _SSHClient(self.host, port=self.port, num_retries=1, allow_agent=False) self.assertIsInstance(client, SSHClient)
def scope_killer(): for _ in range(5): client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1, allow_agent=False) host_out = client.run_command(self.cmd) output = list(host_out.stdout) self.assertListEqual(output, [self.resp]) client.disconnect()
def test_multiple_clients_exec_terminates_channels(self): # See #200 - Multiple clients should not interfere with # each other. session.disconnect can leave state in libssh2 # and break subsequent sessions even on different socket and # session for _ in range(5): client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1, allow_agent=False) channel = client.execute(self.cmd) output = list(client.read_output(channel)) self.assertListEqual(output, [b'me']) client.disconnect()
def test_disconnect_exc(self): class DiscError(Exception): pass def _disc(): raise DiscError client = SSHClient( self.host, port=self.port, pkey=self.user_key, retry_delay=.1, num_retries=1, timeout=1, ) client._disconnect_eagain = _disc client._connect_init_session_retry(1) client.disconnect()
def test_forwarder_exit(self): def _start_server(): raise Exception forwarder = LocalForwarder() forwarder.daemon = True forwarder.start() forwarder.started.wait() client = SSHClient(self.proxy_host, port=self.proxy_port, pkey=self.user_key) forwarder.enqueue(client, self.proxy_host, self.port) forwarder.out_q.get() self.assertTrue(len(forwarder._servers) > 0) client.sock.close() client.disconnect() forwarder._cleanup_servers() self.assertEqual(len(forwarder._servers), 0) forwarder._start_server = _start_server forwarder.enqueue(client, self.proxy_host, self.port) sleep(.1)
def test_tunnel_channel_failure(self): remote_host = '127.0.0.8' remote_server = OpenSSHServer(listen_ip=remote_host, port=self.port) remote_server.start_server() in_q, out_q = deque(), deque() try: tunnel = Tunnel(self.proxy_host, in_q, out_q, port=self.port, pkey=self.user_key, num_retries=1) tunnel.daemon = True tunnel.start() in_q.append((remote_host, self.port)) while not tunnel.tunnel_open.is_set(): sleep(.1) if not tunnel.is_alive(): raise ProxyError self.assertTrue(tunnel.tunnel_open.is_set()) self.assertIsNotNone(tunnel.client) while True: try: _port = out_q.pop() except IndexError: sleep(.5) else: break proxy_client = SSHClient('127.0.0.1', pkey=self.user_key, port=_port, num_retries=1, _auth_thread_pool=False) sleep(1) cmd = spawn(proxy_client.execute, 'echo me') proxy_client.disconnect() joinall([cmd]) self.assertEqual(proxy_client.sock, None) finally: remote_server.stop()