예제 #1
0
def get_connection_helper(reactor, address, username, port):
    """
    Get a :class:`twisted.conch.endpoints._ISSHConnectionCreator` to connect to
    the given remote.

    :param reactor: Reactor to connect with.
    :param bytes address: The address of the remote host to connect to.
    :param bytes username: The user to connect as.
    :param int port: The port of the ssh server to connect to.

    :return _ISSHConnectionCreator:
    """
    try:
        agentEndpoint = UNIXClientEndpoint(
            reactor, os.environ["SSH_AUTH_SOCK"])
    except KeyError:
        agentEndpoint = None

    return _NewConnectionHelper(
        reactor, address, port, None, username,
        keys=None,
        password=None,
        agentEndpoint=agentEndpoint,
        knownHosts=KnownHostsFile.fromPath(FilePath("/dev/null")),
        ui=ConsoleUI(lambda: _ReadFile(b"yes")))
예제 #2
0
def get_connection_helper(reactor, address, username, port):
    """
    Get a :class:`twisted.conch.endpoints._ISSHConnectionCreator` to connect to
    the given remote.

    :param reactor: Reactor to connect with.
    :param bytes address: The address of the remote host to connect to.
    :param bytes username: The user to connect as.
    :param int port: The port of the ssh server to connect to.

    :return _ISSHConnectionCreator:
    """
    try:
        agentEndpoint = UNIXClientEndpoint(reactor,
                                           os.environ["SSH_AUTH_SOCK"])
    except KeyError:
        agentEndpoint = None

    return _NewConnectionHelper(reactor,
                                address,
                                port,
                                None,
                                username,
                                keys=None,
                                password=None,
                                agentEndpoint=agentEndpoint,
                                knownHosts=KnownHostsFile.fromPath(
                                    FilePath("/dev/null")),
                                ui=ConsoleUI(lambda: _ReadFile(b"yes")))
예제 #3
0
 def test_cleanupConnectionNotImmediately(self):
     """
     L{_NewConnectionHelper.cleanupConnection} closes the transport cleanly
     if called with C{immediate} set to C{False}.
     """
     helper = _NewConnectionHelper(
         None, None, None, None, None, None, None, None, None, None)
     connection = SSHConnection()
     connection.transport = StringTransport()
     helper.cleanupConnection(connection, False)
     self.assertTrue(connection.transport.disconnecting)
예제 #4
0
 def test_cleanupConnectionNotImmediately(self):
     """
     L{_NewConnectionHelper.cleanupConnection} closes the transport cleanly
     if called with C{immediate} set to C{False}.
     """
     helper = _NewConnectionHelper(None, None, None, None, None, None, None,
                                   None, None, None)
     connection = SSHConnection()
     connection.transport = StringTransport()
     helper.cleanupConnection(connection, False)
     self.assertTrue(connection.transport.disconnecting)
예제 #5
0
    def test_defaultKnownHosts(self):
        """
        L{_NewConnectionHelper._knownHosts} is used to create a
        L{KnownHostsFile} if one is not passed to the initializer.
        """
        result = object()
        self.patch(_NewConnectionHelper, '_knownHosts', lambda cls: result)

        helper = _NewConnectionHelper(
            None, None, None, None, None, None, None, None, None, None)

        self.assertIdentical(result, helper.knownHosts)
예제 #6
0
    def test_defaultKnownHosts(self):
        """
        L{_NewConnectionHelper._knownHosts} is used to create a
        L{KnownHostsFile} if one is not passed to the initializer.
        """
        result = object()
        self.patch(_NewConnectionHelper, '_knownHosts', lambda cls: result)

        helper = _NewConnectionHelper(None, None, None, None, None, None, None,
                                      None, None, None)

        self.assertIdentical(result, helper.knownHosts)
예제 #7
0
    def test_cleanupConnectionImmediately(self):
        """
        L{_NewConnectionHelper.cleanupConnection} closes the transport with
        C{abortConnection} if called with C{immediate} set to C{True}.
        """
        class Abortable:
            aborted = False
            def abortConnection(self):
                """
                Abort the connection.
                """
                self.aborted = True

        helper = _NewConnectionHelper(
            None, None, None, None, None, None, None, None, None, None)
        connection = SSHConnection()
        connection.transport = SSHClientTransport()
        connection.transport.transport = Abortable()
        helper.cleanupConnection(connection, True)
        self.assertTrue(connection.transport.transport.aborted)
예제 #8
0
    def test_cleanupConnectionImmediately(self):
        """
        L{_NewConnectionHelper.cleanupConnection} closes the transport with
        C{abortConnection} if called with C{immediate} set to C{True}.
        """
        class Abortable:
            aborted = False

            def abortConnection(self):
                """
                Abort the connection.
                """
                self.aborted = True

        helper = _NewConnectionHelper(None, None, None, None, None, None, None,
                                      None, None, None)
        connection = SSHConnection()
        connection.transport = SSHClientTransport()
        connection.transport.transport = Abortable()
        helper.cleanupConnection(connection, True)
        self.assertTrue(connection.transport.transport.aborted)