Skip to content

Commit

Permalink
feat: 新增支持登入远程节点的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
googs1025 committed Aug 4, 2023
1 parent 5de937d commit e9189db
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- BatchRunRemoteNodeFromConfigWithTimeout
4. 支持命令行执行远程运维动作
- 可以将项目二进制编译更方便使用
5. 支持命令行登入远程节点
```yaml
remoteNodes:
- host: 127.0.0.1
Expand Down Expand Up @@ -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=<password> --host=<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 ~]#
```
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
18 changes: 18 additions & 0 deletions cmd/remote_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions pkg/remote_command/remote_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/practice/shell_extender/pkg/waitgroup_timeout"
"golang.org/x/crypto/ssh"
"net"
"os"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -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())
}
}

0 comments on commit e9189db

Please sign in to comment.