This recipe demonstrates if string is a valid IPv4, and also to resolve a FQDN to IP address. This recipe uses os.Args to get the second argument as FQDN and call net.ResolveIPAddr() function to do a FQDN resolution to IP address. package main import ( "fmt" "log" "net" "os" ) func isIPv4Addr(ip string) bool { … Continue reading Golang – Check if IPv4 is valid and resolve FQDN
Author: cyruslab
Golang – Write sentence to file
This recipe demonstrates how to use flag to build a command line program, how to convert string slice to string and how to write sentence to file specified by user. Usage example: writefile -f test.txt hi this is a test. package main import ( "flag" "io" "log" "os" "strings" ) func convertSliceToString(s []string) string { … Continue reading Golang – Write sentence to file
Golang – Download files from website
This recipe downloads all kinds of files from website, but there is no progress tracking, only download the file. package main import ( "io" "log" "net/http" "os" ) func downloadFile(filepath, url string) error { resp, err := http.Get(url) if err != nil { return err } defer resp.Body.Close() f, err := os.Create(filepath) if err != … Continue reading Golang – Download files from website
Golang – Template
In python I can use Jinja2 template module to parse variables to Cisco command template, in Golang there is text/template which is similar to Jinja2 template engine for python. This recipe gives an example on how to parse variables into a text template. The types within the struct have to be exported in order for … Continue reading Golang – Template
Golang – Obfuscate password input
This recipe shows how to obfuscate the password when user is typing the password. package p1 import ( "fmt" "log" "os" "golang.org/x/crypto/ssh/terminal" ) func getPassword() string { fmt.Println("\nPassword: ") // https://godoc.org/golang.org/x/crypto/ssh/terminal#ReadPassword // terminal.ReadPassword accepts file descriptor as argument, returns byte slice and error. passwd, e := terminal.ReadPassword(int(os.Stdin.Fd())) if e != nil { log.Fatal(e) } // … Continue reading Golang – Obfuscate password input
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]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