Error in connecting (SSH) remote server (Mongo DB) through jump server using Paramico in python Script










1















I am trying to connect a Mongo DB server through jump server in my python script using Paramico on a AWS instance.



Let me explain how i connect to Mongo DB server on terminal on AWS:



 $ssh -i Key.pem harman@51.8.11.205 # I ssh on Jump server with this command


After login on to Jump server I use following command to SSH on Mongo DB server:



 $ssh ctpms-lmini1-Jsct1


here i do not require any authentication. No username, password or key is required.



Now i have to establish the same connection with python scrips. Here is my



Script:



 #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)


I am successfully able to connect to jump server and able to run :



 Jump_client.exec_command('ls') 


Establish a channel between Mongo db server and Jump server:



 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)


This was also succefull mongochannel is returning following:



 <paramiko.Channel 0 (open) window=2097152 in-buffer=23 -> <paramiko.Transport at 0x7a952f90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>


Now I am trying use mongochannel as socket and creating new session but getting error:



 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)


Here is the error:



 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:


As I mentioned earlier I do not require any username, password or key for mongodb. Please suggest.



I also tried jumpssh for this purpose but it also did not work.Here is the code:



 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)



Jump server connection worked fine but following code for mongodb server is giving error:



 remote_session = gateway_session.get_remote_session('11.16.19.23','test_user')


Error:



 --------------------------------------------------------------------------
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


Please note I also tried using same pem file for mongodb server that I use for jumpserver but it did not help.



Please provide your suggestions to resolve this.



Regards,
Bharat










share|improve this question






















  • What are you actually trying to do here? Are you trying to connect an SSH session to the "server with MongoDB installed"? Or are you trying to "Use an SSH Tunnel to talk to MongoDB for CRUD"? Those are two different things and your current approach is not how you do the latter one. If you want a "tunnel" then this is usually best set up as port forwarding separately to the application that wants to talk to MongoDB, With port forwarding your application just uses the "local port" which the other software layer forwards to the remote server.

    – Neil Lunn
    Nov 14 '18 at 1:25











  • Also please put auth credentials on your database. Even on a server with only SSH access it's really not safe to not have those.

    – Neil Lunn
    Nov 14 '18 at 1:26















1















I am trying to connect a Mongo DB server through jump server in my python script using Paramico on a AWS instance.



Let me explain how i connect to Mongo DB server on terminal on AWS:



 $ssh -i Key.pem harman@51.8.11.205 # I ssh on Jump server with this command


After login on to Jump server I use following command to SSH on Mongo DB server:



 $ssh ctpms-lmini1-Jsct1


here i do not require any authentication. No username, password or key is required.



Now i have to establish the same connection with python scrips. Here is my



Script:



 #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)


I am successfully able to connect to jump server and able to run :



 Jump_client.exec_command('ls') 


Establish a channel between Mongo db server and Jump server:



 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)


This was also succefull mongochannel is returning following:



 <paramiko.Channel 0 (open) window=2097152 in-buffer=23 -> <paramiko.Transport at 0x7a952f90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>


Now I am trying use mongochannel as socket and creating new session but getting error:



 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)


Here is the error:



 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:


As I mentioned earlier I do not require any username, password or key for mongodb. Please suggest.



I also tried jumpssh for this purpose but it also did not work.Here is the code:



 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)



Jump server connection worked fine but following code for mongodb server is giving error:



 remote_session = gateway_session.get_remote_session('11.16.19.23','test_user')


Error:



 --------------------------------------------------------------------------
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


Please note I also tried using same pem file for mongodb server that I use for jumpserver but it did not help.



Please provide your suggestions to resolve this.



Regards,
Bharat










share|improve this question






















  • What are you actually trying to do here? Are you trying to connect an SSH session to the "server with MongoDB installed"? Or are you trying to "Use an SSH Tunnel to talk to MongoDB for CRUD"? Those are two different things and your current approach is not how you do the latter one. If you want a "tunnel" then this is usually best set up as port forwarding separately to the application that wants to talk to MongoDB, With port forwarding your application just uses the "local port" which the other software layer forwards to the remote server.

    – Neil Lunn
    Nov 14 '18 at 1:25











  • Also please put auth credentials on your database. Even on a server with only SSH access it's really not safe to not have those.

    – Neil Lunn
    Nov 14 '18 at 1:26













1












1








1








I am trying to connect a Mongo DB server through jump server in my python script using Paramico on a AWS instance.



Let me explain how i connect to Mongo DB server on terminal on AWS:



 $ssh -i Key.pem harman@51.8.11.205 # I ssh on Jump server with this command


After login on to Jump server I use following command to SSH on Mongo DB server:



 $ssh ctpms-lmini1-Jsct1


here i do not require any authentication. No username, password or key is required.



Now i have to establish the same connection with python scrips. Here is my



Script:



 #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)


I am successfully able to connect to jump server and able to run :



 Jump_client.exec_command('ls') 


Establish a channel between Mongo db server and Jump server:



 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)


