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

时间:2019-06-17 02:13:03

标签: python twisted twisted.conch

当涉及到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阅读的文档,但是该示例需要的信息比我所能提供的更多(我认为),并且在网络/ SSH / etc方面是我的头等大事。

先谢谢了。我愿意投入工作,但是我需要知道在哪里看。

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

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

1 个答案:

答案 0 :(得分:0)

您可以像这样用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,最容易访问端口转发。