我正在尝试在AWS实例上使用Paramico通过python脚本中的跳转服务器连接Mongo DB服务器。
让我解释一下我如何在AWS终端上连接到Mongo DB服务器:
$ssh -i Key.pem harman@51.8.11.205 # I ssh on Jump server with this command
登录Jump服务器后,我使用以下命令在Mongo DB服务器上进行SSH:
$ssh ctpms-lmini1-Jsct1
在这里,我不需要任何身份验证。不需要用户名,密码或密钥。
现在,我必须使用python scrips建立相同的连接。这是我的
脚本:
#Connect to Jump Server
Jump_client = paramiko.SSHClient()
Jump_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
Jump_client.connect(hostname='51.8.11.205', port=22,
username='harman', key_filename='key.pem', banner_timeout=10)
我能够成功连接到跳转服务器并能够运行:
Jump_client.exec_command('ls')
在Mongo数据库服务器和Jump服务器之间建立通道:
chan = client.get_transport()
dest_addr = ('11.16.19.23', 22) #Mongo DB server IP#
local_addr = ('51.8.11.205', 22) #Jump server IP#
mongochannel = chan.open_channel("direct-tcpip", dest_addr,local_addr)
这也是成功的mongochannel返回以下内容:
<paramiko.Channel 0 (open) window=2097152 in-buffer=23 -> <paramiko.Transport at 0x7a952f90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>
现在我正在尝试使用mongochannel作为套接字并创建新会话,但出现错误:
mongo_client = paramiko.SSHClient()
mongo_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
mongo_client.load_system_host_keys()
mongo_client.connect(hostname='11.16.19.23', sock=mongochannel)
这是错误:
EOFError Traceback (most recent call last)
<ipython-input-26-2000cff93e10> in <module>()
2 client1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
3 client1.load_system_host_keys()
----> 4 client1.connect(hostname='11.16.19.23', sock=vmchannel)
/usr/local/lib/python2.7/dist-packages/paramiko/client.pyc in connect(self, hostname, port, username, password, pkey, key_filename, timeout, allow_agent, look_for_keys, compress, sock, gss_auth, gss_kex, gss_deleg_creds, gss_host, banner_timeout, auth_timeout, gss_trust_dns, passphrase)
395 sec_opts.key_types = [keytype] + other_types
396
--> 397 t.start_client(timeout=timeout)
398
399 # If GSS-API Key Exchange is performed we are not required to check the
/usr/local/lib/python2.7/dist-packages/paramiko/transport.pyc in start_client(self, event, timeout)
585 e = self.get_exception()
586 if e is not None:
--> 587 raise e
588 raise SSHException("Negotiation failed.")
589 if event.is_set() or (
EOFError:
如前所述,我不需要mongodb的任何用户名,密码或密钥。请提出建议。
我也为此目的尝试过jumpssh,但它也没有用。代码如下:
from jumpssh import SSHSession
gateway_session = SSHSession('51.8.11.205', 'test_user', proxy_transport=None, private_key_file='test.pem',
port = 22,password = None,missing_host_key_policy = None, compress = False)
跳转服务器连接工作正常,但是mongodb服务器的以下代码给出了错误:
remote_session = gateway_session.get_remote_session('11.16.19.23','test_user')
错误:
--------------------------------------------------------------------------
ConnectionError Traceback (most recent call last)
<ipython-input-66-10b1b39a1f86> in <module>()
----> 1 remote_session = gateway_session.get_remote_session('11.16.19.23','harman')
/usr/local/lib/python2.7/dist-packages/jumpssh/session.pyc in get_remote_session(self, host, username, retry, private_key_file, port, password, retry_interval, compress)
496 password=password,
497 compress=compress).open(retry=retry,
--> 498 retry_interval=retry_interval)
499
500 # keep reference to opened session, to be able to reuse it later
/usr/local/lib/python2.7/dist-packages/jumpssh/session.pyc in open(self, retry, retry_interval)
159 else:
160 raise exception.ConnectionError("Unable to connect to '%s:%s' with user '%s'"
--> 161 % (self.host, self.port, self.username), original_exception=ex)
162
163 # Get the client's transport
ConnectionError: Unable to connect to '11.16.19.23:22' with user 'test_user': [Errno 104] Connection reset by peer
请注意,我还尝试为mongodb服务器使用与用于Jumpserver相同的pem文件,但没有帮助。
请提供您的建议以解决此问题。
关于, 巴拉特