Created
January 23, 2020 21:56
-
-
Save al26p/bb0a46c5ea7dfe0d226186022a0f363d to your computer and use it in GitHub Desktop.
Draft for ios startup configuration generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"regexp" | |
"strconv" | |
"strings" | |
) | |
type inter struct { | |
tech string | |
id string | |
lan bool | |
ip_address string | |
clock_rate string | |
} | |
type mask struct { | |
masque string | |
imask string | |
} | |
type address struct { | |
ip string | |
net mask | |
} | |
type router struct { | |
hostname string | |
interfaces []inter | |
} | |
func (r router) String() string { | |
s := "\n\n\n\n\n\n _____________________________________\n |" + | |
" |\n | CONFIGURATION FOR " + | |
r.hostname + " |"+ | |
"\n |_____________________________________|\n" + | |
"!\n!\nversion 12.4\n" + | |
"service timestamps debug datetime msec\n" + | |
"service timestamps log datetime msec\n" + | |
"no service password-encryption\n" + | |
"!\n" + | |
"!\n" + | |
"hostname " + r.hostname + "\n" + | |
"boot-start-marker\n" + | |
"boot-end-marker\n" + | |
"!\n" + | |
"!\n" + | |
"no aaa new-model\n" + | |
"memory-size iomem 5\n" + | |
"no ip icmp rate-limit unreachable\n" + | |
"ip cef\n" + | |
"!\n" + | |
"no ip domain lookup\n" + | |
"ip auth-proxy max-nodata-conns 3\n" + | |
"ip admission max-nodata-conns 3\n" + | |
"!\n" + | |
"ip tcp synwait-time 5\n" + | |
"!\n" | |
for _, i := range r.interfaces { | |
if i.tech == "Loopback" { | |
if i.ip_address != "" { | |
s += "!\n" + | |
"interface " + i.tech + i.id + "\n" + | |
" ip address " + i.ip_address + "\n" | |
} | |
} else { | |
s += "!\n" + | |
"interface " + i.tech + i.id + "\n" | |
if i.ip_address != "" { | |
s += " ip address " + i.ip_address + "\n" | |
} else { | |
s += " no ip address \n" | |
s += " shutdown \n" | |
} | |
if i.tech == "Serial" { | |
s += " clock rate " + i.clock_rate + "\n" | |
} else { | |
s += " duplex auto\n speed auto\n" | |
} | |
} | |
} | |
s += "!\n" | |
return s | |
} | |
const end string = "ip forward-protocol nd\n" + | |
"!\n" + | |
"!\n" + | |
"no ip http server\n" + | |
"no ip http secure-server\n" + | |
"!\n" + | |
"no cdp log mismatch duplex\n" + | |
"!\n" + | |
"!\n" + | |
"control-plane\n" + | |
"!\n" + | |
"!\n" + | |
"!\n" + | |
"!\n" + | |
"!\n" + | |
"line con 0\n" + | |
"exec-timeout 0 0\n" + | |
"privilege level 15\n" + | |
"logging synchronous\n" + | |
"line aux 0\n" + | |
"exec-timeout 0 0\n" + | |
"privilege level 15\n" + | |
"logging synchronous\n" + | |
"line vty 0 4\n" + | |
"login\n" + | |
"!\n" + | |
"!\n" + | |
"end\n" | |
var maskDict = map[int]mask{ | |
1: {masque: "128.0.0.0", imask: "127.255.255.255"}, | |
2: {masque: "192.0.0.0", imask: "63.255.255.255"}, | |
3: {masque: "224.0.0.0", imask: "31.255.255.255"}, | |
4: {masque: "240.0.0.0", imask: "15.255.255.255"}, | |
5: {masque: "248.0.0.0", imask: "7.255.255.255"}, | |
6: {masque: "252.0.0.0", imask: "3.255.255.255"}, | |
7: {masque: "254.0.0.0", imask: "1.255.255.255"}, | |
8: {masque: "255.0.0.0", imask: "0.255.255.255"}, | |
9: {masque: "255.128.0.0", imask: "0.127.255.255"}, | |
10: {masque: "255.192.0.0", imask: "0.63.255.255"}, | |
11: {masque: "255.224.0.0", imask: "0.31.255.255"}, | |
12: {masque: "255.240.0.0", imask: "0.15.255.255"}, | |
13: {masque: "255.248.0.0", imask: "0.7.255.255"}, | |
14: {masque: "255.252.0.0", imask: "0.3.255.255"}, | |
15: {masque: "255.254.0.0", imask: "0.1.255.255"}, | |
16: {masque: "255.255.0.0", imask: "0.0.255.255"}, | |
17: {masque: "255.255.128.0", imask: "0.0.127.255"}, | |
18: {masque: "255.255.192.0", imask: "0.0.63.255"}, | |
19: {masque: "255.255.224.0", imask: "0.0.31.255"}, | |
20: {masque: "255.255.240.0", imask: "0.0.15.255"}, | |
21: {masque: "255.255.248.0", imask: "0.0.7.255"}, | |
22: {masque: "255.255.252.0", imask: "0.0.3.255"}, | |
23: {masque: "255.255.254.0", imask: "0.0.1.255"}, | |
24: {masque: "255.255.255.0", imask: "0.0.0.255"}, | |
25: {masque: "255.255.255.128", imask: "0.0.0.127"}, | |
26: {masque: "255.255.255.192", imask: "0.0.0.63"}, | |
27: {masque: "255.255.255.224", imask: "0.0.0.31"}, | |
28: {masque: "255.255.255.240", imask: "0.0.0.15"}, | |
29: {masque: "255.255.255.248", imask: "0.0.0.7"}, | |
30: {masque: "255.255.255.252", imask: "0.0.0.3"}, | |
31: {masque: "255.255.255.254", imask: "0.0.0.1"}, | |
32: {masque: "255.255.255.255", imask: "0.0.0.0"}, | |
} | |
func ask(q string, p string) string { | |
reader := bufio.NewReader(os.Stdin) | |
fmt.Print(q + "[" + p + "]" + "\n--> ") | |
text, _ := reader.ReadString('\n') | |
re := regexp.MustCompile("[^\x20-\x7F]") | |
text = re.ReplaceAllLiteralString(text, "") | |
if text == "" { | |
text = p | |
} | |
return text | |
} | |
func newRouter3175(h string) router { | |
r := router{ | |
hostname: h, | |
interfaces: []inter{{ | |
tech: "Loopback", | |
id: "0", | |
}, { | |
tech: "FastEthernet", | |
id: "0/0", | |
}, { | |
tech: "Serial", | |
id: "0/0", | |
}, { | |
tech: "FastEthernet", | |
id: "0/1", | |
}, { | |
tech: "Serial", | |
id: "0/1", | |
}}} | |
return r | |
} | |
func cidr(s string) address { | |
r := strings.SplitN(s, "/", -1) | |
m, _ := strconv.Atoi(r[1]) | |
return address{ | |
ip: r[0], | |
net: maskDict[m], | |
} | |
} | |
//noinspection GoSnakeCaseUsage | |
func conf_t(f int) router { | |
r := newRouter3175(ask("Entrez le hostname", "R" + strconv.Itoa(f))) | |
for i, _ := range r.interfaces { | |
if r.interfaces[i].tech == "Serial" { | |
r.interfaces[i].clock_rate = "2000000" | |
} | |
if !("y" == ask("Voulez vous configurer "+r.interfaces[i].tech+r.interfaces[i].id+" ?", "n")) { | |
continue | |
} | |
ip := cidr(ask("Entrez l'ip", fmt.Sprintf("%d.%d.%d.%d/32", f, f, f, f))) | |
r.interfaces[i].ip_address = ip.ip + " " + ip.net.masque | |
r.interfaces[i].lan = "y" == ask("Interface LAN ? (utile pour routage)", "n") | |
} | |
return r | |
} | |
func main() { | |
fmt.Println("Bienvenue sur le générateur de configuration iOS") | |
var routers = map[string]router{} | |
n, _ := strconv.Atoi(ask("Combien de routeurs voulez-vous configurer ? ", "4")) | |
for i := 0; i < n; i ++ { | |
r := conf_t(i+1) | |
routers[r.hostname] = r | |
ask("\n\n\n\n" + r.hostname + " Has been configured, press any key to continue.", "") | |
} | |
for k, v := range routers{ | |
ask("Press any key to reveal configuration of ", k) | |
fmt.Println(v) | |
fmt.Println(end) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment