Forum Discussion

elenal's avatar
elenal
Occasional Contributor
6 years ago

Connecting to mongo database through SSH tunnel

Here's what I have.

 

 

 

def sshHost = ''
def sshUser = ''
def sshPort = ''
def localPort = ??
def mongoPort=27017 //default MongoPort

JSch jSch=new JSch();
jSch.addIdentity(
sshUser, // String userName
prvkey, // byte[] privateKey
null, // byte[] publicKey
emptyPassPhrase // byte[] passPhrase
);
Session session = jSch.getSession(sshUser, sshHost, sshPort);
session.setConfig("StrictHostKeyChecking", "no")

try{
session.connect()
session.setPortForwardingL(sshPort, mongohost, mongoPort);

log.info ("SSH connection set up")

//connect to mongo
def mongo= new GMongo (new MongoURI(mongoendpoint))
def db = mongo.getDB(mongocollection)
def collection = db.getCollection(collection)
def result = collection.findOne()

mongo.close()
}
finally{
session.disconnect()
log.info ("SSH disconnected")
null
}

Looks like SSH connections is established successfully, but it can't connect to Mongo.

The closest solution I found is  https://community.smartbear.com/t5/SoapUI-Open-Source/Tunnel-via-ssh-port-22-then-port-forward-3306-to-access-the/m-p/130765#M22383

 

Not sure, but I think the problem is in this line:

session.setPortForwardingL(localPort, mongohost, mongoPort);

So how do I know what the local port is?

and also what should be in mongohost if I'm connection to replice?

 
 
 
 
No RepliesBe the first to reply