Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.79 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "log"
  6.     "os"
  7.     "time"
  8.  
  9.     "golang.org/x/crypto/ssh"
  10. )
  11.  
  12. const (
  13.     user = "root"
  14.     pass = "somepass"
  15.     ip   = "192.168.1.1:22"
  16. )
  17.  
  18. func main() {
  19.     config := &ssh.ClientConfig{
  20.         User: user,
  21.         Auth: []ssh.AuthMethod{
  22.             ssh.Password(pass),
  23.         },
  24.         Config: ssh.Config{
  25.             Ciphers: []string{"aes128-cbc"}, // insecure!!
  26.         },
  27.     }
  28.  
  29.     client, err := ssh.Dial("tcp", ip, config)
  30.     checkError(err, "ssh.Dial()")
  31.  
  32.     session, err := client.NewSession()
  33.     defer session.Close()
  34.     checkError(err, "client.NewSession()")
  35.  
  36.     modes := ssh.TerminalModes{
  37.         ssh.ECHO:          1,     // disable echoing
  38.         ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
  39.         ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
  40.     }
  41.  
  42.     if err = session.RequestPty("vt100", 80, 40, modes); err != nil {
  43.         log.Fatal(err)
  44.     }
  45.  
  46.     //session.Stdin = os.Stdin
  47.     in, _ := session.StdinPipe()
  48.     //session.Stdout = os.Stdout.
  49.     session.Stderr = os.Stderr
  50.  
  51.     if err = session.Shell(); err != nil {
  52.         log.Fatal(err)
  53.     }
  54.     fmt.Println("Backing up Faura1...")
  55.  
  56.     //fmt.Println(buff.String())
  57.     time.Sleep(time.Second * 2)
  58.  
  59.     in.Write([]byte("enable\n"))
  60.     time.Sleep(time.Second * 1)
  61.     //fmt.Println(buff.String()) // print the output
  62.  
  63.     in.Write([]byte("config\n"))
  64.     time.Sleep(time.Second * 1)
  65.  
  66.  
  67.     in.Write([]byte("backup data ftp 192.168.1.2 f1-olt.dat\n"))
  68.     time.Sleep(time.Second * 1)
  69.  
  70.     in.Write([]byte("y\n"))
  71.     time.Sleep(time.Second * 5)
  72.  
  73.     in.Write([]byte("backup configuration ftp 192.168.1.2 f1-olt.cfg\n"))
  74.  
  75.     time.Sleep(time.Second * 1)
  76.  
  77.     in.Write([]byte("y\n"))
  78.     time.Sleep(time.Second * 5)
  79.     os.Exit(1)
  80.  
  81.     err = session.Wait()
  82.     if err != nil {
  83.         panic(err)
  84.     }
  85.  
  86. }
  87.  
  88. func checkError(err error, info string) {
  89.     if err != nil {
  90.         fmt.Printf("%s. error: %s\n", info, err)
  91.         os.Exit(1)
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement