首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用twisted.conch访问SSH服务器所需的最低信息量是多少?

使用twisted.conch访问SSH服务器所需的最低信息量是多少?
EN

Stack Overflow用户
提问于 2019-06-17 10:13:04
回答 1查看 104关注 0票数 0

当涉及到SSH的事情时,我有点不知所措。基本上,我试图通过和SSH隧道访问朋友服务器使用扭曲的海螺。他向我提供了以下资料:

代码语言:javascript
复制
MONGO_HOST = "ip address"
MONGO_DB = "server name"
MONGO_USER = "user name"
MONGO_PASS = "server password"

我能够使用python库motor.motor_asyncio获得这些信息(我需要这个库是异步兼容的,以便与其他库一起使用),但是由于一些原因,如果需要的话,我不能在我计划运行这个程序的树莓派上工作。

长话短说,我想知道是否有人可以用一些示例代码来帮助我使用上面给出的twisted.conch信息访问我的朋友服务器。

我看了twisted.conch的阅读文档,但这个例子需要的信息比我能提供的多(我认为),而且在网络/SSH/等方面我也无法理解。

提前谢谢。我愿意投入这项工作,但我需要知道去哪里寻找。

以下是我到目前为止的相关代码:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2019-06-21 08:34:41

您可以使用Conch转发端口,如下所示:

代码语言:javascript
复制
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来访问端口转发是最容易的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56624002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档