Това проработи при мен. Проверих примера за paramiko по-горе, но не разбрах и намерих това. Взех кода от тук и модифициран само малко за моите нужди.
def data_call(collection_name, query={}):
from sshtunnel import SSHTunnelForwarder
import pymongo
MONGO_HOST = 'HOST'
SERVER_USER = 'server_user'
PRIVATE_KEY ='pem.txt'
MONGO_USER='user_mongo'
MONGO_PASS ='pass_mongo'
MONGO_DB = "db"
# define ssh tunnel
server = SSHTunnelForwarder(
MONGO_HOST,
ssh_username=SERVER_USER,
ssh_pkey=PRIVATE_KEY,
remote_bind_address=('127.0.0.1', 27017)
)
# start ssh tunnel
server.start()
connection = pymongo.MongoClient('127.0.0.1', server.local_bind_port)
db = connection[MONGO_DB]
data = db[collection_name].find(query)
return data