Example #1
0
File: main.go Project: rdrew4/cfops
func main() {
	var auths []ssh.AuthMethod
	if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
		auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers))

	}
	if *PASS != "" {
		auths = append(auths, ssh.Password(*PASS))
	}

	config := ssh.ClientConfig{
		User: *USER,
		Auth: auths,
	}
	addr := fmt.Sprintf("%s:%d", *HOST, *PORT)
	conn, err := ssh.Dial("tcp", addr, &config)
	if err != nil {
		log.Fatalf("unable to connect to [%s]: %v", addr, err)
	}
	defer conn.Close()

	c, err := sftp.NewClient(conn)
	if err != nil {
		log.Fatalf("unable to start sftp subsytem: %v", err)
	}
	defer c.Close()

	r, err := c.Open("/dev/zero")
	if err != nil {
		log.Fatal(err)
	}
	defer r.Close()

	w, err := os.OpenFile("/dev/null", syscall.O_WRONLY, 0600)
	if err != nil {
		log.Fatal(err)
	}
	defer w.Close()

	const size int64 = 1e9

	log.Printf("reading %v bytes", size)
	t1 := time.Now()
	n, err := io.Copy(w, io.LimitReader(r, size))
	if err != nil {
		log.Fatal(err)
	}
	if n != size {
		log.Fatalf("copy: expected %v bytes, got %d", size, n)
	}
	log.Printf("read %v bytes in %s", size, time.Since(t1))
}
Example #2
0
func TestAuth(t *testing.T) {
	a, b, err := netPipe()
	if err != nil {
		t.Fatalf("netPipe: %v", err)
	}

	defer a.Close()
	defer b.Close()

	agent, _, cleanup := startAgent(t)
	defer cleanup()

	if err := agent.Add(testPrivateKeys["rsa"], nil, "comment"); err != nil {
		t.Errorf("Add: %v", err)
	}

	serverConf := ssh.ServerConfig{}
	serverConf.AddHostKey(testSigners["rsa"])
	serverConf.PublicKeyCallback = func(c ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
		if bytes.Equal(key.Marshal(), testPublicKeys["rsa"].Marshal()) {
			return nil, nil
		}

		return nil, errors.New("pubkey rejected")
	}

	go func() {
		conn, _, _, err := ssh.NewServerConn(a, &serverConf)
		if err != nil {
			t.Fatalf("Server: %v", err)
		}
		conn.Close()
	}()

	conf := ssh.ClientConfig{}
	conf.Auth = append(conf.Auth, ssh.PublicKeysCallback(agent.Signers))
	conn, _, _, err := ssh.NewClientConn(b, "", &conf)
	if err != nil {
		t.Fatalf("NewClientConn: %v", err)
	}
	conn.Close()
}
Example #3
0
File: main.go Project: rdrew4/cfops
func main() {
	var auths []ssh.AuthMethod
	if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
		auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers))

	}
	if *PASS != "" {
		auths = append(auths, ssh.Password(*PASS))
	}

	config := ssh.ClientConfig{
		User: *USER,
		Auth: auths,
	}
	addr := fmt.Sprintf("%s:%d", *HOST, *PORT)
	conn, err := ssh.Dial("tcp", addr, &config)
	if err != nil {
		log.Fatalf("unable to connect to [%s]: %v", addr, err)
	}
	defer conn.Close()

	client, err := sftp.NewClient(conn)
	if err != nil {
		log.Fatalf("unable to start sftp subsytem: %v", err)
	}
	defer client.Close()
	switch cmd := flag.Args()[0]; cmd {
	case "ls":
		if len(flag.Args()) < 2 {
			log.Fatalf("%s %s: remote path required", cmd, os.Args[0])
		}
		walker := client.Walk(flag.Args()[1])
		for walker.Step() {
			if err := walker.Err(); err != nil {
				log.Println(err)
				continue
			}
			fmt.Println(walker.Path())
		}
	case "fetch":
		if len(flag.Args()) < 2 {
			log.Fatalf("%s %s: remote path required", cmd, os.Args[0])
		}
		f, err := client.Open(flag.Args()[1])
		if err != nil {
			log.Fatal(err)
		}
		defer f.Close()
		if _, err := io.Copy(os.Stdout, f); err != nil {
			log.Fatal(err)
		}
	case "put":
		if len(flag.Args()) < 2 {
			log.Fatalf("%s %s: remote path required", cmd, os.Args[0])
		}
		f, err := client.Create(flag.Args()[1])
		if err != nil {
			log.Fatal(err)
		}
		defer f.Close()
		if _, err := io.Copy(f, os.Stdin); err != nil {
			log.Fatal(err)
		}
	case "stat":
		if len(flag.Args()) < 2 {
			log.Fatalf("%s %s: remote path required", cmd, os.Args[0])
		}
		f, err := client.Open(flag.Args()[1])
		if err != nil {
			log.Fatal(err)
		}
		defer f.Close()
		fi, err := f.Stat()
		if err != nil {
			log.Fatalf("unable to stat file: %v", err)
		}
		fmt.Printf("%s %d %v\n", fi.Name(), fi.Size(), fi.Mode())
	case "rm":
		if len(flag.Args()) < 2 {
			log.Fatalf("%s %s: remote path required", cmd, os.Args[0])
		}
		if err := client.Remove(flag.Args()[1]); err != nil {
			log.Fatalf("unable to remove file: %v", err)
		}
	case "mv":
		if len(flag.Args()) < 3 {
			log.Fatalf("%s %s: old and new name required", cmd, os.Args[0])
		}
		if err := client.Rename(flag.Args()[1], flag.Args()[2]); err != nil {
			log.Fatalf("unable to rename file: %v", err)
		}
	default:
		log.Fatalf("unknown subcommand: %v", cmd)
	}
}