i have a code file mongo.py
like below to connect n query mongo with username
host
password
and its work, but how i can connect to ssh with ssh address
username
and auth method private_key
? private_key
in the other file
here is my sample config
with ssh:
sql_local = {'mongomdm':{'host':'xx.xxx.xxx.xx', 'user':'aaa', 'pkey':'/Users/satu/dua/tiga/config/settings/googlecloud_dev_rsa'}}
i have ready some references and should import library like this ?
from paramiko import SSHClient, AutoAddPolicy, RSAKey
from paramiko.auth_handler import AuthenticationException
from scp import SCPClient, SCPException
from io import StringIO
here is my code file mongo.py
import csv
import pandas as pd
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure
import config.config as cfg
import config.modules.common as comm
from bson.objectid import ObjectId
class mongoFunc:
def __init__(self):
self.types = 'mongodb'
self.host = cfg.sql_local[self.types]['host']
self.user = cfg.sql_local[self.types]['user']
self.password = cfg.sql_local[self.types]['password']
self.uri = 'mongodb://{user}:{password}@{host}'.format(user = self.user, password = self.password, host = self.host)
def connection(self):
try:
client= MongoClient(self.uri)
client.server_info()
print('Connection Established')
except ConnectionFailure as err:
raise(err)
return client
def get_collection(self, client, database, collection):
self.client= client
self.database= database
self.collection= collection
db= self.client[self.database]
return db[self.collection]
def get_query(self,id_data= None,start_date=None,end_date=None,query=None):
self.id = ObjectId(id_data)
self.start_date = start_date
self.end_date = end_date
self.query = query
if self.end_date:
script= {'date':{'$gt':self.start_date,'$lte':self.end_date}}
if self.end_date is None:
script= {'date':{'$gt':self.start_date}}
if self.id:
script = {'_id':{'$gt':self.id}}
if self.query:
script.update(self.query)
return script
def get_data_to_pandas(self, database, collection, query, skip, limit=None):
self.database = database
self.collection = collection
self.query = query
self.limit = limit
self.skip = skip
self.client = self.connection()
self.collection = self.get_collection(self.client,self.database,self.collection)
if limit:
cursor = self.collection.find(self.query).skip(self.skip).limit(self.limit)
if not limit :
cursor = collection.find(self.query).skip(self.skip)
df = pd.DataFrame(list(cursor))
return df
Edit:
def connection(self):
try:
print('Establishing SSH Connection')
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if (self.password == ''):
private_key = paramiko.RSAKey.from_private_key_file(self.pkey)
self.client.connect(hostname=self.host, port=self.port, username=self.user, pkey=private_key, allow_agent=False,look_for_keys=False)
print("Connected to the server"),self.host
else:
self.client.connect(hostname=self.host, port=self.port, username=self.user, password=self.password, allow_agent=False,look_for_keys=False)
print("Connected to the server"),self.host
except paramiko.AuthenticationException:
print("Authentication failed, please verify your credentials")
result_flag = False
except paramiko.SSHException as sshException:
print("Could not establish SSH connection: %s") % sshException
result_flag = False
else:
result_flag = True
return result_flag
def get_collection(self, client, database, collection):
self.client= client
self.database= database
self.collection= collection
db= self.client[self.database]
return db[self.collection]`
I got an error:
line 64, in get_collection
db= self.client[self.database]
TypeError: 'bool' object is not subscriptable
What should I do?