|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + |
| 9 | + "github.com/fatih/color" |
| 10 | + "github.com/songrgg/llsh/pkg/helper" |
| 11 | + "github.com/songrgg/llsh/pkg/ssh" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +type options struct { |
| 16 | + Command string |
| 17 | + Hosts string |
| 18 | + User string |
| 19 | + UseSSHCommand bool |
| 20 | + UsePassword bool |
| 21 | + PublicKeyPath string |
| 22 | + SSHCommandPath string |
| 23 | +} |
| 24 | + |
| 25 | +type sshResult struct { |
| 26 | + Host string |
| 27 | + Result string |
| 28 | + Error error |
| 29 | +} |
| 30 | + |
| 31 | +// NewLLSHCommand creates `llsh` command. |
| 32 | +func NewLLSHCommand() *cobra.Command { |
| 33 | + options := options{} |
| 34 | + |
| 35 | + cmds := &cobra.Command{ |
| 36 | + Use: "llsh", |
| 37 | + Short: "llsh executes the remote shell command on multiple remote servers", |
| 38 | + Run: func(cmd *cobra.Command, args []string) { |
| 39 | + options.validate() |
| 40 | + options.run() |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + cmds.PersistentFlags().StringVar(&options.Hosts, "hosts", "", |
| 45 | + "The host names for remote server, split by comma, for example, host1,host2,host3") |
| 46 | + cmds.PersistentFlags().StringVarP(&options.User, "user", "u", "", |
| 47 | + "The username for login user") |
| 48 | + cmds.PersistentFlags().StringVarP(&options.Command, "command", "c", "", |
| 49 | + "The command you need to run on the servers") |
| 50 | + cmds.PersistentFlags().BoolVar(&options.UseSSHCommand, "use_ssh_command", true, |
| 51 | + "Use the ssh executable, like /bin/ssh") |
| 52 | + cmds.PersistentFlags().BoolVarP(&options.UsePassword, "password", "p", false, |
| 53 | + "If we need to input password from terminal") |
| 54 | + cmds.PersistentFlags().StringVarP(&options.PublicKeyPath, "publickey", "k", "", |
| 55 | + "The file path of the public key, like ~/.ssh/id_rsa.pub") |
| 56 | + cmds.PersistentFlags().StringVar(&options.SSHCommandPath, "ssh_command_path", "ssh", |
| 57 | + "The file path of the ssh command, like /bin/ssh") |
| 58 | + return cmds |
| 59 | +} |
| 60 | + |
| 61 | +func (o *options) authMethods() []ssh.AuthMethod { |
| 62 | + authMethods := make([]ssh.AuthMethod, 0) |
| 63 | + if o.UsePassword && !o.UseSSHCommand { |
| 64 | + password, err := helper.ReadPassword("Password: ") |
| 65 | + if err != nil { |
| 66 | + helper.Errorlq("password can't be read") |
| 67 | + } |
| 68 | + |
| 69 | + authMethods = append(authMethods, ssh.AuthMethod{ |
| 70 | + Type: ssh.AuthByPassword, |
| 71 | + Content: password, |
| 72 | + }) |
| 73 | + } |
| 74 | + |
| 75 | + if o.PublicKeyPath != "" && !o.UseSSHCommand { |
| 76 | + content, err := ioutil.ReadFile(o.PublicKeyPath) |
| 77 | + if err != nil { |
| 78 | + helper.Errorlq("couldn't read the content of public key: ", err) |
| 79 | + } |
| 80 | + |
| 81 | + authMethods = append(authMethods, ssh.AuthMethod{ |
| 82 | + Type: ssh.AuthByPublicKey, |
| 83 | + Content: string(content), |
| 84 | + }) |
| 85 | + } |
| 86 | + return authMethods |
| 87 | +} |
| 88 | + |
| 89 | +func (o *options) run() { |
| 90 | + authMethods := o.authMethods() |
| 91 | + |
| 92 | + hosts := strings.Split(o.Hosts, ",") |
| 93 | + var wg sync.WaitGroup |
| 94 | + var results []sshResult |
| 95 | + var lock = sync.Mutex{} |
| 96 | + for _, host := range hosts { |
| 97 | + wg.Add(1) |
| 98 | + go func(host string) { |
| 99 | + t := &ssh.Task{ |
| 100 | + Username: o.User, |
| 101 | + AuthMethods: authMethods, |
| 102 | + Host: host, |
| 103 | + Script: o.Command, |
| 104 | + UseSSHCommand: o.UseSSHCommand, |
| 105 | + SSHCommandPath: o.SSHCommandPath, |
| 106 | + } |
| 107 | + |
| 108 | + result, err := t.Execute() |
| 109 | + if err != nil { |
| 110 | + result = fmt.Sprintf("fail to execute task: %s", result) |
| 111 | + } |
| 112 | + |
| 113 | + lock.Lock() |
| 114 | + results = append(results, sshResult{ |
| 115 | + Host: host, |
| 116 | + Result: result, |
| 117 | + Error: err, |
| 118 | + }) |
| 119 | + lock.Unlock() |
| 120 | + wg.Done() |
| 121 | + }(host) |
| 122 | + } |
| 123 | + wg.Wait() |
| 124 | + |
| 125 | + for _, result := range results { |
| 126 | + if result.Error != nil { |
| 127 | + color.Red("Host %s\n", result.Host) |
| 128 | + } else { |
| 129 | + color.Green("Host %s\n", result.Host) |
| 130 | + } |
| 131 | + |
| 132 | + _, _ = fmt.Println(result.Result) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func (o *options) validate() { |
| 137 | + if o.Hosts == "" { |
| 138 | + helper.Errorlq("hosts can't be empty") |
| 139 | + } |
| 140 | + |
| 141 | + if o.PublicKeyPath != "" && o.UsePassword { |
| 142 | + helper.Errorlq("can't use both public key and password") |
| 143 | + } |
| 144 | + |
| 145 | + if (o.PublicKeyPath != "" || o.UsePassword) && o.UseSSHCommand { |
| 146 | + color.Yellow("Public key or password option will be ignored when using ssh command") |
| 147 | + } |
| 148 | +} |
0 commit comments