我有一個腳本來啓動運行在集羣特定節點上的Jupyter Notebook服務器,並且需要能夠從本地Windows機器(瀏覽器界面)訪問運行實例。 ssh命令是通過paramiko處理的,我想要創建隧道的類似pythonic方式,而不僅僅是系統調用。我嘗試了sshtunnel,不幸的是,這是我目前的絆腳石。ssh隧道到運行在集羣計算節點上的服務器
我需要創建兩個隧道,如下所述。爲了清楚起見,HOME是Windows機器,HOST是羣集頭節點,NODE是服務器運行的特定節點。
服務器被綁定到本地主機,默認情況下在端口8888母國和東道國下方的端口是任意的,我只是用它們來說明(在現實中,最便捷的母港也是8888)
[HOME] <------> [HOST] <------> [NODE]
1111 2222 8888
我知道下面的代碼是不完整的,只是顯而易見的錯誤,因爲我無法弄清楚如何正確組合本地/遠程綁定。現在我不知道我是否在咆哮錯誤的樹 - 文檔表明我可以一次創建兩個隧道,但是我需要兩個單獨的實例嗎?
from sshtunnel import SSHTunnelForwarder
t1 = SSHTunnelForwarder(
('headnode.server', 22),
ssh_username='meagain',
ssh_pkey='~/.ssh/id_rsa',
local_bind_address = ('localhost',1111),
remote_bind_addresses =[ ('localhost',2222), ('compute.node',8888)])
t1.start()
編輯:嘗試選項2,如下所述。使用SSH客戶端,配置設置並在命令行上正常工作。作爲參考,這是阿納康達下用蟒3.6,Windows 7的機器(因此附加文件路徑的下方爭論)
print(sshtunnel.__version__)
'0.1.0'
print(paramiko.__version__)
'2.1.2'
t1 = sshtunnel.SSHTunnelForwarder(
NODE,
ssh_username=USER,
ssh_pkey=paramiko.RSAKey.from_private_key_file(os.path.expanduser(os.path.normpath('~/.ssh/id_rsa'))),
ssh_proxy_enabled=True, # DEFAULT
ssh_config_file=os.path.expanduser(os.path.normpath('~/.ssh/config')), # if rather than default
local_bind_address=('localhost', 1111),
remote_bind_address=('localhost', 8889)
)
t1.start()
日誌上:
2017-04-06 12:28:25,523| INF | MainThrea/[email protected] | 0 keys loaded from agent
t1.start()
File "D:\Anaconda3\lib\site-packages\sshtunnel.py", line 1224, in start
reason='Could not establish session to SSH gateway')
File "D:\Anaconda3\lib\site-packages\sshtunnel.py", line 1036, in _raise
raise exception(reason)
sshtunnel.BaseSSHTunnelForwarderError: Could not establish session to SSH gateway
>>> 2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | paramiko.ssh_exception.ProxyCommandFailure: ('ssh HOST -W NODE:22', 'An operation was attempted on something that is not a socket')
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] |
2017-04-06 12:28:25,538| ERR | MainThrea/[email protected] | Could not connect to gateway NODE:22 : ssh HOST -W NODE:22
Traceback (most recent call last):
File "D:\Bioinformatics\Scripts\bsub_jupyter\tunneller.py", line 234, in <module>
2017-04-06 12:28:25,523| INF | MainThrea/[email protected] | Connecting to gateway: NODE:22 as user 'USER'
2017-04-06 12:28:25,523| DEB | MainThrea/[email protected] | Concurrent connections allowed: True
2017-04-06 12:28:25,523| DEB | MainThrea/[email protected] | Trying to log in with key: b'1e44a013d97d417e4900025c6c11d073'
2017-04-06 12:28:25,523| DEB | MainThrea/[email protected] | Connecting via proxy: 'HOST'
2017-04-06 12:28:25,523| ERR | Thread-2/[email protected] | Exception: ('ssh HOST -W NODE:22', 'An operation was attempted on something that is not a socket')
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | Traceback (most recent call last):
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | File "D:\Anaconda3\lib\site-packages\paramiko\proxy.py", line 96, in recv
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | [self.process.stdout], [], [], select_timeout)
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | OSError: [WinError 10038] An operation was attempted on something that is not a socket
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] |
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | During handling of the above exception, another exception occurred:
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] |
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | Traceback (most recent call last):
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | File "D:\Anaconda3\lib\site-packages\paramiko\transport.py", line 1749, in run
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | self._check_banner()
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | File "D:\Anaconda3\lib\site-packages\paramiko\transport.py", line 1893, in _check_banner
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | buf = self.packetizer.readline(timeout)
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | File "D:\Anaconda3\lib\site-packages\paramiko\packet.py", line 331, in readline
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | buf += self._read_timeout(timeout)
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | File "D:\Anaconda3\lib\site-packages\paramiko\packet.py", line 485, in _read_timeout
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | x = self.__socket.recv(128)
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | File "D:\Anaconda3\lib\site-packages\paramiko\proxy.py", line 107, in recv
2017-04-06 12:28:25,538| ERR | Thread-2/[email protected] | raise ProxyCommandFailure(' '.join(self.cmd), e.strerror)
上午嘗試選項2,因爲我一直想要設置配置文件。它在命令行上工作得很好。不幸的是,它拋出一個錯誤,它無法連接到ssh網關。我認爲我已經縮小到ProxyCommand - 當配置一個代理服務器的paramiko客戶端時,我得到了這個:''paramiko.ssh_exception.ProxyCommandFailure:('ssh HOST -W NODE:22','An操作嘗試的東西,不是一個套接字「)'' – blackgore
參考,這裏的配置:'' 主機HOST \t主機名HOSTIPADDRESS \t用戶的用戶 \t IdentityFile〜/。SSH/id_rsa 主機節點 \t用戶的用戶 \t ProxyCommand SSH主機-W%H:%P '' – blackgore
可以設置'DEBUG_LEVEL = 「DEBUG」'和問題後呢?您的版本相同('[l .__ version__ for [paramiko,sshtunnel]]') – fernandezcuesta