2017-10-05 1 views
0

포트를 전달하는 Paramiko 서버를 구축하려고합니다. 나는 demo server 코드openssh 클라이언트 -N 옵션을 사용하여 Paramiko 서버 포트 전달

#!/usr/bin/env python 
import base64 
from binascii import hexlify 
import os 
import socket 
import sys 
import threading 
import traceback 
import paramiko 
from paramiko.py3compat import b, u, decodebytes 
import logging 

logging.basicConfig(level=logging.INFO) 
logger = logging.getLogger(__name__) 

host_key = paramiko.RSAKey(filename="test_rsa.key") 
logger.info("Read key: " + u(hexlify(host_key.get_fingerprint()))) 


class Server(paramiko.ServerInterface): 
    def __init__(self): 
     self.event = threading.Event() 

    def check_auth_publickey(self, username, key): 
     logger.info("Auth attempt with key: " + u(hexlify(key.get_fingerprint()))) 
     try: 
      with open("client_rsa.pub.stripped", "rb") as f: 
       good_key = f.read() 
      good_pub_key = paramiko.RSAKey(data=decodebytes(good_key)) 
     except: 
      logger.exception("failed to read public key") 
      return paramiko.AUTH_FAILED 
     if (username == "robey") and (key == good_pub_key): 
      return paramiko.AUTH_SUCCESSFUL 
     return paramiko.AUTH_FAILED 

    def get_allowed_auths(self, username): 
     return "publickey" 

    def check_channel_request(self, kind, chanid): 
     logger.info("inside channel request") 
     return paramiko.OPEN_SUCCEEDED 

    def check_channel_direct_tcpip_request(self, chanid, origin, destination): 
     return paramiko.OPEN_SUCCEEDED 

    def check_channel_shell_request(self, channel): 
     self.event.set() 
     return True 

if __name__ == "__main__": 
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
    sock.bind(("", 2200)) 
    sock.listen(100) 
    logger.info("Listening for connection ...") 
    client, addr = sock.accept() 
    logger.info("Got a connection!") 

    with paramiko.Transport(client) as t: 
     t.load_server_moduli() 
     t.add_server_key(host_key) 
     server = Server() 
     t.start_server(server=server) 

     # wait for auth 
     chan = t.accept(20) 
     if chan is None: 
      logger.info("*** No channel.") 
      sys.exit(1) 
     logger.info("Authenticated!") 

     # prompt for more information 
     chan.send("Username: ") 
     f = chan.makefile("rU") 
     username = f.readline().strip("\r\n") 
     logger.info("received username: " + username) 
     chan.close() 

의 코드를 적응 그리고 성공적으로 연결하려면이 명령을 사용하고 있습니다 :

ssh -i client_rsa.key -p 2200 -L 9999:localhost:4000 -T [email protected] 

을하지만, 나는 ssh 클라이언트, 즉의 -N 옵션을 사용하려고 할 때 :

ssh -i client_rsa.key -p 2200 -L 9999:localhost:4000 -T -N [email protected] 

Paramiko 서버는 결코 check_channel_request 기능에 도달하지 클라이언트를 인증 한 후 중단됩니다.

INFO:__main__:Read key: 689f8799e649f931b116b19227dbb2a3 
INFO:__main__:Listening for connection ... 
INFO:__main__:Got a connection! 
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_7.2p2) 
INFO:paramiko.transport:Auth rejected (none). 
INFO:__main__:Auth attempt with key: cdbb2439816b22a59ee036be3a953e51 
INFO:paramiko.transport:Auth rejected (publickey). 
INFO:__main__:Auth attempt with key: 11c470c88233719a2499f03336589618 
INFO:paramiko.transport:Auth granted (publickey). 

가 Paramiko 서버가이 상황을 처리 할 수 ​​있도록 얻을 어쨌든 거기 다음은 실행의 로그는?

답변

0

알아 냈습니다. 아무 일도 일어나지 않은 이유는 터널 전달을 사용하려고 할 때까지 터널 전달이 열리지 않기 때문입니다. -N 옵션을 사용하지 않아도 터널이 생성되지 않았습니다. 그래서 대답은 SSH 연결을 생성 한 후 로컬 포트를 사용해야한다는 것입니다.