telnet library fork
转到文件
kayos@tcp.direct bcb5811a5a more comprehensive regex 2021-02-23 05:43:33 -08:00
README.md Update README.md 2018-08-08 01:23:03 +03:00
cmd.go SetPrompt added 2018-08-15 13:01:30 +03:00
constants.go strip iac 2018-08-26 16:31:05 +03:00
login.go more comprehensive regex 2021-02-23 05:43:33 -08:00
negotiation.go strip iac 2018-08-26 16:31:05 +03:00
reader.go fix 2018-09-08 10:46:49 +03:00
telnet.go more comprehensive regex 2021-02-23 05:43:33 -08:00
telnet_test.go SetPrompt added 2018-08-15 13:01:30 +03:00
writer.go first commit 2018-08-07 18:54:08 +03:00

tclient

Simple telnet client lib, written in golang.

Some docs: https://godoc.org/github.com/ircop/tclient

Example usage:

main.go:

	client := tclient.New(5, "")
	err := client.Open("10.10.10.10", 23)
	if err != nil {
		panic(err)
	}
	defer client.Close()

	// you can omit this, or do auth stuff manually by calling `ReadUntil` with login/password prompts
	out, err := client.Login("script2", "pw3")
	if err != nil {
		panic(err)
	}
	fmt.Printf(out)

	out, err = client.Cmd("show time")
	if err != nil {
		panic(err)
	}
	fmt.Printf(out)

Output:

Output

Matching callbacks

You can define regular expressions and callbacks that would be called when current output string will match one of regexps.

For example, we need to catch pagination on D-Link switch. Sample show switch paginated output:

Show switch

There is no prompt, so app will stuck on this. So we need to catch something like CTRL+C ESC q Quit SPACE n Next Page ENTER Next Entry a All and send 'n' (next page) or 'a' (all pages). Like this:

	// matching "CTRL+C ESC q Quit SPACE n Next Page ENTER Next Entry a All"
	err = client.RegisterCallback(`(?msi:CTRL\+C.+?a A[Ll][Ll]\s*)`, func() {
		client.WriteRaw([]byte("a"))
	})
	if err != nil {
		panic(err)
	}

Note we are using WriteRaw(), not Write(), because Write() adds CRLF to given string.

TODO

Implement pagination parsing/manipulating (various network devices paging their output)

Implement parsing regexps with callbacks. For output pagination purposes, etc.