Guest User

Untitled

a guest
Jan 26th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 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(),
  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. }
  91.  
  92. package main
  93.  
  94. import (
  95. "os"
  96. "fmt"
  97. "flag"
  98. "golang.org/x/crypto/ssh"
  99. "io"
  100. "log"
  101. "strings"
  102. "time"
  103. "encoding/xml"
  104. "bufio"
  105. "net"
  106. )
  107.  
  108. // SSH connection struct
  109. type SSHConnections struct {
  110. Host string
  111. User string
  112. Password string
  113. CLILogfilepath string
  114. CLILogfile os.File
  115. SSHConn *ssh.Client
  116. Session ssh.Session
  117. SSHOut io.Reader
  118. SSHIn io.WriteCloser
  119. SSHConfig *ssh.ClientConfig
  120. }
  121.  
  122. func main() {
  123. var ipaddr = []string{"host1","host2"}
  124. //build out SSHConnections struct
  125. var connections []SSHConnections
  126. for i := 0; i < len(ipaddr); i++ {
  127. tempsshConfig := &ssh.ClientConfig{
  128. User: "administrator",
  129. Auth: []ssh.AuthMethod{
  130. ssh.Password("Password!"),
  131. },
  132. HostKeyCallback: ssh.InsecureIgnoreHostKey(),
  133. }
  134. tempsshConfig.Config.Ciphers = append(tempsshConfig.Config.Ciphers, "aes128-cbc")
  135. var newitem = SSHConnections{
  136. Host: ipaddr[i],
  137. User: "administrator",
  138. Password: "Password!",
  139. CLILogfilepath: ipaddr[1]+".txt",
  140. //SSHConn: ssh.Client,
  141. SSHConfig: *tempsshConfig,
  142. }
  143.  
  144. connections = append(connections, newitem)
  145. }
  146.  
  147. //establish connections, check each for relevant 'admin:' readiness
  148. for i := 0; i < len(connections); i++ {
  149. var connections[i].SSHConn, err := ssh.Dial("tcp", connections[i].Host+":22", connections[i].SSHConfig)
  150. if err != nil {
  151. log.Fatalf("Failed to dial: %s", err)
  152. }
  153. connections[i].Session, err = connections[i].SSHConn.NewSession()
  154. handleError(err, true, "Failed to create session: %s")
  155. connections[i].SSHOut, err = connections[i].Session.StdoutPipe()
  156. handleError(err, true, "Unable to setup stdin for session: %v")
  157. connections[i].SSHIn, err = connections[i].Session.StdinPipe()
  158. handleError(err, true, "Unable to setup stdout for session: %v")
  159. if err := connections[i].Session.RequestPty("xterm", 0, 200, modes); err != nil {
  160. connections[i].Session.Close()
  161. handleError(err, true, "request for pseudo terminal failed: %s")
  162. }
  163. if err := connections[i].Session.Shell(); err != nil {
  164. connections[i].Session.Close()
  165. handleError(err, true, "request for shell failed: %s")
  166. }
  167. //prepare logfiles
  168. connections[i].CLILogfile, ferr = os.OpenFile(connections[i].CLILogfilepath+".txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
  169. if ferr != nil {
  170. panic(ferr)
  171. }
  172. defer connections[i].CLILogfile.Close()
  173. writeToFile(connections[i].CLILogfile, ferr, "testing output to file")
  174. }
  175. }
  176.  
  177. syntax error: unexpected ., expecting type
  178.  
  179. var connections[i].SSHConn, err := ssh.Dial("tcp", connections[i].Host+":22", connections[i].SSHConfig)
  180.  
  181. for i := 0; i < len(ipaddr); i++ {
  182. // ...
  183. var newitem = SSHConnections{
  184. // ...
  185. SSHConn: ssh.Client,
  186. // ...
  187. }
  188.  
  189. SSHConnections = append(SSHConnections, newitem)
  190. }
  191.  
  192. SSHConnections = append(SSHConnections, newitem)
  193.  
  194. someSlice = append(someSlice, newitem)
  195.  
  196. var connections []SSHConnections
  197. for i := 0; i < len(ipaddr); i++ {
  198. // ...
  199. var newitem = SSHConnections{
  200. // Everything that's there now except the SSHConn ...
  201. }
  202. connections = append(connections, newitem)
  203. }
  204.  
  205. connections := make([]SSHConnections, len(ipaddr))
  206. for i := 0; i < len(ipaddr); i++ {
  207. // ...
  208. var newitem = SSHConnections{
  209. // Everything that's there now except the SSHConn ...
  210. }
  211. connections[i] = newitem
  212. }
Add Comment
Please, Sign In to add comment