Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "os"
- "fmt"
- "flag"
- "golang.org/x/crypto/ssh"
- "io"
- "log"
- "strings"
- "time"
- "encoding/xml"
- "bufio"
- "net"
- )
- // SSH connection struct
- type SSHConnections struct {
- Host string
- User string
- Password string
- CLILogfile string
- SSHConn ssh.Client
- SSHConfig ssh.ClientConfig
- }
- func main() {
- //parse list of addresses, get usernames and passwords
- var ipaddr = []string{"a.b.c.d","e.f.g.h"} //hard-coded for now, will receive list from external source later
- //build out SSHConnections struct
- for i := 0; i < len(ipaddr); i++ {
- tempsshConfig := &ssh.ClientConfig{
- User: "administrator",
- Auth: []ssh.AuthMethod{
- ssh.Password("Password!"),
- },
- HostKeyCallback: ssh.InsecureIgnoreHostKey(),
- }
- tempsshConfig.Config.Ciphers = append(tempsshConfig.Config.Ciphers, "aes128-cbc")
- var newitem = SSHConnections{
- Host: ipaddr[i],
- User: "administrator",
- Password: "Password!",
- CLILogfile: ipaddr[1]+".txt",
- SSHConn: ssh.Client,
- SSHConfig: *tempsshConfig,
- }
- SSHConnections = append(SSHConnections, newitem)
- }
- type ssh.Client is not an expression
- type SSHConnections is not an expression
- //extra SSH parameters required before connecting
- sshConfig.Config.Ciphers = append(sshConfig.Config.Ciphers, "aes128-cbc")
- modes := ssh.TerminalModes{
- ssh.ECHO: 0, // disable echoing
- ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
- ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
- }
- //prepare logfiles
- f, ferr := os.OpenFile("outputfile.txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
- if ferr != nil {
- panic(ferr)
- }
- defer f.Close()
- //SSH connection procedure
- connection, err := ssh.Dial("tcp", hostname+":22", sshConfig)
- if err != nil {
- log.Fatalf("Failed to dial: %s", err)
- }
- session, err := connection.NewSession()
- handleError(err, true, "Failed to create session: %s")
- sshOut, err := session.StdoutPipe()
- handleError(err, true, "Unable to setup stdin for session: %v")
- sshIn, err := session.StdinPipe()
- handleError(err, true, "Unable to setup stdout for session: %v")
- if err := session.RequestPty("xterm", 0, 200, modes); err != nil {
- session.Close()
- handleError(err, true, "request for pseudo terminal failed: %s")
- }
- if err := session.Shell(); err != nil {
- session.Close()
- handleError(err, true, "request for shell failed: %s")
- }
- package main
- import (
- "os"
- "fmt"
- "flag"
- "golang.org/x/crypto/ssh"
- "io"
- "log"
- "strings"
- "time"
- "encoding/xml"
- "bufio"
- "net"
- )
- // SSH connection struct
- type SSHConnections struct {
- Host string
- User string
- Password string
- CLILogfilepath string
- CLILogfile os.File
- SSHConn *ssh.Client
- Session ssh.Session
- SSHOut io.Reader
- SSHIn io.WriteCloser
- SSHConfig *ssh.ClientConfig
- }
- func main() {
- var ipaddr = []string{"host1","host2"}
- //build out SSHConnections struct
- var connections []SSHConnections
- for i := 0; i < len(ipaddr); i++ {
- tempsshConfig := &ssh.ClientConfig{
- User: "administrator",
- Auth: []ssh.AuthMethod{
- ssh.Password("Password!"),
- },
- HostKeyCallback: ssh.InsecureIgnoreHostKey(),
- }
- tempsshConfig.Config.Ciphers = append(tempsshConfig.Config.Ciphers, "aes128-cbc")
- var newitem = SSHConnections{
- Host: ipaddr[i],
- User: "administrator",
- Password: "Password!",
- CLILogfilepath: ipaddr[1]+".txt",
- //SSHConn: ssh.Client,
- SSHConfig: *tempsshConfig,
- }
- connections = append(connections, newitem)
- }
- //establish connections, check each for relevant 'admin:' readiness
- for i := 0; i < len(connections); i++ {
- var connections[i].SSHConn, err := ssh.Dial("tcp", connections[i].Host+":22", connections[i].SSHConfig)
- if err != nil {
- log.Fatalf("Failed to dial: %s", err)
- }
- connections[i].Session, err = connections[i].SSHConn.NewSession()
- handleError(err, true, "Failed to create session: %s")
- connections[i].SSHOut, err = connections[i].Session.StdoutPipe()
- handleError(err, true, "Unable to setup stdin for session: %v")
- connections[i].SSHIn, err = connections[i].Session.StdinPipe()
- handleError(err, true, "Unable to setup stdout for session: %v")
- if err := connections[i].Session.RequestPty("xterm", 0, 200, modes); err != nil {
- connections[i].Session.Close()
- handleError(err, true, "request for pseudo terminal failed: %s")
- }
- if err := connections[i].Session.Shell(); err != nil {
- connections[i].Session.Close()
- handleError(err, true, "request for shell failed: %s")
- }
- //prepare logfiles
- connections[i].CLILogfile, ferr = os.OpenFile(connections[i].CLILogfilepath+".txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
- if ferr != nil {
- panic(ferr)
- }
- defer connections[i].CLILogfile.Close()
- writeToFile(connections[i].CLILogfile, ferr, "testing output to file")
- }
- }
- syntax error: unexpected ., expecting type
- var connections[i].SSHConn, err := ssh.Dial("tcp", connections[i].Host+":22", connections[i].SSHConfig)
- for i := 0; i < len(ipaddr); i++ {
- // ...
- var newitem = SSHConnections{
- // ...
- SSHConn: ssh.Client,
- // ...
- }
- SSHConnections = append(SSHConnections, newitem)
- }
- SSHConnections = append(SSHConnections, newitem)
- someSlice = append(someSlice, newitem)
- var connections []SSHConnections
- for i := 0; i < len(ipaddr); i++ {
- // ...
- var newitem = SSHConnections{
- // Everything that's there now except the SSHConn ...
- }
- connections = append(connections, newitem)
- }
- connections := make([]SSHConnections, len(ipaddr))
- for i := 0; i < len(ipaddr); i++ {
- // ...
- var newitem = SSHConnections{
- // Everything that's there now except the SSHConn ...
- }
- connections[i] = newitem
- }
Add Comment
Please, Sign In to add comment