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]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]Netmask to inverse mask
Cisco IOS uses inverse mask in access control list and router network command, only Cisco uses inverse mask instead of actual netmask. Suppose if the mask is 255.255.255.240, then the inverse mask is 0.0.0.15, another example is 255.255.240.0 then the inverse mask is 0.0.15.255, if you are handling cisco acl with python then there will … Continue reading [python]Netmask to inverse mask
[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
[selenium] Select option from dropdown box
There are two ways to do this, first find the xpath of the element of the option then issue a click method, but this method will not work on some website which monitors mouse hover event and enable the element if the mouse is hovered over the drop down box. Another sure way of selecting … Continue reading [selenium] Select option from dropdown box