Generator masks extensions dialplan Asterisk

 3r33576. 3r3-31. The system administrator who works with Asterisk often needs to compile masks for dialplan extensions at intervals of DEF codes. Manually doing it is time consuming, and almost the only one working script , which can be found on the Internet, in fact, not so working. 3r33561.  3r33576. 3r33561.  3r33576. I hope someone will use a small cli generator for go, which I recently had to write in order to update the existing dialplan. 3r33561.  3r33576.
3r33561.  3r33576. Features: 3r33561.  3r33576. 3r33561.  3r33576. 3r318.  3r33576.
filter by region
 3r33576.
filter by operator
 3r33576.
simple output formatting
 3r33576.
asterisk comments with information about the interval
 3r33576.
Grouping by telecom operators
 3r33576. 3r3335. 3r33561.  3r33576. 3r3338. Sources and binaries for Mac OS and Linux on GitHub
3r33561.  3r33576. 3r33561.  3r33576.
Code [/b] 3r3494.
package main
3r33576. import (
"encoding /csv"
"fmt"
"io /ioutil" 3r353576. "log"
"net /http" 3rr3576. "os"
"sort" 3r37635. "rrrrrr7676. "strings"
"golang.org/x/text/encoding/charmap"
)
3r33576. type params struct {3r33576. URL string
Region string
Operator string
Comment bool
Prefix string
Suffix string
Group bool
} 3r33535. 3r33576. func main () {
p: = readArgs ()
3r33576. var values ​​[] []string
if p.Region! = "" {3r3353576. values ​​= filterRegion (parse (getCodes (p.URL)), p.Region)
} else {
values ​​= parse (getCodes (p.URL))
} 3r33535. 3r33576. if p.Operator! = "" {3r3353576. values ​​= filterOperator (values, p.Operator)
} 3r33535. 3r33576. if p. Group {
sort.Slice (values, func (i, j int) bool {return values ​​[i] [4] f-1 {3r33576. v = "
} 3r33535.} 3r33535. x = append (x, v)
} 3r33535. return string (x)
} 3r33535. 3r33576. func filterRegion (values ​​[] []string, region string)[] []string {3r33535. var res[] []string
for _, v: = range values ​​{
if strings.Contains (strings.ToLower (v[5]), strings.ToLower (region)) {
res = append (res, v)
} 3r33535.} 3r33535. return res
} 3r33535. 3r33576. func filterOperator (values ​​[] []string, operator string)[] []string {3r33535. var res[] []string
for _, v: = range values ​​{
if strings.Contains (strings.ToLower (v[4]), strings.ToLower (operator)) {
res = append (res, v)
} 3r33535.} 3r33535. return res
} 3r33535. 3r33576. func validate (min, max, dif int) bool {
if max-min == dif-1 {
Return true
} 3r33535. return false
} 3r33535. 3r33576. func convert (v[]string) (pre, min, max, dif int) {
pre, err: = strconv.Atoi (v[0])
if err! = nil {3r3353576. log.Panic (err) 3r33535.} 3r33535. min, err = strconv. Atoi (v[1])
if err! = nil {3r3353576. log.Panic (err) 3r33535.} 3r33535. max, err = strconv. Atoi (v[2])
if err! = nil {3r3353576. log.Panic (err) 3r33535.} 3r33535. dif, err = strconv. Atoi (v[3])
if err! = nil {3r3353576. log.Panic (err) 3r33535.} 3r33535. return
} 3r33535. 3r33576. type runes[]rune 3r3766 3r33576. func increment (r runes) runes {3r33535. if len (r) <= 1 {
return r
} 3r33535. var res runes
i, err: = strconv.Atoi (string (r))
if err! = nil {3r3353576. log.Panic (err) 3r33535.} 3r33535. i ++ 3r3766. res = runes (strconv.Itoa (i))
if len (res) == len (r) {
return res
} 3r33535. 3r33576. res = res.reverse ()
l: = len (res)
3r33576. for i: = 0; i 3r3307. res = append (res, '0')
3r33576.} 3r33535. 3r33576. return res.reverse ()
} 3r33535. 3r33576. func (r runes) reverse () (rev[]rune) {
for i: = len (r) - 1; i> = 0; i-- {
rev = append (rev, r[i])
} 3r33535. return
} 3r33535. 3r33576. func hi (r runes) runes {3r33576. var res runes
for i: = len (r) - 1; i> = 1; i-- {
res = append (res, '9')
if r[i]== '0' {
continue
} 3r33535. break
} 3r33535. l: = len (res)
r = r.reverse ()
for i: = l; i < len(r); i++ {
res = append (res, r[i])
} 3r33535. 3r33576. return res.reverse ()
} 3r33535. 3r33576. func decrement (r runes) runes {3r33535. if len (r) <= 1 {
return r
} 3r33535. var res runes
i, err: = strconv.Atoi (string (r))
if err! = nil {3r3353576. log.Panic (err) 3r33535.} 3r33535. i--
res = runes (strconv.Itoa (i))
3r33576. if len (res) == len (r) {
return res
} 3r33535. 3r33576. res = res.reverse ()
l: = len (res)
3r33576. for i: = 0; i 3r3307. res = append (res, '0')
3r33576.} 3r33535. 3r33576. return res.reverse ()
} 3r33535. 3r33576. func low (r runes) runes {3r33576. var res runes
res = append (res, r[0]) 3r3-3576. for i: = 2; i <= len(r); i++ {
res = append (res, '0')
} 3r33535. return res
} 3r33535. 3r33576. func compute (pre, min, max, suf string) {
3r33576. //mask found if min and max are equal
if min == max {
fmt.Printf ("% v% v% vn", pre, min, suf)
return
} 3r33535. 3r33576. var prefix[]rune 3r3766 mi: = runes (min)
ma: = runes (max)
3r33576. if len (mi)! = len (ma) {
log.Panic ("it is not equal") and
} 3r33535. 3r33576. for k, v: = range ma {3r3353576. if v == mi[k]{3r33576. prefix = append (prefix, v)
continue
} 3r33535. break
} 3r33535. 3r33576. if l: = len (prefix); l! = 0 {
compute (pre + string (prefix), string (mi[l:]), string (ma[l:]), suf)
return
} 3r33535. 3r33576. var suffix runes
for k, v: = range mi.reverse () {
if ma.reverse ()[k]-v == 9 {
suffix = append (suffix, 'X')
continue
} 3r33535. break
} 3r33535. 3r33576. if l: = len (suffix); l! = 0 {
compute (pre,
string (mi)[:len(mi)-l],
string (ma)[:len(ma)-l],
string (suffix) + suf) 3r33535. return
} 3r33535. if len (mi) == 1 {3r3353576. compute (pre + "["+string(mi)+"-"+string(ma)+"]", "", "", suf)
return
} 3r33535. 3r33576. zc, err: = strconv.Atoi (min)
if err! = nil {3r3353576. log.Panic (err) 3r33535.} 3r33535. 3r33576. if zc == 0 {3r33535. compute (pre, string (mi), string (decrement (low (ma))), suf)
compute (pre, string (low (ma)), max, suf)
return
} 3r33535. 3r33576. compute (pre, string (mi), string (hi (mi)), suf)
3r33576. if increment (hi (mi))[0]== mi[0]{3r33576. compute (pre, string (increment (hi (mi))), max, suf)
} else {
if increment (hi (mi))[0]== ma[0]{3r33576. compute (pre, string (increment (hi (mi))), max, suf)
} else {
compute (pre, string (increment (hi (mi))), string (decrement (low (ma))), suf)
compute (pre, string (low (ma)), max, suf)
} 3r33535.} 3r33535.} 3r33535. 3r33576. func readArgs () params {3r3353576. p: = params {
URL: "https://rossvyaz.gov.ru/docs/articles/DEF-9x.csv",
Region: "",
Operator: "",
Comment: false,
Prefix: "",
Suffix: "",
Group: false,
} 3r33535. 3r33576. wait: = false
key: = ""
3r33576. for _, v: = range os.Args[1:]{3r33576. if wait {3r33576. switch key {
case "-u":
p.URL = v
case "-r":
p.Region = v
case "-o":
p.Operator = v
case "-p":
p.Prefix = v
case "-s":
p.Suffix = v
} 3r33535. wait = false
} else {
if v == "-c" {3r3353576. p.Comment = true
continue
} 3r33535. if v == "-g" {
p.Group = true
continue
} 3r33535. 3r33576. switch v {3r33576. case "-u", "-r", "-o", "-p", "-s":
key = v
wait = true
case "-h":
help () 3r33576. os.Exit (0)
default:
fmt.Println ("unknown option:", v) 3r3-3576. fmt.Println ("show help: genmask -h")
os.Exit (1) 3r33535.} 3r33535.} 3r33535.} 3r33535. if wait == true {
fmt.Printf ("missing value of% s argumentn", key) 3r-3576. os.Exit (1) 3r33535.} 3r33535. return p 3r3766.} 3r33535. 3r33576. func help () {
fmt.Println ("usage: genmask[-u ] [-r ] [-c] [-p ] [-s ]")
fmt.Println ("t-u
: url to csv file. Default is https://rossvyaz.gov.ru/docs/articles/DEF-9x.csv") 3r33576. fmt.Println ("t-r
: find the value of the region in the csv file.")
fmt.Println ("t is better to use short masks, because of the csv file.") 3r33576. fmt.Println ("t-o
: find the value in the operator field.")
fmt.Println ("t is better to use short masks, because of the csv file.") 3r33576. fmt.Println ("t-c Print a comment: <; code, min, max, length, cellular operator, region> before each interval")
fmt.Println ("t-p
: Print a prefix for each mask")
fmt.Println ("t-s
: Print a suffix for each mask")
fmt.Println ("t-g
: Group output by cellular operator")
fmt.Println ("show this help: genmask -h")
} 3r33535. 3r3-3559. 3r33560. 3r33561.  3r33576. 3r? 3572. 3r? 3572. 3r33561.  3r33576.
Example output 3r3493. 3r3494.
genmask -r Amursk -g 3r33535. ; AO "Company TransTeleCom" 3-333576. 958034XXXX
; OJSC "MTT" 3-333576. 95840430XX
; GAZPROM TELECOM LTD. 9584586XXX
; Global Telecom Ltd.
958385[0-1]XXX 3r33576. ; Scartel Ltd.
99916[5-6]XXXX 3r33535. 99646[0-2]XXXX 3r33535. 99638[3-7]XXXX 3r33535. 999681[0-4]XXX 3r33576. 991116[8-9]XXX 3r33576. 9911170XXX
; T2 Mobile
90101368XX
994200XXXX
; PJSC "Vympel-Communication" 3r33576. 968246XXXX
9696392XXX
965677XXXX
96567[0-2]XXXX 3r33535. 96384[2-9]XXXX 3r33535. 96381[8-9]XXXX 3r33535. 96381[0-7]XXXX 3r33535. 90989[3-5]XXXX 3r33535. 96380XXXXX
96228[3-5]XXXX 3r33535. 96195XXXXX
90988[3-5]XXXX 3r33535. 90981XXXXX
96813[2-4]XXXX 3r33535. 96229[3-5]XXXX 3r33535. ; PJSC "MegaFon" 3r33576. 999251[5-9]XXX 3r33576. 92949[3-4]XXXX 3r33535. 92949[0-1]XXXX 3r33535. 92947[5-9]XXXX 3r33535. 924748XXXX
92484XXXXX
92474[4-6]XXXX 3r33535. 92467XXXXX
92468[0-4]XXXX 3r33535. 92458[0-4]XXXX 3r33535. 92444XXXXX
92434XXXXX
934476XXXX
92404[0-1]XXXX 3r33535. 924025[4-7]XXX 3r33576. 999254[7-8]XXX 3r33576. 92414XXXXX
; PJSC "Mobile TeleSystems" 3-333576. 91453[0-2]XXXX 3r33535. 91406[0-4]XXXX 3r33535. 91404XXXXX
914538XXXX
914539[0-2]XXX 3r33576. 9145[5-9]XXXXX
9146[0-1]XXXXX
9143[8-9]XXXXX
3r3-3559. 3r33560. 3r33561.  3r33576. 3r? 3572. 3r? 3572. 3r? 3572. 3r33576. 3r33576. 3r33576. 3r? 3569. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d,! 1): e.attachEvent ("onload", d ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () (); 3r33570. 3r33576. 3r? 3572. 3r33576. 3r33576. 3r33576. 3r33576.
+ 0 -

Add comment