Skip to content

Commit

Permalink
- add compatibility for direct login in privileged mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ipcjk committed Jul 7, 2017
1 parent 244219d commit 9776c0a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
2 changes: 1 addition & 1 deletion mlxsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func init() {
flag.Parse()

if version {
log.Println("mlxsh 0.2a (C) 2017 by Jörg Kost, [email protected]")
log.Println("mlxsh 0.2b (C) 2017 by Jörg Kost, [email protected]")
os.Exit(0)
}

Expand Down
44 changes: 39 additions & 5 deletions netironDevice/netiron.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"strings"
"time"
"regexp"
)

type NetironConfig struct {
Expand Down Expand Up @@ -46,7 +47,7 @@ func NetironDevice(Config NetironConfig) *netironDevice {
/* Add some ciphers for old ironware versions (xmr,mlx,turobiron,...)
*/
sshClientConfig.Ciphers = append(sshClientConfig.Ciphers, "aes128-cbc", "3des-cbc")

/* Allow authentication with ssh dsa or rsa key */
if Config.KeyFile != "" {
if file, err := os.Open(Config.KeyFile); err != nil {
Expand Down Expand Up @@ -121,11 +122,42 @@ func (b *netironDevice) ConnectPrivilegedMode() (err error) {
return err
}

b.sshUnprivilegedPrompt, err = b.readTill([]string{">"})
prompt, err := b.readTill([]string{">", "#"})
if err != nil {
return err
}

if err := b.DetectSetPrompt(prompt); err != nil {
return err
}

/* Try login if promptMode is NonEnabled */
if b.promptMode == "sshNonEnabled" && !b.loginDialog() {
return fmt.Errorf("Cant login")
}
return
}

func (b *netironDevice) DetectSetPrompt(prompt string) error {
matched, err := regexp.MatchString(">$", prompt)
if err == nil && matched {
b.promptMode = "sshNonEnabled"
b.sshUnprivilegedPrompt = prompt
} else if err != nil {
return fmt.Errorf("Cant run regexp for prompt detection, weird!")
}

matched, err = regexp.MatchString("#$", prompt)
if err == nil && matched {
b.promptMode = "sshEnabled"
b.sshUnprivilegedPrompt = strings.Replace(prompt, "#", ">", 1)
} else if err != nil {
return fmt.Errorf("Cant run regexp for prompt detection, weird!")
}

/*
FIXME: Need regex for replace the last one, not the first match
*/
b.sshEnabledPrompt = strings.Replace(b.sshUnprivilegedPrompt, ">", "#", 1)
b.sshConfigPrompt = strings.Replace(b.sshUnprivilegedPrompt, ">", "(config)#", 1)
b.sshConfigPromptPre = strings.Replace(b.sshUnprivilegedPrompt, ">", "(config", 1)
Expand All @@ -142,10 +174,12 @@ func (b *netironDevice) ConnectPrivilegedMode() (err error) {
fmt.Fprintf(b.W, "ConfigSection:(%s)\n", b.sshConfigPromptPre)
}

if !b.loginDialog() {
return fmt.Errorf("Cant login")
if b.sshEnabledPrompt == "" || b.sshUnprivilegedPrompt == "" {
return fmt.Errorf("Cant detect any prompt")
}
return

return nil

}

func (b *netironDevice) loginDialog() bool {
Expand Down
22 changes: 22 additions & 0 deletions netironDevice/netiron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ func TestSSHConnect(t *testing.T) {

}

func TestDetectPrompt(t *testing.T) {
var Config = libhost.HostConfig{
DeviceType: "MLX",
Hostname: "localhost",
Username: "myuser",
Password: "mypassword",
EnablePassword: "enablepassword",
}

router := netironDevice.NetironDevice(
netironDevice.NetironConfig{HostConfig: Config, Debug: true, W: new(bytes.Buffer)})

if err := router.DetectSetPrompt("SSH@frankfurt-rt1#"); err != nil {
t.Errorf("Cant detect prompt! :%s", err)
}

if err := router.DetectSetPrompt("SSH@frankfurt-rt1>"); err != nil {
t.Errorf("Cant detect prompt! :%s", err)
}

}

var sampleSSHKey = `-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA3h5u/Jb0TKlwLAwOgaVeHevwMdCqwf2mJRvVMheNOeu2qSEk
18Rf3YS3URkUvZhdQmd/fafJYALamcxl1nO9IVEUvWXBIn3pjKR5Yf6rl4bl8V7n
Expand Down

0 comments on commit 9776c0a

Please sign in to comment.