Reputation: 673
If I want to create a small database in Python, what would be the best practice to do it?
For instance, if I want to store output from Cisco's command "sh ip route" in a database,
O 1.1.1.1 [110/2] via 10.0.0.1, 00:00:23, FastEthernet0/0
stores these values:
1.1.1.1 —> next hop, outgoing interface, source (like O, C, S)
I feel that SQL would be the best solution but if not SQL, what is another option?
Upvotes: 5
Views: 8436
Reputation: 999
routing tables look to have a form more similar to documents that relational tables in a RDBMS like Postgres or MySQL.
I would prefer to use a document oriented database in this case. Regarding which database to use it depends on your deployment scenario and application architecture.
If your aim is a database server which can be accessed via the network then Mongo is a very good choice. If you are going for a single-node application which will access the data only locally, then you can have a look in TinyDB.
With TinyDB your code would look like:
from tinydb import TinyDB, Query
db = TinyDB('/path/to/db.json')
db.insert({ 'Destination': '1.1.1.1',
'NextHop': '10.0.0.1',
'A': '*',
'P': 'B',
'Prf': 170',
....
})
And finally look for the individual routes as:
route = Query()
db.search(route.Destination == '1.1.1.1')
# [{'Destination': '1.1.1.1','NextHop': '10.0.0.1','A': '*',...}]
or get all of them at once:
db.all()
Hope it helps!
Upvotes: 7