init project

This commit is contained in:
Maxim Slipenko 2022-10-02 13:55:59 +03:00
commit 9160b0ff43
Signed by: Maks1mS
GPG Key ID: 5945C22851757472
4 changed files with 199 additions and 0 deletions

105
client.go Normal file
View File

@ -0,0 +1,105 @@
package client
import (
"fmt"
"log"
"sync"
"github.com/reiver/go-telnet"
)
type Client struct {
t *telnet.Conn
mutex sync.Mutex
login string
password string
}
func (c *Client) readUntil(read bool, delims ...string) ([]byte, int, error) {
if len(delims) == 0 {
return nil, 0, nil
}
p := make([]string, len(delims))
for i, s := range delims {
if len(s) == 0 {
return nil, 0, nil
}
p[i] = s
}
var line []byte
b := make([]byte, 1)
for {
_, err := c.t.Read(b)
if err != nil {
return nil, 0, err
}
if read {
line = append(line, b...)
}
for i, s := range p {
if s[0] == b[0] {
if len(s) == 1 {
return line, i, nil
}
p[i] = s[1:]
} else {
p[i] = delims[i]
}
}
}
}
func (c *Client) ExecuteCommand(cmd string) []byte {
c.mutex.Lock()
c.t.Write([]byte(cmd + "\n"))
result, _ := c.ReadUntil(">")
c.mutex.Unlock()
return result
}
func (c *Client) ReadUntil(delims ...string) ([]byte, error) {
d, _, err := c.readUntil(true, delims...)
return d, err
}
func (c *Client) SkipUntil(delims ...string) error {
_, _, err := c.readUntil(false, delims...)
return err
}
func expect(c *Client, d ...string) {
c.SkipUntil(d...)
}
func (c *Client) Authorize() {
c.mutex.Lock()
expect(c, "Login:")
fmt.Println("l")
c.t.Write([]byte(c.login + "\n"))
expect(c, "Password:")
fmt.Println("p")
c.t.Write([]byte(c.password + "\n"))
expect(c, ">")
c.mutex.Unlock()
}
func New(dst string, login string, password string) (*Client, error) {
t, err := telnet.DialTo(dst)
if err != nil {
log.Fatal(err)
}
if err != nil {
return nil, err
}
return &Client{
t: t,
mutex: sync.Mutex{},
login: login,
password: password,
}, nil
}

7
go.mod Normal file
View File

@ -0,0 +1,7 @@
module git.slipenko.com/Maks1mS/go-keenetic
go 1.18
require github.com/reiver/go-telnet v0.0.0-20180421082511-9ff0b2ab096e
require github.com/reiver/go-oi v1.0.0 // indirect

4
go.sum Normal file
View File

@ -0,0 +1,4 @@
github.com/reiver/go-oi v1.0.0 h1:nvECWD7LF+vOs8leNGV/ww+F2iZKf3EYjYZ527turzM=
github.com/reiver/go-oi v1.0.0/go.mod h1:RrDBct90BAhoDTxB1fenZwfykqeGvhI6LsNfStJoEkI=
github.com/reiver/go-telnet v0.0.0-20180421082511-9ff0b2ab096e h1:quuzZLi72kkJjl+f5AQ93FMcadG19WkS7MO6TXFOSas=
github.com/reiver/go-telnet v0.0.0-20180421082511-9ff0b2ab096e/go.mod h1:+5vNVvEWwEIx86DB9Ke/+a5wBI464eDRo3eF0LcfpWg=

83
iproute.go Normal file
View File

@ -0,0 +1,83 @@
package client
import (
"errors"
"fmt"
"strings"
)
type IProute struct {
Destination string
Gateway string
Interface string
Metric string
}
func GetIpRoutesCmd() string {
return "show ip route"
}
func (c *Client) GetIpRoutes() []IProute {
raw := c.ExecuteCommand(GetIpRoutesCmd())
prefix := 305
postfix := 12
string_result := string(raw[prefix : len(raw)-postfix])
lines := strings.Split(string_result, "\r\n")
result := make([]IProute, len(lines))
for i, x := range lines {
l := strings.Fields(x)
result[i] = IProute{Destination: l[0], Gateway: l[1], Interface: l[2], Metric: l[3]}
}
return result
}
func AddIpRouteCmd(route IProute, auto bool) string {
autoString := "auto"
if !auto {
autoString = ""
}
return fmt.Sprintf("ip route %s %s %s %s %s", route.Destination, route.Gateway, route.Interface, route.Metric, autoString)
}
func (c *Client) AddIpRoute(route IProute, auto bool) error {
cmd := AddIpRouteCmd(route, auto)
raw := c.ExecuteCommand(cmd)
prefix := len(cmd)*4 + 7
postfix := 11
result := string(raw[prefix : len(raw)-postfix])
if strings.HasPrefix(result, "Network::RoutingTable: added static route") || strings.HasPrefix(result, "Network::RoutingTable: renewed static route") {
return nil
} else {
return errors.New(result)
}
}
func RemoveIpRouteCmd(route IProute) string {
return fmt.Sprintf("no ip route %s %s %s", route.Destination, route.Interface, route.Metric)
}
func (c *Client) RemoveIpRoute(route IProute) error {
cmd := RemoveIpRouteCmd(route)
raw := c.ExecuteCommand(cmd)
prefix := len(cmd)*4 + 7
postfix := 11
result := string(raw[prefix : len(raw)-postfix])
if strings.HasPrefix(result, "Network::RoutingTable: deleted static route:") {
return nil
} else {
return errors.New(result)
}
}