Skip to content

Commit 63e9e4b

Browse files
authored
Update main.go
Update code and add Pty session to script to better interact with arista eos.
1 parent 0857c5a commit 63e9e4b

1 file changed

Lines changed: 51 additions & 25 deletions

File tree

arista/ssh_client/main.go

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,37 @@ import (
1212
func commands() map[string][]string {
1313
var count int
1414
var host string
15+
1516
fmt.Print("Number of hosts: ")
1617
fmt.Scanf("%d", &count)
18+
1719
hostCmds := make(map[string][]string)
18-
fmt.Printf("\nEnter host and ssh port being used. (Ex. x.x.x.x:22 or hostname:22)\n\n\n")
20+
21+
fmt.Printf("\nEnter host and ssh port being used. (Ex. x.x.x.x:22 or hostname:22)\n\n")
22+
1923
for i := 1; i <= count; i++ {
2024
fmt.Print("Enter host: ")
2125
fmt.Scanf("%s", &host)
2226
fmt.Println()
27+
2328
reader := bufio.NewReader(os.Stdin)
2429
fmt.Print("cmds: ")
2530
cmds, err := reader.ReadString('\n')
2631
if err != nil {
27-
log.Fatal("You screwed something up: ", err)
32+
log.Fatal(err)
2833
}
34+
2935
s := strings.Split(cmds, ",")
30-
fmt.Println()
3136
hostCmds[host] = s
3237
}
38+
3339
return hostCmds
3440
}
3541

3642
func main() {
37-
user := "arista"
38-
pass := "arista"
43+
user := "admin"
44+
pass := "admin"
45+
3946
hostData := commands()
4047

4148
interactiveAuth := ssh.KeyboardInteractive(
@@ -44,44 +51,63 @@ func main() {
4451
for i := range answers {
4552
answers[i] = pass
4653
}
47-
4854
return answers, nil
4955
},
5056
)
5157

52-
for host, commands := range hostData {
58+
for host, cmds := range hostData {
59+
fmt.Println("++++++++++++++++++++++++++++++++")
60+
fmt.Println("Connected to:", host)
61+
fmt.Println("++++++++++++++++++++++++++++++++")
62+
5363
config := &ssh.ClientConfig{
54-
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
5564
User: user,
5665
Auth: []ssh.AuthMethod{interactiveAuth},
66+
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
5767
}
58-
fmt.Println("++++++++++++++++++++++++++++++++")
59-
fmt.Println("Connected to: ", host)
60-
fmt.Println("++++++++++++++++++++++++++++++++")
68+
6169
conn, err := ssh.Dial("tcp", host, config)
6270
if err != nil {
63-
log.Fatal("Failed to dial: ", err)
71+
log.Fatal("Failed to dial:", err)
6472
}
6573
defer conn.Close()
74+
6675
sess, err := conn.NewSession()
6776
if err != nil {
68-
log.Fatal("Failed to create session: ", err)
77+
log.Fatal("Failed to create session:", err)
6978
}
7079
defer sess.Close()
71-
stdin, err := sess.StdinPipe()
72-
if err != nil {
73-
log.Fatal("Failed to connect to remote devices stdin: ", err)
80+
//
81+
// Updated section
82+
// EOS requires a PTY for CLI
83+
//
84+
modes := ssh.TerminalModes{
85+
ssh.ECHO: 0,
7486
}
75-
sess.Stdout = os.Stdout
76-
sess.Stderr = os.Stderr
77-
sess.Shell()
78-
fmt.Fprintf(stdin, "term len 0\n")
79-
for _, v := range commands {
80-
fmt.Fprintf(stdin, "%s\n", v)
87+
88+
if err := sess.RequestPty("vt100", 80, 40, modes); err != nil {
89+
log.Fatal("PTY request failed:", err)
8190
}
82-
stdin.Close()
83-
sess.Wait()
91+
92+
// Build buffer for commands
93+
var cmdBuffer strings.Builder
94+
cmdBuffer.WriteString("term len 0\n")
95+
96+
for _, v := range cmds {
97+
cmd := strings.TrimSpace(v)
98+
if cmd == "" {
99+
continue
100+
}
101+
cmdBuffer.WriteString(cmd + "\n")
102+
}
103+
104+
// Run commands and print output
105+
output, err := sess.CombinedOutput(cmdBuffer.String())
106+
if err != nil {
107+
log.Fatal(err)
108+
}
109+
110+
fmt.Print(string(output))
84111
}
85112
}
86113

87-

0 commit comments

Comments
 (0)