SetPrompt added

This commit is contained in:
ircop 2018-08-15 13:01:30 +03:00
parent dad69cc3c7
commit 867e38478b
4 changed files with 21 additions and 19 deletions

5
cmd.go
View File

@ -1,13 +1,12 @@
package tclient
// Cmd sends command and returns output.
// Actually, it just sends given command to server and waits for default prompt, and you can du this stuff manually.
// Cmd sends command and returns output
func (c *TelnetClient) Cmd(cmd string) (string, error) {
err := c.Write([]byte(cmd))
if err != nil {
return "", err
}
return c.ReadUntil(c.Prompt)
return c.ReadUntil(c.prompt)
}

View File

@ -1,8 +1,6 @@
package tclient
// Login is a simple wrapper for login/password auth.
// All this func do is waits for specific login/password prompts and sends login/password.
// You can always implement login manually.
// Login is a simple wrapper for login/password auth
func (c *TelnetClient) Login(login string, password string) (string, error) {
// wait for login
result := ""
@ -31,7 +29,7 @@ func (c *TelnetClient) Login(login string, password string) (string, error) {
}
// and wait for prompt
out, err = c.ReadUntil(c.Prompt)
out, err = c.ReadUntil(c.prompt)
result += out
if err != nil {
return result, err

View File

@ -20,11 +20,11 @@ type TelnetClient struct {
// Timeout of read/write operations
Timeout int
// TimeoutGlobal is global operation timeout; i.e. like stucked in DLink-like refreshing pagination
TimeoutGlobal int
Prompt string
conn net.Conn
closed bool
Options []int
TimeoutGlobal int
prompt string
conn net.Conn
closed bool
Options []int
loginPrompt string
passwordPrompt string
@ -39,16 +39,16 @@ func New(tout int, prompt string) *TelnetClient {
tout = 1
}
c := TelnetClient{
Timeout: tout,
Prompt: `(?msi:[\$%#>]$)`,
loginPrompt: `[Uu]ser[Nn]ame\:$`,
Timeout: tout,
prompt: `(?msi:[\$%#>]$)`,
loginPrompt: `[Uu]ser[Nn]ame\:$`,
passwordPrompt: `[Pp]ass[Ww]ord\:$`,
closed:false,
Options: make([]int,0),
closed: false,
Options: make([]int,0),
}
if prompt != "" {
c.Prompt = prompt
c.prompt = prompt
}
// Global timeout defaults to 3 * rw timeout
@ -68,6 +68,11 @@ func (c *TelnetClient) GlobalTimeout(t int) {
c.TimeoutGlobal = t
}
// SetPrompt allows you to change prompt without re-creating ssh client
func (c *TelnetClient) SetPrompt(prompt string) {
c.prompt = prompt
}
// SetLoginPrompt sets custom login prompt. Default is "[Uu]ser[Nn]ame\:$"
func (c *TelnetClient) SetLoginPrompt(s string) {
c.loginPrompt = s

View File

@ -148,7 +148,7 @@ DES-3028:5#`
}
}()
out, err := client.ReadUntil(client.Prompt)
out, err := client.ReadUntil(client.prompt)
if err != nil {
t.Fatalf("Error reading prompt: %s\nlast: %s", err.Error(), out)
}