-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobench.go
85 lines (68 loc) · 2.35 KB
/
gobench.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"os/exec"
"sync"
"time"
)
// randomUsername selects a random username from a list
func randomUsername() string {
usernames := []string{"user1", "user2", "user3", "user4", "user5"}
rand.Seed(time.Now().UnixNano())
return usernames[rand.Intn(len(usernames))]
}
// runSSHCommand executes an SSH command using the system's SSH CLI
func runSSHCommand(username, host string, port int) error {
// The SSH command to execute (you might need to configure the SSH binary to avoid interactive prompts)
cmd := exec.Command("ssh", fmt.Sprintf("%s@%s", username, host), "-p", fmt.Sprintf("%d", port))
// Get the output from the command
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to run SSH command: %w | output: %s", err, output)
}
log.Printf("host: %s | port: %d", host, port)
return nil
}
func main() {
// Define command-line flags for externalizing the number of connections and concurrency level
numConnections := flag.Int("connections", 10, "Total number of SSH connections to establish")
maxConcurrent := flag.Int("concurrent", 3, "Maximum number of concurrent SSH connections")
host := flag.String("host", "example.com", "ssh host")
port := flag.Int("port", 80, "ssh port")
// Parse the flags
flag.Parse()
// Print the configuration for confirmation
log.Printf("Starting with %d total connections and %d concurrent connections", *numConnections, *maxConcurrent)
var wg sync.WaitGroup
// Limit the number of concurrent connections
semaphore := make(chan struct{}, *maxConcurrent)
// Counter for successful connections
var successfulConnections int
var mu sync.Mutex
for i := 0; i < *numConnections; i++ {
wg.Add(1)
go func() {
defer wg.Done()
// Limit concurrency
semaphore <- struct{}{}
defer func() { <-semaphore }()
// Random username for each connection
username := randomUsername()
// Run the SSH command using the system's SSH client
if err := runSSHCommand(username, *host, *port); err != nil {
log.Printf("Failed to connect as %s: %v", username, err)
} else {
mu.Lock()
successfulConnections++
mu.Unlock()
}
}()
}
// Wait for all connections to finish
wg.Wait()
// Output total number of successful connections
fmt.Printf("Total successful connections: %d/%d\n", successfulConnections, *numConnections)
}