Skip to content

Commit

Permalink
add gmre cli feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Naseredin Aramnejad committed Mar 17, 2022
1 parent 29df2b5 commit 1ead294
Showing 1 changed file with 61 additions and 4 deletions.
65 changes: 61 additions & 4 deletions pssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,28 @@ func (s *Nokia_1830PSS) cliLogin() error {
}

//Run executes the given cli command on the opened session.
func (s *Nokia_1830PSS) Run(cmd string) (string, error) {
func (s *Nokia_1830PSS) Run(env, cmd string) (string, error) {
prompt := []string{s.Name + "#"}
if env == "gmre" {
if err := s.gmreLogin(); err != nil {
return "", err
}
cmd += "\r"
prompt = []string{"]#"}
}

if _, err := writeBuff(cmd, s.SshIn); err != nil {
s.Session.Close()
return "", fmt.Errorf("%v:%v - failure on Exec(%v) - details: %v", s.Ip, s.Port, cmd, err.Error())
return "", fmt.Errorf("%v:%v - failure on Run(%v) - details: %v", s.Ip, s.Port, cmd, err.Error())
}

data, err := readBuff([]string{s.Name + "#"}, s.SshOut, 15)
data, err := readBuff(prompt, s.SshOut, 15)
if err != nil {
return "", fmt.Errorf("%v:%v - failure on Exec(%v) - readBuff(%v#) - details: %v", s.Ip, s.Port, s.Name, cmd, err.Error())
return "", fmt.Errorf("%v:%v - failure on Run(%v) - readBuff(%v#) - details: %v", s.Ip, s.Port, s.Name, cmd, err.Error())
}

if env == "gmre" {
s.gmreLogout()
}

return data, nil
Expand Down Expand Up @@ -242,3 +255,47 @@ func validateNode(s *Nokia_1830PSS) error {

return nil
}

func (s *Nokia_1830PSS) gmreLogin() error {
if _, err := writeBuff("tools gmre", s.SshIn); err != nil {
s.Session.Close()
return fmt.Errorf("%v:%v - failure on Run(tools gmre) - details: %v", s.Ip, s.Port, err.Error())
}

if _, err := readBuff([]string{"username:"}, s.SshOut, 15); err != nil {
return fmt.Errorf("%v:%v - failure on gmre login - readBuff(username:) - details: %v", s.Ip, s.Port, err.Error())
}

if _, err := writeBuff("gmre\r", s.SshIn); err != nil {
s.Session.Close()
return fmt.Errorf("%v:%v - failure on username(gmre) - details: %v", s.Ip, s.Port, err.Error())
}

if _, err := readBuff([]string{"password:"}, s.SshOut, 15); err != nil {
return fmt.Errorf("%v:%v - failure on gmre login - readBuff(password:) - details: %v", s.Ip, s.Port, err.Error())
}

if _, err := writeBuff("gmre\r", s.SshIn); err != nil {
s.Session.Close()
return fmt.Errorf("%v:%v - failure on password(gmre) - details: %v", s.Ip, s.Port, err.Error())
}

if _, err := readBuff([]string{"]#"}, s.SshOut, 15); err != nil {
return fmt.Errorf("%v:%v - failure on gmre login - readBuff(]#) - details: %v", s.Ip, s.Port, err.Error())
}

return nil
}

func (s *Nokia_1830PSS) gmreLogout() error {
if _, err := writeBuff("quit\r", s.SshIn); err != nil {
s.Session.Close()
return fmt.Errorf("%v:%v - failure on Run(quit) - details: %v", s.Ip, s.Port, err.Error())
}

if _, err := readBuff([]string{s.Name + "#"}, s.SshOut, 15); err != nil {
return fmt.Errorf("%v:%v - failure on gmre login - readBuff(cli prompt) - details: %v", s.Ip, s.Port, err.Error())
}

return nil
}

0 comments on commit 1ead294

Please sign in to comment.