Guest User

Untitled

a guest
Jan 25th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "os"
  5. "fmt"
  6. "flag"
  7. "golang.org/x/crypto/ssh"
  8. "io"
  9. "log"
  10. "strings"
  11. "time"
  12. "encoding/xml"
  13. "bufio"
  14. "net"
  15. )
  16.  
  17. // SSH connection struct
  18. type SSHConnections struct {
  19. Host string
  20. User string
  21. Password string
  22. CLILogfile string
  23. SSHConn ssh.Client
  24. SSHConfig ssh.ClientConfig
  25. }
  26.  
  27. func main() {
  28. //parse list of addresses, get usernames and passwords
  29. var ipaddr = []string{"a.b.c.d","e.f.g.h"} //hard-coded for now, will receive list from external source later
  30.  
  31. //build out SSHConnections struct
  32.  
  33. for i := 0; i < len(ipaddr); i++ {
  34. tempsshConfig := &ssh.ClientConfig{
  35. User: "administrator",
  36. Auth: []ssh.AuthMethod{
  37. ssh.Password("Password!"),
  38. },
  39. HostKeyCallback: ssh.InsecureIgnoreHostKey(), //FUTURECHECK allows connections to all machines, reconsider for security's sake
  40. }
  41. tempsshConfig.Config.Ciphers = append(tempsshConfig.Config.Ciphers, "aes128-cbc")
  42. var newitem = SSHConnections{
  43. Host: ipaddr[i],
  44. User: "administrator",
  45. Password: "Password!",
  46. CLILogfile: ipaddr[1]+".txt",
  47. SSHConn: ssh.Client,
  48. SSHConfig: *tempsshConfig,
  49. }
  50.  
  51. SSHConnections = append(SSHConnections, newitem)
  52. }
  53.  
  54. type ssh.Client is not an expression
  55. type SSHConnections is not an expression
  56.  
  57. //extra SSH parameters required before connecting
  58. sshConfig.Config.Ciphers = append(sshConfig.Config.Ciphers, "aes128-cbc")
  59. modes := ssh.TerminalModes{
  60. ssh.ECHO: 0, // disable echoing
  61. ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
  62. ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
  63. }
  64.  
  65. //prepare logfiles
  66. f, ferr := os.OpenFile("outputfile.txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
  67. if ferr != nil {
  68. panic(ferr)
  69. }
  70. defer f.Close()
  71.  
  72. //SSH connection procedure
  73. connection, err := ssh.Dial("tcp", hostname+":22", sshConfig)
  74. if err != nil {
  75. log.Fatalf("Failed to dial: %s", err)
  76. }
  77. session, err := connection.NewSession()
  78. handleError(err, true, "Failed to create session: %s")
  79. sshOut, err := session.StdoutPipe()
  80. handleError(err, true, "Unable to setup stdin for session: %v")
  81. sshIn, err := session.StdinPipe()
  82. handleError(err, true, "Unable to setup stdout for session: %v")
  83. if err := session.RequestPty("xterm", 0, 200, modes); err != nil {
  84. session.Close()
  85. handleError(err, true, "request for pseudo terminal failed: %s")
  86. }
  87. if err := session.Shell(); err != nil {
  88. session.Close()
  89. handleError(err, true, "request for shell failed: %s")
  90. }
Add Comment
Please, Sign In to add comment