Golang SSH Connection switch

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
/* Switch sshusername Cisco password CISCOIP domain name hupucrypto key generate Rsaip ssh time-out 30line vty 0 4transpor T input alllogin local*/package mainimport ("FMT" "io" "Log" "OS" "Strings" "Sync") Import ("Golang.org/x/crypto/ssh") func Main () {config: = &ssh. Clientconfig{user: "Cisco", Auth: []ssh. Authmethod{ssh. Password ("Cisco"),},config:ssh. Config{ciphers: []string{"AES128-CBC"},},}//config. config.ciphers = append (config. Config.ciphers, "AES128-CBC") clinet, err: = ssh. Dial ("TCP", "192.168.1.253:22", config) checkerror (err, "connection switch") session, Err: = Clinet. NewSession () Defer session. Close () checkerror (err, "create Shell") modes: = SSH. Terminalmodes{ssh. Echo:1,//disable ECHOINGSSH. tty_op_ispeed:14400,//input speed = 14.4kbaudssh. tty_op_ospeed:14400,//output Speed = 14.4kbaud}if ERR: = Session. Requestpty ("VT100", modes); Err! = Nil {log. Fatal (Err)}w, err: = Session. Stdinpipe () if err! = Nil {panic (err)}r, err: = Session. Stdoutpipe () if err! = Nil {panic (err)}e,ERR: = Session. Stderrpipe () if err! = Nil {panic (err)}in, out: = Muxshell (W, R, E) If err: = Session. Shell (); Err! = Nil {log. Fatal (Err)}<-out//ignore the shell Outputin <-' show arp ' in <-' show int status ' in <-' exit ' in <-' exit ' FM t.printf ("%s\n%s\n", <-out, <-out) _, _ = <-out, <-outsession. Wait ()}func checkerror (err error, info string) {if err! = Nil {fmt. Printf ("%s. Error:%s\n", info, err) os. Exit (1)}}func Muxshell (w io. Writer, R, E io. Reader) (chan<-string, <-chan string) {in: = Do (chan string, 3) Out: = Make (Chan string, 5) var wg sync. Waitgroupwg.add (1)//for the Shell Itselfgo func () {for cmd: = range in {WG. ADD (1) w.write ([]byte (cmd + "\ n")) WG. Wait ()}} () go func () {var (buf [$ * 1024]bytet int) for {n, err: = R.read (buf[t:]) if err! = Nil {fmt. Println (Err. Error ()) close (in) close (out) return}t + = Nresult: = String (Buf[:t]) if strings. Contains (Result, "Username:") | | Strings. Contains (Result, "Password:") | | Strings. Contains (Result, "#") {out <-StrinG (Buf[:t]) T = 0WG. Done ()}}} () return in, out}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.