config.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/user"
  7. "path"
  8. "regexp"
  9. "strconv"
  10. "git.wecise.com/wecise/common/logger"
  11. "github.com/atrox/homedir"
  12. "github.com/kevinburke/ssh_config"
  13. "golang.org/x/crypto/ssh"
  14. "gopkg.in/yaml.v2"
  15. )
  16. type Node struct {
  17. Name string `yaml:"name"`
  18. Alias string `yaml:"alias"`
  19. Host string `yaml:"host"`
  20. User string `yaml:"user"`
  21. Port int `yaml:"port"`
  22. KeyPath string `yaml:"keypath"`
  23. Passphrase string `yaml:"passphrase"`
  24. Password string `yaml:"password"`
  25. Commands []*Command `yaml:"commands"`
  26. Children []*Node `yaml:"children"`
  27. Jump []*Node `yaml:"jump"`
  28. }
  29. type Regexp struct {
  30. *regexp.Regexp
  31. Output string
  32. }
  33. type Command struct {
  34. Cmd string `yaml:"cmd"`
  35. Password string `yaml:"password"`
  36. Regexp []*Regexp
  37. }
  38. func (n *Node) String() string {
  39. return n.Name
  40. }
  41. func (n *Node) user() string {
  42. if n.User == "" {
  43. return "root"
  44. }
  45. return n.User
  46. }
  47. func (n *Node) port() int {
  48. if n.Port <= 0 {
  49. return 22
  50. }
  51. return n.Port
  52. }
  53. func (n *Node) password() ssh.AuthMethod {
  54. if n.Password == "" {
  55. return nil
  56. }
  57. return ssh.Password(n.Password)
  58. }
  59. func (n *Node) alias() string {
  60. return n.Alias
  61. }
  62. var (
  63. config []*Node
  64. )
  65. func GetConfig() []*Node {
  66. return config
  67. }
  68. func LoadConfig() error {
  69. b, err := LoadConfigBytes(".sshw", ".sshw.yml", ".sshw.yaml")
  70. if err != nil {
  71. return err
  72. }
  73. var c []*Node
  74. err = yaml.Unmarshal(b, &c)
  75. if err != nil {
  76. return err
  77. }
  78. config = c
  79. return nil
  80. }
  81. func LoadSshConfig() error {
  82. u, err := user.Current()
  83. if err != nil {
  84. logger.Error(err)
  85. return nil
  86. }
  87. f, _ := os.Open(path.Join(u.HomeDir, ".ssh/config"))
  88. cfg, _ := ssh_config.Decode(f)
  89. var nc []*Node
  90. for _, host := range cfg.Hosts {
  91. alias := fmt.Sprintf("%s", host.Patterns[0])
  92. hostName, err := cfg.Get(alias, "HostName")
  93. if err != nil {
  94. return err
  95. }
  96. if hostName != "" {
  97. port, _ := cfg.Get(alias, "Port")
  98. if port == "" {
  99. port = "22"
  100. }
  101. var c = new(Node)
  102. c.Name = alias
  103. c.Alias = alias
  104. c.Host = hostName
  105. c.User, _ = cfg.Get(alias, "User")
  106. c.Port, _ = strconv.Atoi(port)
  107. keyPath, _ := cfg.Get(alias, "IdentityFile")
  108. c.KeyPath, _ = homedir.Expand(keyPath)
  109. nc = append(nc, c)
  110. // fmt.Println(c.Alias, c.Host, c.User, c.Port, c.KeyPath)
  111. }
  112. }
  113. config = nc
  114. return nil
  115. }
  116. func LoadConfigBytes(names ...string) ([]byte, error) {
  117. u, err := user.Current()
  118. if err != nil {
  119. return nil, err
  120. }
  121. // homedir
  122. for i := range names {
  123. sshw, err := ioutil.ReadFile(path.Join(u.HomeDir, names[i]))
  124. if err == nil {
  125. return sshw, nil
  126. }
  127. }
  128. // relative
  129. for i := range names {
  130. sshw, err := ioutil.ReadFile(names[i])
  131. if err == nil {
  132. return sshw, nil
  133. }
  134. }
  135. return nil, err
  136. }