SSH client with identities support
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package main
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "os/user"
  6. "path"
  7. "strings"
  8. "gopkg.in/yaml.v2"
  9. )
  10. type Config struct {
  11. BinDir string `yaml:"bin_dir"`
  12. AgentsDir string
  13. IdentitiesDir string
  14. DefaultIdentity string `yaml:"default_identity"`
  15. Identities map[string]string
  16. }
  17. var sshOptions = map[string]map[string]string{
  18. "ssh": {
  19. "short": "1246AaconfiggKkMNnqsTtVvXxYy",
  20. "long": "bcDEeFIiJLlmOopQRSWw",
  21. },
  22. "scp": {
  23. "short": "12346BCpqrv",
  24. "long": "cFiloPS",
  25. },
  26. }
  27. func removeOptions(prog string, args []string) []string {
  28. notOptions := []string{}
  29. longOptions := sshOptions[prog]["long"]
  30. long := false
  31. for _, arg := range args {
  32. if long {
  33. long = false
  34. continue
  35. } else if strings.HasPrefix(arg, "-") {
  36. last := arg[len(arg)-1:]
  37. long = strings.Contains(longOptions, last)
  38. } else {
  39. notOptions = append(notOptions, arg)
  40. }
  41. }
  42. return notOptions
  43. }
  44. func main() {
  45. usr, err := user.Current()
  46. fatal(err)
  47. home := usr.HomeDir
  48. sshDir := path.Join(home, ".ssh")
  49. identitiesDir := path.Join(sshDir, "identities")
  50. configFile := path.Join(identitiesDir, "config.yml")
  51. data, err := ioutil.ReadFile(configFile)
  52. fatal(err)
  53. config := Config{}
  54. err = yaml.Unmarshal(data, &config)
  55. fatal(err)
  56. config.IdentitiesDir = identitiesDir
  57. prog := os.Getenv("SSH_BINARY")
  58. if prog == "" {
  59. prog = os.Args[0]
  60. prog = path.Base(prog)
  61. }
  62. args := os.Args[1:]
  63. identity := FindIdentity(config, prog, args)
  64. agentsDir := path.Join(sshDir, "agents")
  65. config.AgentsDir = agentsDir
  66. agent := NewAgent(config, identity)
  67. agent.Run(config, prog, args)
  68. }