Unexpected session close error is thrown when connecting netconf - python

I am using ncclient to connect to the netconf. However when ever i try to connect through python
"ncclient.transport.errors.SessionCloseError: Unexpected session close" error is thrown. the code snippet that i am using is given below
manager.connect('<servername>',22,username='<username>')
Any help on this is much appriciated. I am able to connect to the remote server by using public key, hence i didnt provide passwordk in connect
And in the netconf server logs i am able to see access-denied error. (I got the same prob even when i tried with username and pwd)

You haven't given a lot of information.
Which version of ncclient are you using?
Which version of Python are you using?
Which NETCONF implementation are you trying to connect to? Is this to an actual switch or router, or something like a Linux server running libnetconf or yuma?
Based on the info here, I could imagine a couple of things being wrong:
paramiko isn't using the right key to establish SSH transport.
You're attempting to establish a NETCONF session with an SSH server rather than a NETCONF server.
In your script, create some logs with something like manager.logging.basicConfig(filename='ncclient.log', level=manager.logging.DEBUG) and then re-run your script - do you get anything more informative?
This is an old question, but I hope I can point you in the right direction at least.

its possible that your machines don't know each other (like when you connect via normal ssh and get the "unknown key, really connect (y/n)?" error. In that case, by default the session will not connect. To change this behavior use the "unknown_host_cb" parameter:
def allowUnknownHosts(host,fingerprint):
return True
self.manager = manager.connect(host=host, port=port, username=user,password=password, unknown_host_cb=allowUnknownHosts)

Related

Exasol_Error: I keep getting Exasol connection error timed out

I am trying to connect to my Exasol SaaS database, I tried via these tools(TALEND, DBVISUALIZER, POWERBI) and via python but I cannot connect and I keep getting the same error.
I saw another post on Exasol community https://community.exasol.com/t5/discussion-forum/exaconnectionfailederror/m-p/8049#M1855 of this type of error but it doesn't explain exactly what was done to fix the error. I tried via the ODBC Data Source administrator(64-bit) too but still the same error. Maybe its an connection issue with my pc self but I'm not sure or maybe I am just inserting wrong values I don't know.
Oh the values I inserted are the recommended ones from what Exasol docs states and I have removed anything about proxy or vpn.
I put my errors under. I tried via different devices and I get the same error I really don't know what I can do any more, so any help will be greatly appreciated.
Note: I am using the Exasol SaaS database and I am currently on the trial mode so I am not sure if this is limiting me.
**Errors: **
Error message odbc exasol: [EXASOL][EXASolution driver]connection attempt timed out.
Error message Talend : Connection failure. You must change the Database Settings.
java.lang.RuntimeException: com.exasol.jdbc.ConnectFailed: connect timed out ->
Caused by: com.exasol.jdbc.ConnectFailed: connect timed out
Error message pyexasol : socket.timeout: timed out
Error message dbvisualizer : java.net.SocketTimeoutException: Connect timed out com.exasol.jdbc.ConnectFailed: java.net.SocketTimeoutException: Connect timed out
Error message Power BI desktop : Details: "ODBC: ERROR [HYT00][EXASOL][EXASolution driver]Connection attempt timed out."
My applications versions:
DbVisualizer Free 14.0.1 (build: 3540)
Talend Open Studio Data integration(8.0.1.2021119_1610)
java version -> jdk-16.0.02
Power BI -> Version: 2.110.1085.0 64-bit (October 2022)
ODBC : exasolodbc x64 7.1.14
JDBC : exasoljdbc 7.1.14
Python: python 3.8.10 -> pyexasol : 0.25.1
The error means that the client is not able to reach the host for some reason. Try the following:
Make sure the database is still online (they auto-shutdown after 2 hours if there is no activity by default)
Check that the IP Address of the host you are connecting with is added to the allow list in the SaaS UI. (see the docs)
Check if your host is able to reach the host and port specified in the SaaS UI (for example using telnet on port 8563). Maybe some firewall is preventing access to the database?
So I did more digging. actually I have no idea what the issue was.
Talend:
I made a connection via JDBC in Talend with the help of exasol-support. The DBType Exasol in talend doesn't work for some reason, its not known if it's talend side or Exasol side, maybe this will be updated in the future. Just remember in the jdbc url type this: "jdbc:exa:yourconnectionstring", don't forget the "exa".
PowerBI:
I tried the connection string with fingerprint method that worked for me. Just put the fingerprint with the connection string and it should connect.
https://exasol.my.site.com/s/article/PowerBI-Encryption-Fingerprint-Issue-in-Exasol-7-1?language=en_US
DBvisualizer:
I had a wrong in connection string.
Python:
I had a wrong in connection string.
Hopefully this helps someone.

connect to the database via Python

I am trying to connect to the database via Python ?
I use oracle with the sqldevolper and have no problems there, but now when connecting between oracle and python the problem appears.
When I try to connect, I get the following error :
cx_Oracle.DatabaseError: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
I tried the script with spyder:
import cx_Oracle
con = cx_Oracle.connect(
user="abc",
password ="pass",
dsn="....")
ORA-12514 means you are connecting to a listener, but the service name you specified in your connection string, isn't known by the listener. As you omitted the definition of the connect string (from SQL Developer and Python), hard to say if there is a typo or what caused it...
On the server you can check with "lsnrctl status" on the known services... maybe that gives you a hint, what might be wrong.

Why is PyMongo 3 giving ServerSelectionTimeoutError?

I'm using:
Python 3.4.2
PyMongo 3.0.2
mongolab running mongod 2.6.9
uWSGI 2.0.10
CherryPy 3.7.0
nginx 1.6.2
uWSGI start params:
--socket 127.0.0.1:8081 --daemonize --enable-threads --threads 2 --processes 2
I setup my MongoClient ONE time:
self.mongo_client = MongoClient('mongodb://user:pw#host.mongolab.com:port/mydb')
self.db = self.mongo_client['mydb']
I try and save a JSON dict to MongoDB:
result = self.db.jobs.insert_one(job_dict)
It works via a unit test that executes the same code path to mongodb. However when I execute via CherryPy and uWSGI using an HTTP POST, I get this:
pymongo.errors.ServerSelectionTimeoutError: No servers found yet
Why am I seeing this behavior when run via CherryPy and uWSGI? Is this perhaps the new thread model in PyMongo 3?
Update:
If I run without uWSGI and nginx by using the CherryPy built-in server, the insert_one() works.
Update 1/25 4:53pm EST:
After adding some debug in PyMongo, it appears that topology._update_servers() knows that the server_type = 2 for server 'myserver-a.mongolab.com'. However server_description.known_servers() has the server_type = 0 for server 'myserver.mongolab.com'
This leads to the following stack trace:
result = self.db.jobs.insert_one(job_dict)
File "/usr/local/lib/python3.4/site-packages/pymongo/collection.py", line 466, in insert_one
with self._socket_for_writes() as sock_info:
File "/usr/local/lib/python3.4/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/usr/local/lib/python3.4/site-packages/pymongo/mongo_client.py", line 663, in _get_socket
server = self._get_topology().select_server(selector)
File "/usr/local/lib/python3.4/site-packages/pymongo/topology.py", line 121, in select_server
address))
File "/usr/local/lib/python3.4/site-packages/pymongo/topology.py", line 97, in select_servers
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: No servers found yet
We're investigating this problem, tracked in PYTHON-961. You may be able to work around the issue by passing connect=False when creating instances of MongoClient. That defers background connection until the first database operation is attempted, avoiding what I suspect is a race condition between spin up of MongoClient's monitor thread and multiprocess forking.
As mentioned here: https://stackoverflow.com/a/54314615/8953378
I added ?ssl=true&ssl_cert_reqs=CERT_NONE to my connection string, and it fixed the issue.
so instead of:
connection_string = "mongodb+srv://<USER>:<PASSWORD>#<CLUSTER>/<COLLECTION>"
I wrote:
connection_string = "mongodb+srv://<USER>:<PASSWORD>#<CLUSTER>/<COLLECTION>?ssl=true&ssl_cert_reqs=CERT_NONE"
(Note that if you have other parameters in your connection string, you need to change the ? to & )
I am not sure if you are using the MongoDB paired with AWS Cloud service. But if you are, I found that you have to specify which IP Address you want MongoDB to have access to.
So what you need to do is add the IP Address of your host server to allow entry.
In MongoAtlas, this can be done at this page
I know there was already a solution to the same issue, but I didn't find a solution that helped my situation, so wanted to post this, so others could benefit if they ever face the same problem that I do.
I fixed it for myself by downgrading from pymongo 3.0 to 2.8. No idea what's going on.
flask/bin/pip uninstall pymongo
flask/bin/pip install pymongo==2.8
I had the same problem with Pymongo 3.5
Turns out replacing localhost with 127.0.0.1 or corresponding ip address of your mongodb instance solves the problem.
I solved this by installing dnspython (pip install dnspython). The issue is that: "The "dnspython" module must be installed to use mongodb+srv:// URIs"
Go to your Atlas Console > Network Access, then add your client IP address,
Ex. 0.0.0.0/00
(Note: All client ips can access your database)
In my case
I was using Mongo Atlas
I got another IP adress after a router reboot
hence I had to add that IP to the whitelist on Mongo Atlas settings via
MongoAtlas website -> Network Access -> IP Whitelist -> Add IP Address -> Add Current IP Address
then wait for IP Address's status to change to Active and then try to run the app again
if you are using a repl.it server to host, just add the host ip you used to configure your server, for me it was 0.0.0.0, which is the most common
I was facing the same exception today. In my case, the proxy settings were probably blocking the connection since I could establish a successful connection to the mongodb by changing my wifi. Even if this question is marked as solved already, it can hopefully narrow down the problem for some others.
I've come accross the same problem and finally I found that the client IP is blocked by the firewall of the mongo server.
I encountered this too.
This could be due to pymongo3 isn't fork safe.
I fix this by adding --lazy-apps param to uwsgi, this can avoid the "fork safe" problem.
seeing uwsgi doc preforking-vs-lazy-apps-vs-lazy.
Notice, no sure for this two having positive connection.
I simply added my current IP address in the network access tab, as it got changed automatically. Deleted the earlier one, there was a slight change in IP address.
pymongo 3 will not tell you your connection failed when you instantiate your client. You may not be connected.
https://api.mongodb.com/python/3.5.1/api/pymongo/mongo_client.html
"it no longer raises ConnectionFailure if they are unavailable ..
You can check if the server is available like this:"
from pymongo.errors import ConnectionFailure
client = MongoClient()
try:
# The ismaster command is cheap and does not require auth.
client.admin.command('ismaster')
except ConnectionFailure:
print("Server not available")
maybe you can try to add your server ip address into the mongod.conf file.
if you use linux(ubuntu) os,you can try my solution:
modify mongod.conf file:
vi /etc/mongod.conf
and you can add mongodb server ip address behind 127.0.0.1,and save:
net:
port:27017
bindIp:127.0.0.1,mongodb server ip
in the teminal:
sudo service mongod restart
Now,you can try to connect mongodb by using pymongo MongoClient.
That error has occurred because there is no MongoDB server running in the background. To run the MongoDB server open cmd or anaconda prompt and type this:-
"C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe"
then run
import pymongo
myclient = pymongo.MongoClient()
mydb = myclient["mydatabase"]
myclient.list_database_names()
I'm using pymongo 3.2 and I run into the same error, however it was a missconfiguration in my case. After enabling authorization, I forgot to update the port in the url which ended up in a connection timout. Probably it is worth to mention that ?authSource might be required as it is typically different than the database storing the application data.
I commented out bindIP variable in mongod.conf instead of allowing all connections (for which you have to enter 0.0.0.0). Of course, beware of the consequence.
The developers are investigating this problem, tracked in PYTHON-961. You may be able to work around the issue by running mongod.exe manually and monitoring it. This issue arises when the console freezes and you can hit the enter if the mongod console is got stuck. This is the simplest solution for now until the developers fix this bug.
I ran into the same issue during development. It was due to the fact that mongodb wasn't running on my local machine (sudo systemctl restart mongod to get mongodb running on Ubuntu).
I faced the same error on windows and I just started the MongoDB service
open services ctrl+R then type services.msc then Enter
For my case I only set my ip allow list 0.0.0.0 allow anywhere but you can set your ip using "what is my ip" and copy paste it to network access > add ip
I have been struggling with same problem. Read and either insert did not work at all failed with ServerSelectionTimeoutError.
I have been using pymongo==3.11.4 on Ubuntu 18.04 LTS.
Tried use connect=False, pass extra ?ssl=true&ssl_cert_reqs=CERT_NONE options to my connection string and other suggestions listed above. In my case they didn't work.
Finally simple tried to upgrade to pymongo==3.12.1 and connection started to work without passing connect=false, and other extra arguments suggested.
login = '<USERNAME>'
password = '<PASSWORD>'
host = '*.mongodb.net'
db = '<DB>'
uri = f'mongodb+srv://{login}:{password}#{host}/{db}?retryWrites=true&w=majority'
client = MongoClient(uri, authsource='admin')#, connect=False)
collection = client.db.get_collection('collection_name')
# t = collection.find_one({'hello': '1'})
t = collection.insert_one({'hello': '2'})
print(t)
Make sure you entered the user password, not the MongoDB account password. I encountered similar issue. In my case, I mistakenly entered the MongoDB account password instead of the user password.
I had this issue today. I managed to deal with it by:
installing dnspython library > going to MongoDB webpage > signing in > security > network access > add IP address > adding the IP address from where my request comes from.
Hope this could help someone.
I had the same issue..the code that was working perfectly fine 2 minutes before gave this error. I was looking for solutions over google for about 30 minutes and it automatically got fixed. The problem could be my home internet connection. Just a guess but if you haven't made any changes to the code or any other config file best to wait for sometime and retry.
I was also facing the same issue. Then, I added
import certifi
Client = MongoClient("mongodb+srv://<username>:<password>#cluster0.ax9ugoz.mongodb.net/?retryWrites=true&w=majority", tlsCAFile=certifi.where())
and it solved my issue.
Certifi provides a collection of Root Certificates for validating the trustworthiness of SSL certificates while verifying the identity of TLS hosts.
This has been fixed in PyMongo with this pull_request.
This problem solved when I just toggled the MongoDB in the services to running which was stopped previously.
First set up the MongoDB environment.
Run this on CMD - "C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe"
Open another CMD and run this - "C:\Program Files\MongoDB\Server\3.6\bin\mongo.exe"
And then you can use pymongo [anaconda prompt]
import pymongo
from pymongo import MongoClient
client = MongoClient()
db = client.test_db
collection = db['test_coll']
Refer - https://docs.mongodb.com/tutorials/install-mongodb-on-windows/

