paramiko 服务器模式端口转发 [英] paramiko server mode port forwarding

查看:69
本文介绍了paramiko 服务器模式端口转发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 paramiko 实现一个 ssh 服务器,它只处理像这样的-R"端口转发请求:

I need to implement a ssh server using paramiko that only handles '-R' port forwarding requests like this:

ssh -N -T -R 40005:destination_host:22 user@example.com

到目前为止,我所理解的我必须实现 ServerInterface.check_port_forward_request 并在之后的某个时间创建一个套接字并侦听指定的端口.任何通过 Channel/Connection 的数据都分别转到 Connection/Channel

So far from what i understand i'll have to implement ServerInterface.check_port_forward_request and at some point after, create a socket and listen to the specified port. Any data that comes through the Channel/Connection go to Connection/Channel respectively

class Server (paramiko.ServerInterface):
    .
    .
    .
    def check_port_forward_request(self, address, port):
        'Check if the requested port forward is allowed'
        ...
        return port

def handler(chan, port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('', port))
    sock.listen(1)
    conn, addr = s.accept()    

    while True:
        r, w, x = select.select([conn, chan], [], [])
        if conn in r:
            data = conn.recv(1024)
            if len(data) == 0:
                break
            chan.send(data)
        if chan in r:
            data = chan.recv(1024)
            if len(data) == 0:
                break
            conn.send(data)
    chan.close()
    conn.close()
    verbose('Tunnel closed from %r' % (chan.origin_addr,))

thr = threading.Thread(target=handler, args=(chan,server_port))
thr.setDaemon(True)
thr.start()

这是实现服务器端 paramiko ssh 端口转发背后的总体思路吗?我应该在 check_port_forward_request 或其他地方启动线程吗?

Is this the general idea behind implementing server-side paramiko ssh port forwarding? Should i start the thread inside check_port_forward_request or somewhere else?

推荐答案

这是来自 Paramiko 的反向端口转发源的一个工作示例:

Here's a working example from Paramiko's source of reverse port forwarding:

https://github.com/paramiko/paramiko/blob/master/demos/rforward.py

这篇关于paramiko 服务器模式端口转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