Giter Site home page Giter Site logo

Add a "debug" flag about sweet HOT 1 OPEN

appliedtrust avatar appliedtrust commented on July 29, 2024
Add a "debug" flag

from sweet.

Comments (1)

dotwaffle avatar dotwaffle commented on July 29, 2024

FYI, the changes I've made so far are:

commit b586a919f0722dc17be075e10936f560e89446f7
Author: Matthew Walster <[email protected]>
Date:   Wed Jul 1 11:40:10 2015 +0000

    Reference Arista in the main collection routine

diff --git a/sweet.go b/sweet.go
index 1c8b385..60b4b5c 100644
--- a/sweet.go
+++ b/sweet.go
@@ -227,6 +227,8 @@ func collectDevice(device DeviceConfig, Opts *SweetOptions) DeviceStatus {
        c = newExternalCollector()
    } else if device.Method == "junos" {
        c = newJunOSCollector()
+   } else if device.Method == "arista" {
+       c = newAristaCollector()
    } else {
        status.State = StateError
        status.ErrorMessage = fmt.Sprintf("Unknown access method: %s", device.Method)

commit 04d8d03b548cc20b9255ef50ff1fdee1e32cfe26
Author: Matthew Walster <[email protected]>
Date:   Wed Jul 1 11:37:27 2015 +0000

    Initial import of Arista model

diff --git a/arista.go b/arista.go
new file mode 100644
index 0000000..e37b4f8
--- /dev/null
+++ b/arista.go
@@ -0,0 +1,65 @@
+package sweet
+
+import (
+   "fmt"
+   "strings"
+)
+
+type Arista struct {
+}
+
+func newAristaCollector() Collector {
+   return Arista{}
+}
+
+func (collector Arista) Collect(device DeviceConfig) (map[string]string, error) {
+   result := make(map[string]string)
+
+   c, err := newSSHCollector(device)
+   if err != nil {
+       return result, fmt.Errorf("Error connecting to %s: %s", device.Hostname, err.Error())
+   }
+
+   if err := expect("assword:", c.Receive); err != nil {
+       return result, fmt.Errorf("Missing password prompt: %s", err.Error())
+   }
+   c.Send <- device.Config["pass"] + "\n"
+   multi := []string{"#", ">", "assword:"}
+   m, err := expectMulti(multi, c.Receive)
+   if err != nil {
+       return result, fmt.Errorf("Invalid response to password: %s", err.Error())
+   }
+   if m == "assword:" {
+       return result, fmt.Errorf("Bad username or password.")
+   } else if m == ">" {
+       c.Send <- "enable\n"
+       if err := expect("assword:", c.Receive); err != nil {
+           return result, fmt.Errorf("Missing enable password prompt: %s", err.Error())
+       }
+       c.Send <- device.Config["enable"] + "\n"
+       if err := expect("#", c.Receive); err != nil {
+           return result, fmt.Errorf("Enable attempt failed: %s", err.Error())
+       }
+   }
+   c.Send <- "terminal length 0\n"
+   if err := expect("#", c.Receive); err != nil {
+       return result, fmt.Errorf("Command 'terminal length 0' failed: %s", err.Error())
+   }
+   c.Send <- "show running-config\n"
+   result["config"], err = expectSaveTimeout("#", c.Receive, device.CommandTimeout)
+   if err != nil {
+       return result, fmt.Errorf("Command 'show running-config' failed: %s", err.Error())
+   }
+   c.Send <- "show version\n"
+   result["version"], err = expectSaveTimeout("#", c.Receive, device.CommandTimeout)
+   if err != nil {
+       return result, fmt.Errorf("Command 'show version' failed: %s", err.Error())
+   }
+
+   // cleanup config results
+   result["config"] = strings.TrimSpace(strings.TrimPrefix(result["config"], "show running-config"))
+
+   c.Send <- "exit\n"
+
+   return result, nil
+}
diff --git a/arista_test.go b/arista_test.go
new file mode 100644
index 0000000..d571969
--- /dev/null
+++ b/arista_test.go
@@ -0,0 +1,40 @@
+package sweet
+
+import (
+   "os"
+   "strings"
+   "testing"
+   "time"
+)
+
+func TestAristaGood(t *testing.T) {
+   d := new(DeviceConfig)
+   d.Config = make(map[string]string)
+   d.Method = "Arista"
+   d.Timeout = 10 * time.Second
+
+   if os.Getenv("SWEET_TEST_HOST") == "" {
+       t.Error("Test requries SWEET_TEST_HOST environment variable")
+       return
+   }
+   if os.Getenv("SWEET_TEST_USER") == "" {
+       t.Error("Test requries SWEET_TEST_USER environment variable")
+       return
+   }
+   if os.Getenv("SWEET_TEST_PASS") == "" {
+       t.Error("Test requries SWEET_TEST_PASS environment variable")
+       return
+   }
+
+   d.Hostname = os.Getenv("SWEET_TEST_HOST")
+   d.Config["user"] = os.Getenv("SWEET_TEST_USER")
+   d.Config["pass"] = os.Getenv("SWEET_TEST_PASS")
+
+   d.Target = d.Hostname
+
+   s := CollectArista(*d)
+   if !strings.Contains(s["config"], "end") {
+       t.Errorf("Config missing end at the end")
+   }
+
+}

and to reference that in main.go (which I won't commit):

13:26:09 [mwalster@mwmbp:~/Code/gohome/src/github.com/dotwaffle/sweet] arista(+1/-1) ± git diff
diff --git a/cmd/main.go b/cmd/main.go
index 776b8e2..35fac5b 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -4,8 +4,8 @@ package main

 import (
        "errors"
-       "github.com/appliedtrust/sweet"
        "github.com/docopt/docopt-go"
+       "github.com/dotwaffle/sweet"
        "github.com/vaughan0/go-ini"
        "log/syslog"
        "os"

from sweet.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.