python – Generating RSA key pairs with pycryptodome module

Thank you for the creator of pycryptodome module, this module has made RSA key pair easy. This recipe presents a function for generating private and public key pair. from Crypto.PublicKey import RSA # pycryptodome def rsa_key_gen(bits=2048, privatekey_path=RSA_PRIVATE_KEY, pubkey_path=RSA_PUBLIC_KEY): private_key = RSA.generate(bits) with open(privatekey_path, "wb") as privkey_file: privkey_file.write(private_key.export_key()) with open(pubkey_path, "wb") as pubkey_file: pubkey_file.write(private_key.publickey().export_key())

[python]Simple syslog server with socketserver module

The code of syslog server can be found here. The syslog server is simple because it only collects logs from syslog client, I have not tested with multiple clients yet, which I think may need threading. The logs are saved to log file and also display on the console. This demo uses a cisco switch … Continue reading [python]Simple syslog server with socketserver module

[python]Creating vlans on multiple switches

Introduction The entire script demo can be found here. The script reads from an excel sheet named as "vlans.xlsx" and extracts the information, the information is then converted into vlan commands with a jinja2 template, the script is able to send to multiple switches by using threading, on each thread a new Switch instance is … Continue reading [python]Creating vlans on multiple switches

[python]Test connection with socket module

This is the code snippet that checks if a specific port is listening on the target server or not, if the port could not be reached and does not exist the socket module throws a timeout exception or ConnectionRefusedError. from socket import socket, timeout as _timeout, AF_INET, SOCK_STREAM ERRORS = TimeoutError, ConnectionRefusedError, _timeout def has_service(address, … Continue reading [python]Test connection with socket module

[python]Record router information with mongodb

Introduction This is a lab demonstration on how to record cisco router's information to mongo database. Mongo database is very easy to learn for new developers who understand the json structure because the structure to insert data into the database is very json-like this is very easy to learn without knowing SQL syntax. I am … Continue reading [python]Record router information with mongodb

[python]Delete many by objectid using pymongo

I have multiple duplicated entries, I think this is the problem with NoSQL type of database, although easy to use it has no normalization method like the relational type database. So my mongo database have duplicated like this: These are the duplicates when I accidentally run the script multiple times with the same device info. … Continue reading [python]Delete many by objectid using pymongo

[python]Change interesting contents of a text file into dictionary

Introduction This exercise downloads the CHECKSUM file from the centos download page, the file downloaded is a temporary file and is destroyed once it is closed after used. The data read from the temporary file is then processed into a dictionary, the dictionary key is the filename and the value is the hash digest. Tempfile … Continue reading [python]Change interesting contents of a text file into dictionary

[python]Download file and use tqdm for progress bar.

Download file with requests To download the file from web with requests module is the easiest, you just need to turn on stream while using get method. Then download the file chunk by chunk by using the iter_content method. Here is the example code on how to download file with requests. import requests from pathlib … Continue reading [python]Download file and use tqdm for progress bar.