SHARE
TWEET

Untitled

a guest Nov 12th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "flag"
  6.     "fmt"
  7.     "log"
  8.     "net"
  9.     "os"
  10.  
  11.     "golang.org/x/crypto/ssh"
  12. )
  13.  
  14. var (
  15.     user = flag.String("user", "iu9_32_09", "User name")
  16.     pass = flag.String("pass", "bydfkbl5151", "Password for login")
  17.     port = flag.Int("port", 22, "Port")
  18.     host = flag.String("host", "185.20.227.83", "Host")
  19. )
  20.  
  21. func main() {
  22.  
  23.     flag.Parse()
  24.  
  25.     config := &ssh.ClientConfig{
  26.         User: *user,
  27.         Auth: []ssh.AuthMethod{
  28.             ssh.Password(*pass)},
  29.         HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
  30.             return nil
  31.         }}
  32.  
  33.     addr := fmt.Sprintf("%s:%d", *host, *port)
  34.  
  35.     // звоним на сервер
  36.     client, err := ssh.Dial("tcp", addr, config)
  37.     if err != nil {
  38.         log.Fatal(err)
  39.     }
  40.  
  41.     // создаём ssh сессию
  42.     session, err := client.NewSession()
  43.     if err != nil {
  44.         log.Fatal(err)
  45.     }
  46.     // не забываем закрыть перед выходом
  47.     defer session.Close()
  48.  
  49.     stdin, err := session.StdinPipe()
  50.     if err != nil {
  51.         log.Fatal(err)
  52.     }
  53.  
  54.     session.Stdout = os.Stdout
  55.     session.Stderr = os.Stderr
  56.  
  57.     err = session.Shell()
  58.     if err != nil {
  59.         log.Fatal(err)
  60.     }
  61.  
  62.     scanner := bufio.NewScanner(os.Stdin)
  63.     for scanner.Scan() {
  64.         s := scanner.Text()
  65.         _, err = fmt.Fprintf(stdin, "%s\n", s)
  66.         if err != nil {
  67.             log.Fatal(err)
  68.         }
  69.         if s == "exit" {
  70.             break
  71.         }
  72.     }
  73.  
  74.     err = session.Wait()
  75.     if err != nil {
  76.         log.Fatal(err)
  77.     }
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top