|
| 1 | +package foozler |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "golang.org/x/crypto/ssh" |
| 7 | + "io" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +// Debugee represents a Cisco switch that runs l2t. In this case - as a |
| 13 | +// a debug target. Upon connection, the target switch will be configured |
| 14 | +// to produce l2t debug output. |
| 15 | +type Debugee struct { |
| 16 | + session *ssh.Session |
| 17 | + stdin io.WriteCloser |
| 18 | + enable chan bool |
| 19 | + out chan string |
| 20 | + onDeath chan error |
| 21 | + stop chan chan struct{} |
| 22 | +} |
| 23 | + |
| 24 | +// Enable enables output from the switch. |
| 25 | +func (o *Debugee) Enable() { |
| 26 | + o.enable <- true |
| 27 | +} |
| 28 | + |
| 29 | +// Disable disables output from the switch. |
| 30 | +func (o *Debugee) Disable() { |
| 31 | + o.enable <- false |
| 32 | +} |
| 33 | + |
| 34 | +// Wait returns a channel that receives nil, or an error when the SSH |
| 35 | +// session ends. |
| 36 | +func (o *Debugee) Wait() <-chan error { |
| 37 | + return o.onDeath |
| 38 | +} |
| 39 | + |
| 40 | +// Output returns a channel that receives debug output from a switch |
| 41 | +// when output is enabled. |
| 42 | +func (o *Debugee) Output() <-chan string { |
| 43 | + return o.out |
| 44 | +} |
| 45 | + |
| 46 | +// Close closes the SSH session. |
| 47 | +func (o *Debugee) Close() { |
| 48 | + rejoin := make(chan struct{}) |
| 49 | + o.stop <- rejoin |
| 50 | + <-rejoin |
| 51 | +} |
| 52 | + |
| 53 | +// Execute executes a command on the switch. |
| 54 | +func (o *Debugee) Execute(command string) error { |
| 55 | + _, err := io.WriteString(o.stdin, fmt.Sprintf("%s\r\n", command)) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +// ConnectTo connects to a Cisco switch via SSH to facilitate debugging. |
| 64 | +func ConnectTo(address string, port int, clientConfig *ssh.ClientConfig) (*Debugee, error) { |
| 65 | + client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", address, port), clientConfig) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + session, err := client.NewSession() |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + sshIn, err := session.StdinPipe() |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + sshOut, err := session.StdoutPipe() |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + sshErr, err := session.StderrPipe() |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + onSessionDeath := make(chan error, 2) |
| 91 | + rawOutput := make(chan []byte) |
| 92 | + go func() { |
| 93 | + scanner := bufio.NewScanner(io.MultiReader(sshOut, sshErr)) |
| 94 | + |
| 95 | + for scanner.Scan() { |
| 96 | + rawOutput <- scanner.Bytes() |
| 97 | + } |
| 98 | + |
| 99 | + err := scanner.Err() |
| 100 | + if err != nil { |
| 101 | + onSessionDeath <- fmt.Errorf("stderr/stdout scanner exited - %s", err.Error()) |
| 102 | + } |
| 103 | + }() |
| 104 | + |
| 105 | + d := &Debugee{ |
| 106 | + session: session, |
| 107 | + stdin: sshIn, |
| 108 | + enable: make(chan bool), |
| 109 | + out: make(chan string, 1), |
| 110 | + onDeath: onSessionDeath, |
| 111 | + stop: make(chan chan struct{}), |
| 112 | + } |
| 113 | + |
| 114 | + go func() { |
| 115 | + discardIntialLines := 9 |
| 116 | + discardTimeout := time.NewTimer(5 * time.Second) |
| 117 | + keepAliveTicker := time.NewTicker(10 * time.Minute) |
| 118 | + |
| 119 | + isEnabled := false |
| 120 | + for { |
| 121 | + select { |
| 122 | + case <-keepAliveTicker.C: |
| 123 | + d.Execute("show clock") |
| 124 | + case isEnabled = <-d.enable: |
| 125 | + case <-discardTimeout.C: |
| 126 | + discardIntialLines = 0 |
| 127 | + case raw := <-rawOutput: |
| 128 | + if discardIntialLines > 0 { |
| 129 | + discardIntialLines-- |
| 130 | + continue |
| 131 | + } |
| 132 | + if isEnabled { |
| 133 | + d.out <- removeTimestamp(string(raw)) |
| 134 | + } |
| 135 | + case rejoin := <-d.stop: |
| 136 | + client.Close() |
| 137 | + keepAliveTicker.Stop() |
| 138 | + rejoin <- struct{}{} |
| 139 | + return |
| 140 | + } |
| 141 | + } |
| 142 | + }() |
| 143 | + |
| 144 | + err = session.Shell() |
| 145 | + if err != nil { |
| 146 | + return nil, err |
| 147 | + } |
| 148 | + |
| 149 | + err = d.Execute("no debug all") |
| 150 | + if err != nil { |
| 151 | + return nil, err |
| 152 | + } |
| 153 | + |
| 154 | + err = d.Execute("debug l2trace") |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + |
| 159 | + err = d.Execute("terminal monitor") |
| 160 | + if err != nil { |
| 161 | + return nil, err |
| 162 | + } |
| 163 | + |
| 164 | + go func() { |
| 165 | + onSessionDeath <- session.Wait() |
| 166 | + }() |
| 167 | + |
| 168 | + return d, nil |
| 169 | +} |
| 170 | + |
| 171 | +func removeTimestamp(s string) string { |
| 172 | + if strings.Count(s, ":") < 3 { |
| 173 | + return s |
| 174 | + } |
| 175 | + |
| 176 | + firstSpace := strings.Index(s, " ") |
| 177 | + if firstSpace < 0 || firstSpace == len(s)-1 { |
| 178 | + return s |
| 179 | + } |
| 180 | + |
| 181 | + secondSpace := strings.Index(s, " ") |
| 182 | + if secondSpace < 0 || secondSpace == len(s)-1 || secondSpace - firstSpace > 2 { |
| 183 | + return s |
| 184 | + } |
| 185 | + |
| 186 | + end := strings.Index(s, ": ") |
| 187 | + if end - secondSpace < 10 || end - secondSpace > 20 { |
| 188 | + return s |
| 189 | + } |
| 190 | + |
| 191 | + if end + 2 > len(s) { |
| 192 | + return s[end+1:] |
| 193 | + } |
| 194 | + |
| 195 | + return s[end+2:] |
| 196 | +} |
0 commit comments