SSH tunnel to MongoDB in Python
from sshtunnel import SSHTunnelForwarder
import pymongo
import pprint
import json
MONGO_HOST = "x.x.x.x"
MONGO_DB = "ids"
SSH_USER = "dmitri.telinov"
SSH_KEY = "/Users/dtelinov/.ssh/id_rsa"
SSH_KEY_PW = ""
server = SSHTunnelForwarder(
(MONGO_HOST, 999),
ssh_username=SSH_USER,
ssh_pkey=SSH_KEY,
ssh_private_key_password=SSH_KEY_PW,
remote_bind_address=('127.0.0.1', 27017)
)
server.start()
client = pymongo.MongoClient('127.0.0.1', server.local_bind_port) # server.local_bind_port is assigned local port
db = client[MONGO_DB]
# execute search
for post in db.users.find( {"type" : "system"}, {"username.encryptedData":1,"_id":0} ):
data = json.dumps(post)
o = json.loads(data)
print o['username']['encryptedData']
server.stop()