本文整理汇总了Python中twisted.conch.client.knownhosts.KnownHostsFile.save方法的典型用法代码示例。如果您正苦于以下问题:Python KnownHostsFile.save方法的具体用法?Python KnownHostsFile.save怎么用?Python KnownHostsFile.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.conch.client.knownhosts.KnownHostsFile
的用法示例。
在下文中一共展示了KnownHostsFile.save方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_savingAvoidsDuplication
# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import save [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()))
示例2: test_saveResetsClobberState
# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import save [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()))
示例3: test_defaultInitializerClobbersExisting
# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import save [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())
示例4: setUp
# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import save [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
}
示例5: setUp
# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import save [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
}
示例6: test_readExisting
# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import save [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))
示例7: NewConnectionTests
# 需要导入模块: from twisted.conch.client.knownhosts import KnownHostsFile [as 别名]
# 或者: from twisted.conch.client.knownhosts.KnownHostsFile import save [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}.
#.........这里部分代码省略.........