コード例 #1
0
ファイル: expect_test.go プロジェクト: jamesharr/expect
func TestExpect_send(t *testing.T) {
	// Start cat
	exp, err := expect.Spawn("cat")
	if err != nil {
		t.Error("Unexpected error spawning 'cat'", err)
	}
	defer exp.Close()
	exp.SetLogger(expect.TestLogger(t))
	exp.SetTimeout(time.Second)

	// Send some data
	err = exp.Send("Hello\nWorld\n")
	if err != nil {
		t.Error("Unexpected error spawning 'cat'", err)
	}

	// Get first chunk
	m, err := exp.Expect("Hello")
	if err != nil {
		t.Error("Expect() error:", err)
	}
	m_exp := expect.Match{
		Before: "",
		Groups: []string{"Hello"},
	}
	if !reflect.DeepEqual(m, m_exp) {
		t.Errorf("expected match to be %v, got %v", m_exp, m)
	}

	// Check new lines
	m, err = exp.Expect("World\n")
	m_exp = expect.Match{
		Before: "\n",
		Groups: []string{"World\n"},
	}
	if !reflect.DeepEqual(m, m_exp) {
		t.Errorf("expected match to be %v, got %v", m_exp, m)
	}
}
コード例 #2
0
func usage() { // OMIT
	exp, err := expect.Spawn("ssh", "localhost")
	if err != nil {
		panic(err)
	}
	defer exp.Close()

	exp.SetTimeout(5 * time.Second) // HL

	exp.Expect(`[Pp]assword:`) // HL
	exp.Sendln("terriblepassword")

	exp.Expect(`\$`) // HL
	exp.Sendln("ls -lh")
	exp.Expect("ls -lh") // Cut out remote-echo

	m, _ := exp.Expect(`(?m)^.*\$`) // HL
	fmt.Println("Directory Listing:", m.Before)

	exp.Sendln("exit") // HL
	exp.ExpectEOF()    // HL
} // OMIT
コード例 #3
0
ファイル: expect_test.go プロジェクト: jamesharr/expect
func TestExpect_largeBuffer(t *testing.T) {
	// Start cat
	exp, err := expect.Spawn("cat")
	if err != nil {
		t.Error("Unexpected error spawning 'cat'", err)
	}
	defer exp.Close()
	exp.SetLogger(expect.TestLogger(t))
	exp.SetTimeout(time.Second)

	// Sending large amounts of text
	t.Log("Generating large amounts of text")
	text := make([]byte, 128)
	for i := range text {
		text[i] = '.'
	}
	text[len(text)-1] = '\n'

	t.Log("Writing large amounts of text")
	for i := 0; i < 1024; i++ {
		//		t.Logf(" Writing %d bytes", i*len(text))
		err := exp.Send(string(text))
		if err != nil {
			t.Logf(" Send Error: %#v", err)
		}
	}
	exp.Send("\nDONE\n")

	t.Log("Expecting to see finish message")
	match, err := exp.Expect("DONE")
	t.Logf(" match.Groups=%#v", match.Groups)
	t.Logf(" err=%#v", err)
	if err != nil {
		t.Error("Unexpected err", err)
	}

}
コード例 #4
0
ファイル: ssh.go プロジェクト: jamesharr/expect
func main() {

	// Start up ssh process
	exp, err := expect.Spawn(
		"ssh",
		"-F", "/dev/null",
		"-o", "UserKnownHostsFile /dev/null",
		"-o", "StricthostKeyChecking false",
		"localhost",
	)
	checkErr(err)

	// Add logger
	exp.SetLogger(expect.FileLogger("ssh.log"))
	//	exp.SetLogger(expect.StderrLogger())

	// Set a timeout
	exp.SetTimeout(5 * time.Second)

	// Loop with until user gets password right
	for loggedIn := false; !loggedIn; {
		m, err := exp.Expect(`[Pp]assword:|\$`)
		checkErr(err)

		if m.Groups[0] == "$" {
			loggedIn = true
		} else {
			password := readPassword()
			exp.SendMasked(password)
			exp.Send("\n")
		}
	}

	// Run a command, chew up echo.
	const CMD = "ls -lh"
	checkErr(exp.SendLn(CMD))
	_, err = exp.Expect(CMD)
	checkErr(err)

	// Expect new prompt, get results from m.Before
	m, err := exp.Expect(`(?m)^.*\$`)
	checkErr(err)
	fmt.Println("Directory Listing:", m.Before)

	// Exit
	checkErr(exp.SendLn("exit"))

	// Remote should close the connection
	err = exp.ExpectEOF()
	if err != io.EOF {
		panic(fmt.Sprintf("Expected EOF, got %v", err))
	}

	// In most cases you'd do this in an 'defer' clause right after it was
	// opened.
	exp.Close()

	// You can use this to see that there's no extra expect processes running
	// time.Sleep(100 * time.Millisecond)
	// panic("DEBUG: Who's running")
}