当前位置: 首页>>代码示例>>Python>>正文


Python KnownHostsFile.addHostKey方法代码示例

本文整理汇总了Python中twisted.conch.client.knownhosts.KnownHostsFile.addHostKey方法的典型用法代码示例。如果您正苦于以下问题:Python KnownHostsFile.addHostKey方法的具体用法?Python KnownHostsFile.addHostKey怎么用?Python KnownHostsFile.addHostKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在twisted.conch.client.knownhosts.KnownHostsFile的用法示例。


在下文中一共展示了KnownHostsFile.addHostKey方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_mismatchedHostKey

# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import addHostKey [as 别名]
    def test_mismatchedHostKey(self):
        """
        If the SSH public key presented by the SSH server does not match the
        previously remembered key, as reported by the L{KnownHostsFile}
        instance use to construct the endpoint, for that server, the
        L{Deferred} returned by L{SSHCommandClientEndpoint.connect} fires with
        a L{Failure} wrapping L{HostKeyChanged}.
        """
        differentKey = Key.fromString(privateDSA_openssh).public()
        knownHosts = KnownHostsFile(self.mktemp())
        knownHosts.addHostKey(self.serverAddress.host, differentKey)
        knownHosts.addHostKey(self.hostname, differentKey)

        # The UI may answer true to any questions asked of it; they should
        # make no difference, since a *mismatched* key is not even optionally
        # allowed to complete a connection.
        ui = FixedResponseUI(True)

        endpoint = SSHCommandClientEndpoint.newConnection(
            self.reactor, b"/bin/ls -l", b"dummy user",
            self.hostname, self.port, password=b"dummy password",
            knownHosts=knownHosts, ui=ui)

        factory = Factory()
        factory.protocol = Protocol
        connected = endpoint.connect(factory)

        server, client, pump = self.connectedServerAndClient(
            self.factory, self.reactor.tcpClients[0][2])

        f = self.failureResultOf(connected)
        f.trap(HostKeyChanged)
开发者ID:AlexanderHerlan,项目名称:syncpy,代码行数:34,代码来源:test_endpoints.py

示例2: test_iterentriesUnsaved

# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import addHostKey [as 别名]
 def test_iterentriesUnsaved(self):
     """
     If the save path for a L{KnownHostsFile} does not exist,
     L{KnownHostsFile.iterentries} still returns added but unsaved entries.
     """
     hostsFile = KnownHostsFile(FilePath(self.mktemp()))
     hostsFile.addHostKey("www.example.com", Key.fromString(sampleKey))
     self.assertEqual(1, len(list(hostsFile.iterentries())))
开发者ID:12019,项目名称:OpenWrt_Luci_Lua,代码行数:10,代码来源:test_knownhosts.py

示例3: test_saveResetsClobberState

# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import addHostKey [as 别名]
    def test_saveResetsClobberState(self):
        """
        After L{KnownHostsFile.save} is used once with an instance initialized
        by the default initializer, contents of the save path are respected and
        preserved.
        """
        hostsFile = KnownHostsFile(self.pathWithContent(sampleHashedLine))
        preSave = hostsFile.addHostKey(
            "www.example.com", Key.fromString(otherSampleKey))
        hostsFile.save()
        postSave = hostsFile.addHostKey(
            "another.example.com", Key.fromString(thirdSampleKey))
        hostsFile.save()

        self.assertEqual([preSave, postSave], list(hostsFile.iterentries()))
开发者ID:12019,项目名称:OpenWrt_Luci_Lua,代码行数:17,代码来源:test_knownhosts.py

示例4: setUp

# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import addHostKey [as 别名]
    def setUp(self):
        """
        Configure an SSH server with password authentication enabled for a
        well-known (to the tests) account.
        """
        SSHCommandClientEndpointTestsMixin.setUp(self)

        knownHosts = KnownHostsFile(FilePath(self.mktemp()))
        knownHosts.addHostKey(
            self.hostname, self.factory.publicKeys['ssh-rsa'])
        knownHosts.addHostKey(
            self.serverAddress.host, self.factory.publicKeys['ssh-rsa'])

        self.endpoint = SSHCommandClientEndpoint.newConnection(
            self.reactor, b"/bin/ls -l", self.user, self.hostname, self.port,
            password=self.password, knownHosts=knownHosts,
            ui=FixedResponseUI(False))
开发者ID:AlexanderHerlan,项目名称:syncpy,代码行数:19,代码来源:test_endpoints.py

示例5: setUp

# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import addHostKey [as 别名]
 def setUp(self):
     """
     Patch 'open' in verifyHostKey.
     """
     self.fakeFile = FakeFile()
     self.patch(default, "_open", self.patchedOpen)
     self.hostsOption = self.mktemp()
     knownHostsFile = KnownHostsFile(FilePath(self.hostsOption))
     knownHostsFile.addHostKey("exists.example.com", Key.fromString(sampleKey))
     knownHostsFile.addHostKey("4.3.2.1", Key.fromString(sampleKey))
     knownHostsFile.save()
     self.fakeTransport = FakeObject()
     self.fakeTransport.factory = FakeObject()
     self.options = self.fakeTransport.factory.options = {
         'host': "exists.example.com",
         'known-hosts': self.hostsOption
         }
开发者ID:ChimmyTee,项目名称:oh-mainline,代码行数:19,代码来源:test_knownhosts.py

示例6: test_readExisting

# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import addHostKey [as 别名]
    def test_readExisting(self):
        """
        Existing entries in the I{known_hosts} file are reflected by the
        L{KnownHostsFile} created by L{_NewConnectionHelper} when none is
        supplied to it.
        """
        key = CommandFactory().publicKeys['ssh-rsa']
        path = FilePath(self.mktemp())
        knownHosts = KnownHostsFile(path)
        knownHosts.addHostKey("127.0.0.1", key)
        knownHosts.save()

        msg("Created known_hosts file at %r" % (path.path,))

        # Unexpand ${HOME} to make sure ~ syntax is respected.
        home = os.path.expanduser("~/")
        default = path.path.replace(home, "~/")
        self.patch(_NewConnectionHelper, "_KNOWN_HOSTS", default)
        msg("Patched _KNOWN_HOSTS with %r" % (default,))

        loaded = _NewConnectionHelper._knownHosts()
        self.assertTrue(loaded.hasHostKey("127.0.0.1", key))
开发者ID:AlexanderHerlan,项目名称:syncpy,代码行数:24,代码来源:test_endpoints.py

示例7: test_savingAvoidsDuplication

# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import addHostKey [as 别名]
    def test_savingAvoidsDuplication(self):
        """
        L{KnownHostsFile.save} only writes new entries to the save path, not
        entries which were added and already written by a previous call to
        C{save}.
        """
        path = FilePath(self.mktemp())
        knownHosts = KnownHostsFile(path)
        entry = knownHosts.addHostKey(
            "some.example.com", Key.fromString(sampleKey))
        knownHosts.save()
        knownHosts.save()

        knownHosts = KnownHostsFile.fromPath(path)
        self.assertEqual([entry], list(knownHosts.iterentries()))
开发者ID:12019,项目名称:OpenWrt_Luci_Lua,代码行数:17,代码来源:test_knownhosts.py

示例8: test_defaultInitializerClobbersExisting

# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import addHostKey [as 别名]
 def test_defaultInitializerClobbersExisting(self):
     """
     After using the default initializer for L{KnownHostsFile}, the first use
     of L{KnownHostsFile.save} overwrites any existing contents in the save
     path.
     """
     path = self.pathWithContent(sampleHashedLine)
     hostsFile = KnownHostsFile(path)
     entry = hostsFile.addHostKey(
         "www.example.com", Key.fromString(otherSampleKey))
     hostsFile.save()
     # Check KnownHostsFile to see what it thinks the state is
     self.assertEqual([entry], list(hostsFile.iterentries()))
     # And also directly check the underlying file itself
     self.assertEqual(entry.toString() + "\n", path.getContent())
开发者ID:12019,项目名称:OpenWrt_Luci_Lua,代码行数:17,代码来源:test_knownhosts.py

示例9: test_unsavedEntryHasKeyMismatch

# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import addHostKey [as 别名]
 def test_unsavedEntryHasKeyMismatch(self):
     """
     L{KnownHostsFile.hasHostKey} raises L{HostKeyChanged} if the host key is
     present in memory (but not yet saved), but different from the expected
     one.  The resulting exception has a C{offendingEntry} indicating the
     given entry, but no filename or line number information (reflecting the
     fact that the entry exists only in memory).
     """
     hostsFile = KnownHostsFile(FilePath(self.mktemp()))
     entry = hostsFile.addHostKey(
         "www.example.com", Key.fromString(otherSampleKey))
     exception = self.assertRaises(
         HostKeyChanged, hostsFile.hasHostKey,
         "www.example.com", Key.fromString(thirdSampleKey))
     self.assertEqual(exception.offendingEntry, entry)
     self.assertEqual(exception.lineno, None)
     self.assertEqual(exception.path, None)
开发者ID:12019,项目名称:OpenWrt_Luci_Lua,代码行数:19,代码来源:test_knownhosts.py

示例10: setUp

# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import addHostKey [as 别名]
 def setUp(self):
     """
     Patch 'open' in verifyHostKey.
     """
     self.fakeFile = FakeFile()
     self.patch(default, "_open", self.patchedOpen)
     self.hostsOption = self.mktemp()
     self.hashedEntries = {}
     knownHostsFile = KnownHostsFile(FilePath(self.hostsOption))
     for host in (b"exists.example.com", b"4.3.2.1"):
         entry = knownHostsFile.addHostKey(host, Key.fromString(sampleKey))
         self.hashedEntries[host] = entry
     knownHostsFile.save()
     self.fakeTransport = FakeObject()
     self.fakeTransport.factory = FakeObject()
     self.options = self.fakeTransport.factory.options = {
         'host': b"exists.example.com",
         'known-hosts': self.hostsOption
         }
开发者ID:JohnDoes95,项目名称:project_parser,代码行数:21,代码来源:test_knownhosts.py

示例11: NewConnectionTests

# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import addHostKey [as 别名]
class NewConnectionTests(TestCase, SSHCommandClientEndpointTestsMixin):
    """
    Tests for L{SSHCommandClientEndpoint} when using the C{newConnection}
    constructor.
    """
    def setUp(self):
        """
        Configure an SSH server with password authentication enabled for a
        well-known (to the tests) account.
        """
        SSHCommandClientEndpointTestsMixin.setUp(self)
        # Make the server's host key available to be verified by the client.
        self.hostKeyPath = FilePath(self.mktemp())
        self.knownHosts = KnownHostsFile(self.hostKeyPath)
        self.knownHosts.addHostKey(
            self.hostname, self.factory.publicKeys['ssh-rsa'])
        self.knownHosts.addHostKey(
            self.serverAddress.host, self.factory.publicKeys['ssh-rsa'])
        self.knownHosts.save()


    def create(self):
        """
        Create and return a new L{SSHCommandClientEndpoint} using the
        C{newConnection} constructor.
        """
        return SSHCommandClientEndpoint.newConnection(
            self.reactor, b"/bin/ls -l", self.user, self.hostname, self.port,
            password=self.password, knownHosts=self.knownHosts,
            ui=FixedResponseUI(False))


    def finishConnection(self):
        """
        Establish the first attempted TCP connection using the SSH server which
        C{self.factory} can create.
        """
        return self.connectedServerAndClient(
            self.factory, self.reactor.tcpClients[0][2])


    def assertClientTransportState(self, client, immediateClose):
        """
        Assert that the transport for the given protocol has been disconnected.
        L{SSHCommandClientEndpoint.newConnection} creates a new dedicated SSH
        connection and cleans it up after the command exits.
        """
        # Nothing useful can be done with the connection at this point, so the
        # endpoint should close it.
        if immediateClose:
            self.assertTrue(client.transport.aborted)
        else:
            self.assertTrue(client.transport.disconnecting)


    def test_destination(self):
        """
        L{SSHCommandClientEndpoint} uses the L{IReactorTCP} passed to it to
        attempt a connection to the host/port address also passed to it.
        """
        endpoint = SSHCommandClientEndpoint.newConnection(
            self.reactor, b"/bin/ls -l", self.user, self.hostname, self.port,
            password=self.password, knownHosts=self.knownHosts,
            ui=FixedResponseUI(False))
        factory = Factory()
        factory.protocol = Protocol
        endpoint.connect(factory)

        host, port, factory, timeout, bindAddress = self.reactor.tcpClients[0]
        self.assertEqual(self.hostname, host)
        self.assertEqual(self.port, port)
        self.assertEqual(1, len(self.reactor.tcpClients))


    def test_connectionFailed(self):
        """
        If a connection cannot be established, the L{Deferred} returned by
        L{SSHCommandClientEndpoint.connect} fires with a L{Failure}
        representing the reason for the connection setup failure.
        """
        endpoint = SSHCommandClientEndpoint.newConnection(
            self.reactor, b"/bin/ls -l", b"dummy user",
            self.hostname, self.port, knownHosts=self.knownHosts,
            ui=FixedResponseUI(False))
        factory = Factory()
        factory.protocol = Protocol
        d = endpoint.connect(factory)

        factory = self.reactor.tcpClients[0][2]
        factory.clientConnectionFailed(None, Failure(ConnectionRefusedError()))

        self.failureResultOf(d).trap(ConnectionRefusedError)


    def test_userRejectedHostKey(self):
        """
        If the L{KnownHostsFile} instance used to construct
        L{SSHCommandClientEndpoint} rejects the SSH public key presented by the
        server, the L{Deferred} returned by L{SSHCommandClientEndpoint.connect}
        fires with a L{Failure} wrapping L{UserRejectedKey}.
#.........这里部分代码省略.........
开发者ID:AlexanderHerlan,项目名称:syncpy,代码行数:103,代码来源:test_endpoints.py


注:本文中的twisted.conch.client.knownhosts.KnownHostsFile.addHostKey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。