python - 使用 twisted.conch 访问 SSH 服务器所需的最少信息量是多少?
问题描述
当谈到这个 SSH 事情时,我有点不知所措。基本上我正在尝试使用扭曲的海螺通过 SSH 隧道访问朋友服务器。他给了我以下信息:
MONGO_HOST = "ip address"
MONGO_DB = "server name"
MONGO_USER = "user name"
MONGO_PASS = "server password"
我能够使用 python 库 motor.motor_asyncio 获取这些信息(我需要它是异步兼容的,以便与其他库一起使用)但是由于我可以在必要时进入的原因,不适用于树莓派我计划在上面运行这个程序。
长话短说,我想知道是否有人可以帮助我提供一些示例代码,以使用上面提供的信息和 twisted.conch 访问我的朋友服务器。
我查看了twisted.conch readthedocs,但该示例需要的信息比我所能提供的更多(我认为),并且在网络/SSH/等方面超出了我的想象。
提前致谢。我愿意投入工作,但我需要知道去哪里寻找。
到目前为止,这是我的相关代码:
from motor.motor_asyncio import AsyncIOMotorClient
from sshtunnel import SSHTunnelForwarder
MONGO_HOST = "host address"
MONGO_DB = "server name"
MONGO_USER = "username"
MONGO_PASS = "password"
server = SSHTunnelForwarder(
MONGO_HOST,
ssh_username=MONGO_USER,
ssh_password=MONGO_PASS,
remote_bind_address=('address', gate),
local_bind_address=('address', gate)
)
server.start()
client = AsyncIOMotorClient('address', gate)
db = client.server_name
解决方案
您可以像这样使用 Conch 转发端口:
rom twisted.internet.defer import Deferred
from twisted.conch.scripts import conch
from twisted.conch.scripts.conch import ClientOptions, SSHConnection
from twisted.conch.client.direct import connect
from twisted.conch.client.default import SSHUserAuthClient, verifyHostKey
from twisted.internet.task import react
def main(reactor):
# authenticate as this user to SSH
user = "sshusername"
# the SSH server address
host = "127.0.0.1"
# the SSH server port number
port = 22
# a local port number to listen on and forward
localListenPort = 12345
# an address to forward to from the remote system
remoteForwardHost = "127.0.0.1"
# and the port number to forward to
remoteForwardPort = 22
conch.options = ClientOptions()
conch.options.parseOptions([
# don't ask the server for a shell
"--noshell",
# set up the local port forward
"--localforward={}:{}:{}".format(
localListenPort,
remoteForwardHost,
remoteForwardPort,
),
# specify the hostname for host key checking
host,
])
# don't stop when the last forwarded connection stops
conch.stopConnection = lambda: None
# do normal-ish authentication - agent, keys, passwords
userAuthObj = SSHUserAuthClient(user, conch.options, SSHConnection())
# create a Deferred that will tell `react` when to stop the reactor
runningIndefinitely = Deferred()
# establish the connection
connecting = connect(
host,
port,
conch.options,
verifyHostKey,
userAuthObj,
)
# only forward errors so the reactor will run forever unless the
# connection attempt fails. note this does not set up reconnection for a
# connection that succeeds and then fails later.
connecting.addErrback(runningIndefinitely.errback)
return runningIndefinitely
# run the reactor, call the main function with it, passing no other args
react(main, [])
有些 API 很奇怪,因为它们专注于 CLI。您不必这样做,但使用这些 API 而不是更专注于编程使用的 API 最容易访问端口转发。
推荐阅读
- javascript - 作为 npm 包的一部分提供 mp3 文件
- azure - Azure Functions 2 - 如何控制 json 序列化设置
- spring-boot - 测试期间的 Spring Boot 配置类与属性
- java - 春季启动中的错误“org.springframework.web.client.HttpClientErrorException:404 null”
- go - 使用扫描仪时如何获取包括换行符在内的行长?
- visual-studio-mac - Visual Studio for Mac 中的预生成/生成后事件
- javascript - 浏览器同步不提供根文件
- java - 解析失败:Lcom/google/android/gms/common/internal/Preconditions;在统一
- python - 在一行迭代中扩展多个元素
- php - 使用 PHP 将数据插入 SQLite3 数据库时出错