This was also succefull mongochannel is returning following:



 <paramiko.Channel 0 (open) window=2097152 in-buffer=23 -> <paramiko.Transport at 0x7a952f90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>


Now I am trying use mongochannel as socket and creating new session but getting error:



 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)


Here is the error:



 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:


As I mentioned earlier I do not require any username, password or key for mongodb. Please suggest.



I also tried jumpssh for this purpose but it also did not work.Here is the code:



 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)



Jump server connection worked fine but following code for mongodb server is giving error:



 remote_session = gateway_session.get_remote_session('11.16.19.23','test_user')


Error:



 --------------------------------------------------------------------------
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


Please note I also tried using same pem file for mongodb server that I use for jumpserver but it did not help.



Please provide your suggestions to resolve this.



Regards,
Bharat










share|improve this question














I am trying to connect a Mongo DB server through jump server in my python script using Paramico on a AWS instance.



Let me explain how i connect to Mongo DB server on terminal on AWS:



 $ssh -i Key.pem harman@51.8.11.205 # I ssh on Jump server with this command


After login on to Jump server I use following command to SSH on Mongo DB server:



 $ssh ctpms-lmini1-Jsct1


here i do not require any authentication. No username, password or key is required.



Now i have to establish the same connection with python scrips. Here is my



Script:



 #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)


I am successfully able to connect to jump server and able to run :



 Jump_client.exec_command('ls') 


Establish a channel between Mongo db server and Jump server:



 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)


This was also succefull mongochannel is returning following:



 <paramiko.Channel 0 (open) window=2097152 in-buffer=23 -> <paramiko.Transport at 0x7a952f90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>


Now I am trying use mongochannel as socket and creating new session but getting error:



 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)


Here is the error:



 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:


As I mentioned earlier I do not require any username, password or key for mongodb. Please suggest.



I also tried jumpssh for this purpose but it also did not work.Here is the code:



 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)



Jump server connection worked fine but following code for mongodb server is giving error:



 remote_session = gateway_session.get_remote_session('11.16.19.23','test_user')


Error:



 --------------------------------------------------------------------------
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


Please note I also tried using same pem file for mongodb server that I use for jumpserver but it did not help.



Please provide your suggestions to resolve this.



Regards,
Bharat







python mongodb amazon-web-services ssh






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 23:08









BharatBharat

61




61












  • What are you actually trying to do here? Are you trying to connect an SSH session to the "server with MongoDB installed"? Or are you trying to "Use an SSH Tunnel to talk to MongoDB for CRUD"? Those are two different things and your current approach is not how you do the latter one. If you want a "tunnel" then this is usually best set up as port forwarding separately to the application that wants to talk to MongoDB, With port forwarding your application just uses the "local port" which the other software layer forwards to the remote server.

    – Neil Lunn
    Nov 14 '18 at 1:25











  • Also please put auth credentials on your database. Even on a server with only SSH access it's really not safe to not have those.

    – Neil Lunn
    Nov 14 '18 at 1:26

















  • What are you actually trying to do here? Are you trying to connect an SSH session to the "server with MongoDB installed"? Or are you trying to "Use an SSH Tunnel to talk to MongoDB for CRUD"? Those are two different things and your current approach is not how you do the latter one. If you want a "tunnel" then this is usually best set up as port forwarding separately to the application that wants to talk to MongoDB, With port forwarding your application just uses the "local port" which the other software layer forwards to the remote server.

    – Neil Lunn
    Nov 14 '18 at 1:25











  • Also please put auth credentials on your database. Even on a server with only SSH access it's really not safe to not have those.

    – Neil Lunn
    Nov 14 '18 at 1:26
















What are you actually trying to do here? Are you trying to connect an SSH session to the "server with MongoDB installed"? Or are you trying to "Use an SSH Tunnel to talk to MongoDB for CRUD"? Those are two different things and your current approach is not how you do the latter one. If you want a "tunnel" then this is usually best set up as port forwarding separately to the application that wants to talk to MongoDB, With port forwarding your application just uses the "local port" which the other software layer forwards to the remote server.

– Neil Lunn
Nov 14 '18 at 1:25





What are you actually trying to do here? Are you trying to connect an SSH session to the "server with MongoDB installed"? Or are you trying to "Use an SSH Tunnel to talk to MongoDB for CRUD"? Those are two different things and your current approach is not how you do the latter one. If you want a "tunnel" then this is usually best set up as port forwarding separately to the application that wants to talk to MongoDB, With port forwarding your application just uses the "local port" which the other software layer forwards to the remote server.

– Neil Lunn
Nov 14 '18 at 1:25













Also please put auth credentials on your database. Even on a server with only SSH access it's really not safe to not have those.

– Neil Lunn
Nov 14 '18 at 1:26





Also please put auth credentials on your database. Even on a server with only SSH access it's really not safe to not have those.

– Neil Lunn
Nov 14 '18 at 1:26












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290848%2ferror-in-connecting-ssh-remote-server-mongo-db-through-jump-server-using-par%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290848%2ferror-in-connecting-ssh-remote-server-mongo-db-through-jump-server-using-par%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands