Ejemplo n.º 1
0
func main() {
	exp, err := expect.Spawn("mail")
	if err != nil {
		log.Fatalf("failed to spawn mail program: %s", err)
	}
	exp.LogUser(true)

	for {
		// '&' is the mail user prompt
		i, _, err := exp.Expect("No mail", "& ", "No applicable messages")
		if err != nil {
			log.Fatalf("failed to match expressed expressions: %s", err)
		}

		if i == 0 { // Nothing to delete
			break
		} else if i == 1 { // prompt
			exp.SendL("d")
		} else if i == 2 { // no applicable messages - everything deleted
			exp.SendL("q")
			break
		} else if i == expect.NotFound {
			log.Fatalf("program closed unexpectedly")
		}
	}
}
Ejemplo n.º 2
0
func main() {
	exp, err := expect.Spawn("python")
	if err != nil {
		log.Fatalf("Failed to spawn python process: %s", err)
	}
	exp.LogUser(true)

	exp.Expect(">>>")
	exp.SendL("import time", "print 'It is now %s' % time.ctime(time.time())", "exit()")
	exp.Expect("exit()")
}
Ejemplo n.º 3
0
Archivo: ftp.go Proyecto: grrtrr/expect
func main() {
	exp, err := expect.Spawn("ftp", "openbsd.cs.toronto.edu")
	if err != nil {
		log.Fatalf("failed to spawn OpenBSD ftp: %s", err)
	}
	exp.LogUser(true)

	exp.Expect("Name")
	exp.SendL("anonymous")
	exp.Expect("Password")
	exp.SendL("*****@*****.**")
	exp.Expect("ftp> ")
	exp.SendL("cd /pub/OpenBSD/5.8")
	exp.Expect("ftp> ")
	exp.SendL("dir")
	exp.Expect("ftp> ")
	exp.SendL("prompt")
	exp.Expect("ftp> ")
	exp.SendL("pwd")
	exp.Expect("ftp> ")
}
Ejemplo n.º 4
0
func main() {
	var remoteUser = flag.String("l", "", "Remote user to use (defaults to local user)")
	var password = flag.String("p", "", "Password for remote user")
	var verbose = flag.Bool("v", true, "Log user interaction to stdout")

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "usage: %s [options] <remoteHost>\n", path.Base(os.Args[0]))
		flag.PrintDefaults()
	}
	flag.Parse()

	if flag.NArg() != 1 || *password == "" {
		flag.Usage()
		os.Exit(1)
	}
	host := flag.Arg(0)

	user, err := user.Current()
	if err != nil {
		log.Fatalf("failed to look up current user: %s", err)
	}

	if *remoteUser == "" {
		*remoteUser = user.Username
	}

	keyPath := path.Join(user.HomeDir, ".ssh", keyName)
	if _, err := os.Stat(keyPath); err != nil {
		log.Fatalf("Can not use ssh key %s: %s", keyPath, err)
	}

	// First remove existing entries for @host from ~/.ssh/known_hosts
	exp, err := expect.Spawn("ssh-keygen", "-R", host)
	if err != nil {
		log.Fatalf("failed to remove existing hostkeys for %s: %s", host, err)
	}
	exp.LogUser(*verbose)

	_, _, err = exp.Expect("known_hosts: No such file or directory", "Original contents retained")
	if err != nil {
		log.Fatalf("unexpected interaction removing %s from known_hosts: %s", host, err)
	}
	exp.Kill()

	exp, err = expect.Spawn("ssh-copy-id", "-i", keyPath, fmt.Sprintf("%s@%s", *remoteUser, host))
	if err != nil {
		log.Fatalf("filed to run ssh-copy-id: %s", err)
	}
	exp.LogUser(*verbose)

	// We have removed the known_hosts key, so it will prompt to add it back again
	exp.Expect("(yes/no)?")
	exp.SendL("yes")

	for {
		i, _, err := exp.Expect(
			"assword:",                    // standard password prompt
			"Permission denied",           // password received, but incorrect
			"already exist on the remote", // key already exists
			"Now try",                     // issued after transfer
		)
		if err != nil {
			log.Fatalf("expect error: %s", err)
		} else if i == 3 {
			log.Printf("Successfully transferred ssh key for %s@%s", *remoteUser, host)
			break
		} else if i == 2 {
			log.Printf("Key for %s already exists on %s", *remoteUser, host)
			break
		} else if i == 1 {
			log.Fatalf("Wrong password (permission denied) for %s@%s", *remoteUser, host)
		} else if i == 0 {
			exp.SendL(*password)
		} else {
			log.Fatalf("unexpected response - try -v to debug")
		}
	}
}