From e9189db59d1832fe990c9c4265859cc41d954604 Mon Sep 17 00:00:00 2001 From: googs1025 Date: Fri, 4 Aug 2023 22:02:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=99=BB=E5=85=A5=E8=BF=9C=E7=A8=8B=E8=8A=82=E7=82=B9=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++++ cmd/cmd.go | 2 +- cmd/remote_cmd.go | 18 +++++++++++++ pkg/remote_command/remote_command.go | 40 ++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ba2b240..ad5b754 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ - BatchRunRemoteNodeFromConfigWithTimeout 4. 支持命令行执行远程运维动作 - 可以将项目二进制编译更方便使用 +5. 支持命令行登入远程节点 ```yaml remoteNodes: - host: 127.0.0.1 @@ -114,4 +115,9 @@ nfscsi-nginx-768ff5bf55-jjfwx 1/1 Running 0 56d ss-web-0 1/1 Running 0 56d ss-web-1 1/1 Running 1 289d +➜ shell_extender git:(main) ✗ go run main.go remoteCommandLine --user=root --password= --host= --port=22 +Last failed login: Fri Aug 4 21:59:12 CST 2023 from on ssh:notty +There were 11 failed login attempts since the last successful login. +Last login: Fri Aug 4 21:57:08 2023 from 101.207.203.124 +[root@VM-0-16-centos ~]# ``` \ No newline at end of file diff --git a/cmd/cmd.go b/cmd/cmd.go index 54b0ca9..eedd934 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -26,7 +26,7 @@ func init() { 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(), remoteExecPingCmd()) + runCmd.AddCommand(remoteExecShellCmd(), remoteExecPingCmd(), remoteCommandLineCmd()) } func Execute() { diff --git a/cmd/remote_cmd.go b/cmd/remote_cmd.go index 4a5cfac..63e834a 100644 --- a/cmd/remote_cmd.go +++ b/cmd/remote_cmd.go @@ -26,6 +26,24 @@ func remoteExecShellCmd() *cobra.Command { return cmd } +func remoteCommandLineCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "remoteCommandLine", + Short: "exec command line for remote server", + Long: "exec command line for remote server", + Run: func(cmd *cobra.Command, args []string) { + cfg := &config{ + host: host, + user: user, + password: password, + port: port, + } + remote_command.RunRemoteCommandLine(cfg.host, cfg.port, cfg.user, cfg.password) + }, + } + return cmd +} + func readFile(path string) string { // 读取文件内容 data, err := ioutil.ReadFile(path) diff --git a/pkg/remote_command/remote_command.go b/pkg/remote_command/remote_command.go index 7f0f388..883f016 100644 --- a/pkg/remote_command/remote_command.go +++ b/pkg/remote_command/remote_command.go @@ -7,6 +7,7 @@ import ( "github.com/practice/shell_extender/pkg/waitgroup_timeout" "golang.org/x/crypto/ssh" "net" + "os" "strconv" "sync" "time" @@ -176,3 +177,42 @@ func runRemoteNode(user, password, host string, port int, cmd string) error { return nil } + +// RunRemoteCommandLine 登入远程节点命令行 +func RunRemoteCommandLine(ip string, port int, user, password string) { + addr := fmt.Sprintf("%s:%s", ip, strconv.Itoa(port)) + // 建立SSH客户端连接 + client, err := ssh.Dial("tcp", addr, &ssh.ClientConfig{ + User: user, + Auth: []ssh.AuthMethod{ssh.Password(password)}, + HostKeyCallback: ssh.InsecureIgnoreHostKey(), + }) + if err != nil { + fmt.Printf("SSH dial error: %s", err.Error()) + } + + // 建立新会话 + session, err := client.NewSession() + defer session.Close() + if err != nil { + fmt.Printf("new session error: %s", err.Error()) + } + + session.Stdout = os.Stdout // 会话输出关联到系统标准输出设备 + session.Stderr = os.Stderr // 会话错误输出关联到系统标准错误输出设备 + session.Stdin = os.Stdin // 会话输入关联到系统标准输入设备 + modes := ssh.TerminalModes{ + ssh.ECHO: 0, // 禁用回显(0禁用,1启动) + ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud + ssh.TTY_OP_OSPEED: 14400, //output speed = 14.4kbaud + } + if err = session.RequestPty("linux", 32, 160, modes); err != nil { + fmt.Printf("request pty error: %s", err.Error()) + } + if err = session.Shell(); err != nil { + fmt.Printf("start shell error: %s", err.Error()) + } + if err = session.Wait(); err != nil { + fmt.Printf("return error: %s", err.Error()) + } +}