-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
var runCmd = &cobra.Command{ | ||
Use: "run", | ||
Short: "opentelemetry-test-server", | ||
Long: "", | ||
} | ||
|
||
var ( | ||
host string | ||
user string | ||
password string | ||
port int | ||
script string | ||
) | ||
|
||
func init() { | ||
runCmd.PersistentFlags().StringVarP(&host, "host", "i", "", "host ip") | ||
runCmd.PersistentFlags().StringVarP(&user, "user", "u", "root", "remote user name") | ||
runCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "remote user password") | ||
runCmd.PersistentFlags().IntVarP(&port, "port", "P", 22, "remote user password") | ||
runCmd.PersistentFlags().StringVarP(&script, "script", "s", "", "bash shell script") | ||
runCmd.AddCommand(remoteExecShellCmd()) | ||
} | ||
|
||
func Execute() { | ||
if err := runCmd.Execute(); err != nil { | ||
fmt.Printf("cmd err: %s\n", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package cmd | ||
|
||
type config struct { | ||
host string | ||
user string | ||
password string | ||
port int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/practice/shell_extender/pkg/remote_command" | ||
"github.com/spf13/cobra" | ||
"io/ioutil" | ||
) | ||
|
||
func remoteExecShellCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "remoteExec", | ||
Short: "exec shell script for remote server", | ||
Long: "exec shell script for remote server", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cfg := &config{ | ||
host: host, | ||
user: user, | ||
password: password, | ||
port: port, | ||
} | ||
err := remote_command.RunRemoteNode(cfg.user, cfg.password, cfg.host, cfg.port, readFile(script)) | ||
return err | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func readFile(path string) string { | ||
// 读取文件内容 | ||
data, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
fmt.Println("read file error:", err) | ||
return "" | ||
} | ||
|
||
return string(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "github.com/practice/shell_extender/cmd" | ||
|
||
func main() { | ||
cmd.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
kubectl get pods |