package main import ( "bufio" "fmt" "net" "os" "strings" ) func main() { conn, err := net.Dial("tcp", "10.71.20.161:23") if err != nil { fmt.Sprint(os.Stderr, "Error: %s", err.Error()) return } var buf [4096]byte // for { n, err := conn.Read(buf[0:]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } fmt.Println(string(buf[0:n])) fmt.Println((buf[0:n])) buf[1] = 252 buf[4] = 252 buf[7] = 252 buf[10] = 252 fmt.Println((buf[0:n])) n, err = conn.Write(buf[0:n]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) return } n, err = conn.Read(buf[0:]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } fmt.Println(string(buf[0:n])) fmt.Println((buf[0:n])) buf[1] = 252 buf[4] = 251 buf[7] = 252 buf[10] = 254 buf[13] = 252 fmt.Println((buf[0:n])) n, err = conn.Write(buf[0:n]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) return } n, err = conn.Read(buf[0:]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } fmt.Println(string(buf[0:n])) fmt.Println((buf[0:n])) buf[1] = 252 buf[4] = 252 fmt.Println((buf[0:n])) n, err = conn.Write(buf[0:n]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) return } n, err = conn.Read(buf[0:]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } fmt.Println(string(buf[0:n])) fmt.Println((buf[0:n])) /* buf[0] = 255 buf[1] = 252 buf[2] = 1 buf[3] = 255 buf[4] = 253 buf[5] = 1 buf[6] = 255 buf[7] = 252 buf[8] = 1 buf[9] = 255 buf[10] = 253 buf[11] = 1 fmt.Println((buf[0:12])) n, err = conn.Write(buf[0:12]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) return } n, err = conn.Read(buf[0:]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } */ n, err = conn.Write([]byte("admin\n")) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) return } n, err = conn.Read(buf[0:]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } fmt.Println(string(buf[0:n])) n, err = conn.Write([]byte("\n")) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error()) return } n, err = conn.Read(buf[0:]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } fmt.Println(string(buf[0:n])) for { n, err = conn.Read(buf[0:]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } fmt.Println(string(buf[0:n])) if strings.HasSuffix(string(buf[0:n]), "> ") { break } } n, err = conn.Write([]byte("enable\n")) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } n, err = conn.Read(buf[0:]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } fmt.Println(string(buf[0:n])) n, err = conn.Write([]byte("terminal length 0\n")) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } n, err = conn.Read(buf[0:]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } fmt.Println(string(buf[0:n])) n, err = conn.Write([]byte("show port\n")) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } for { n, err = conn.Read(buf[0:]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } fmt.Println(string(buf[0:n])) if strings.HasSuffix(string(buf[0:n]), "# ") { break } } n, err = conn.Write([]byte("show interface\n\n")) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } for { n, err = conn.Read(buf[0:]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } fmt.Println(string(buf[0:n])) if strings.HasSuffix(string(buf[0:n]), "# ") { break } } n, err = conn.Write([]byte("show running-config\n")) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } reader := bufio.NewReader(conn) if reader == nil { fmt.Fprintf(os.Stderr, "Create reader failed.") } for { n, err := reader.Read(buf[0:]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } fmt.Println(string(buf[0:n])) if strings.HasSuffix(string(buf[0:n]), "# ") { break } } /* for { n, err = conn.Read(buf[0:]) if err != nil { fmt.Fprintf(os.Stderr, "Error: %s", err.Error()) return } fmt.Println(string(buf[0:n])) fmt.Println(n) if strings.HasSuffix(string(buf[0:n]), "# ") { break } } */ // } }
有疑问加站长微信联系(非本文作者)