Using Pymongo to connect to MongoDB on AWS instance from Windows

An error is repeatedly being thrown at this line:
client = MongoClient('ec2-12-345-67-89.us-east-2.compute.amazonaws.com', 27017,
ssl=True, ssl_keyfile='C:\\mongo.pem')
(Paths and instance name changed for obvious reasons)
The port (27017) for mongo is allowed inbound connections from my AWS security group. First, I allowed only my IP, now I'm allowing all via that port. I have tried preceding the connection string with "mongodb://" and removing the SSL arguments (I'm fairly certain I don't need it).
The error IntelliJ keeps throwing me is:
pymongo.errors.ConnectionFailure: [WinError 10061] No connection could be made because the target machine actively refused it
It works if I transport the script to the AWS instance and replace the DNS with 'localhost' and remove SSL parameters, but I need this to work remotely.
Three ideas:
Ensure "bind_ip" is set to "0.0.0.0" in your mongod.conf and restart mongod, as #ajduke suggests.
Make sure mongod is running.
Try to connect to the mongod from your client machine using the "mongo" shell to see if it gives you a more informative error.

SSH using Pythondb using private key

I need to SSH into a db using Python, I'm using PythonDB for this. I saw this question which details how to do this but I can't seem to get the syntax right. Would someone be able to point me in the right direction....? I also need to use a private key, how would I go about inserting that...?
ssh -L 9990:127.0.0.0:3396 <79.xxx.xx.xxx>
database = MySQLdb.connect(host'127.0.0.0', port=3306, user='jack', passwd='pass', db='test')
As said in the answer to the other question in the ssh-tunnel you are forwarding from port 9990 on your local machine to the (standard mysql) port on the remove machine. To send requests through that ssh-tunnel you need to connect to port 9990 instead of 3306:
database = MySQLdb.connect(host'127.0.0.0', port=9990, ....

Categories