Golang – Reading a text file

This is a recipe for reading a text file. package main import ( "bufio" "fmt" "log" "os" ) func errCallback(e error) { // General log error to console. if e != nil { log.Fatal(e) } } func openTextFile(fname string) { // os.Open returns file pointer and error fptr, e := os.Open(fname) errCallback(e) defer fptr.Close() // … Continue reading Golang – Reading a text file

Golang – How to write ssh.HostKeyCallback

ssh.InsecureIgnoreHostKey is lazy and seems popular? I have seen many tutorials and some codes in github that ignore host key checking, this is not recommended as you need to ensure everytime you connect to the known ssh server is the actual server that serves your purpose, if host key checking is ignore then any server … Continue reading Golang – How to write ssh.HostKeyCallback

[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] Convert IPv4 subnet mask to CIDR representation

Take an example of this subnet mask 255.255.255.240 which CIDR representation is 28. These are the methods to convert to CIDR representation without using any module. Split the netmask by dots, so that each octet is in a list.For each item in the list, use bin function to get the binary representation of each octet, … Continue reading [python] Convert IPv4 subnet mask to CIDR representation

[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