Skip to content

Commit

Permalink
supported ssh options
Browse files Browse the repository at this point in the history
  • Loading branch information
shynome committed May 11, 2021
1 parent d98d14d commit cbb6acc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ dcip export [email protected] main_pg:5432 0.0.0.0:5432
# 可省略本地端口以及监听地址, 监听地址默认是 0.0.0.0, 本地端口默认为容器端口
dcip export [email protected] main_pg:5432
```

## 进阶使用

现在可以在 host 上传递 ssh options 了, 示例: `dcip of ' -J cn-host usa-host' pg`, 用法就是加上单引号并且使用空格开头
22 changes: 11 additions & 11 deletions cmd/dcip/dcip.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (

var CLI struct {
Of struct {
Host string `arg name:"[host|name]" passthrough help:"remote host optional or just a container name"`
Container string `arg name:"name" optional help:"container name"`
} `cmd help:"get container ip."`
Host string `arg:"" name:"[host|name]" help:"remote host optional or just a container name"`
Container string `arg:"" name:"name" optional:"" help:"container name"`
} `cmd:"" passthrough:"" help:"get container ip."`
Export struct {
Host string `arg name:"host" passthrough help:"ssh host. example: [email protected]"`
ContainerPort string `arg name:"cport" help:"remote container name and port. example: pg:5432"`
LocalAddr string `arg name:"lport" optional help:"local bind address and port. default bind address is 0.0.0.0, default port is remote container port. example: 127.0.0.1:5432 or 5432"`
} `cmd help:"export remote container port to local host."`
Debug bool `name:"debug" short:"D" optional`
Host string `arg:"" name:"host" help:"ssh host. example: [email protected]"`
ContainerPort string `arg:"" name:"cport" help:"remote container name and port. example: pg:5432"`
LocalAddr string `arg:"" name:"lport" optional:"" help:"local bind address and port. default bind address is 0.0.0.0, default port is remote container port. example: 127.0.0.1:5432 or 5432"`
} `cmd:"" passthrough:"" help:"export remote container port to local host."`
Debug bool `name:"debug" short:"D" optional:""`
Version kong.VersionFlag `short:"V"`
}

Expand Down Expand Up @@ -48,7 +48,7 @@ func main() {
if params.Host == "" {
cmd = RunCommand(cmdStr)
} else {
cmd = RunSSHCommand(params.Host, cmdStr)
cmd = RunSSHCommand(ParseHostOptions(params.Host), cmdStr)
}
PrintCmd(cmd)
result, err := cmd.CombinedOutput()
Expand Down Expand Up @@ -81,15 +81,15 @@ func main() {
lbind = localAddrArr[0]
lport = localAddrArr[1]
}
getIPCmd := RunSSHCommand(params.Host, dcip.MakeGetContainerIPCmd(container))
getIPCmd := RunSSHCommand(ParseHostOptions(params.Host), dcip.MakeGetContainerIPCmd(container))
PrintCmd(getIPCmd)
cipBytes, err := getIPCmd.CombinedOutput()
cip := strings.Replace(string(cipBytes), "\n", "", 1)
if err != nil {
reportError(err)
return
}
cmdStr := dcip.MakeForwardPortCmd(params.Host, string(cip)+":"+cport, lbind+":"+lport)
cmdStr := dcip.MakeForwardPortCmd(ParseHostOptions(params.Host), string(cip)+":"+cport, lbind+":"+lport)
cmd := RunSSHCommand(cmdStr, "")
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
Expand Down
24 changes: 13 additions & 11 deletions cmd/dcip/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,32 @@ package main
import (
"fmt"
"os/exec"
"strings"
)

func PrintCmd(cmd *exec.Cmd) {
if !CLI.Debug {
return
}
fmt.Print("+ ")
fmt.Println(cmd)
fmt.Printf("+ %+v \n", cmd)
}

func RunCommand(command string) *exec.Cmd {
cmd := exec.Command("bash", "-c", command)
return cmd
}

func RunSSHCommand(host interface{}, command string) *exec.Cmd {
func RunSSHCommand(host []string, command string) *exec.Cmd {
var cmd *exec.Cmd
switch host.(type) {
case string:
cmd = exec.Command("ssh", host.(string), command)
default:
params := host.([]string)
params = append(params, command)
cmd = exec.Command("ssh", params...)
}
params := host
params = append(params, command)
cmd = exec.Command("ssh", params...)
return cmd
}

func ParseHostOptions(host string) []string {
if !strings.HasPrefix(host, " ") {
return []string{host}
}
return strings.Split(host, " ")[1:]
}
7 changes: 4 additions & 3 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package dcip
import "fmt"

// ssh -NT -L 0.0.0.0:5432:172.17.0.5:5432 [email protected]
func MakeForwardPortCmd(host string, cport string, lport string) []string {
return []string{
func MakeForwardPortCmd(host []string, cport string, lport string) []string {
cmd := []string{
"-NT",
"-L", fmt.Sprintf("%s:%s", lport, cport),
host,
}
cmd = append(cmd, host...)
return cmd
}

0 comments on commit cbb6acc

Please sign in to comment.