예제 #1
0
func (c *Communicator) newCopyClient() (*winrmcp.Winrmcp, error) {
	addr := fmt.Sprintf("%s:%d", c.endpoint.Host, c.endpoint.Port)
	return winrmcp.New(addr, &winrmcp.Config{
		Auth: winrmcp.Auth{
			User:     c.connInfo.User,
			Password: c.connInfo.Password,
		},
		OperationTimeout:      c.Timeout(),
		MaxOperationsPerShell: 15, // lowest common denominator
	})
}
예제 #2
0
func (c *Communicator) newCopyClient() (*winrmcp.Winrmcp, error) {
	addr := fmt.Sprintf("%s:%d", c.endpoint.Host, c.endpoint.Port)
	return winrmcp.New(addr, &winrmcp.Config{
		Auth: winrmcp.Auth{
			User:     c.config.Username,
			Password: c.config.Password,
		},
		Https:                 c.config.Https,
		Insecure:              c.config.Insecure,
		OperationTimeout:      c.config.Timeout,
		MaxOperationsPerShell: 15, // lowest common denominator
		TransportDecorator:    c.config.TransportDecorator,
	})
}
예제 #3
0
파일: main.go 프로젝트: pecigonzalo/winrmcp
func runMain() error {
	flags := flag.NewFlagSet("cli", flag.ContinueOnError)
	flags.Usage = func() { fmt.Print(usage) }
	addr := flags.String("addr", "localhost:5985", "winrm remote host:port")
	user := flags.String("user", "", "winrm admin username")
	pass := flags.String("pass", "", "winrm admin password")
	https := flags.Bool("https", false, "use https instead of http")
	insecure := flags.Bool("insecure", false, "do not validate https certificate chain")
	cacert := flags.String("cacert", "", "ca certificate to validate against")
	opTimeout := flags.Duration("op-timeout", time.Second*60, "operation timeout")
	maxOpsPerShell := flags.Int("max-ops-per-shell", 15, "max operations per shell")
	flags.Parse(os.Args[1:])

	var certBytes []byte
	var err error
	if *cacert != "" {
		certBytes, err = ioutil.ReadFile(*cacert)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
	} else {
		certBytes = nil
	}

	client, err := winrmcp.New(*addr, &winrmcp.Config{
		Auth:                  winrmcp.Auth{*user, *pass},
		Https:                 *https,
		Insecure:              *insecure,
		CACertBytes:           certBytes,
		OperationTimeout:      *opTimeout,
		MaxOperationsPerShell: *maxOpsPerShell,
	})
	if err != nil {
		return err
	}

	args := flags.Args()
	if len(args) < 1 {
		return errors.New("Source directory is required.")
	}
	if len(args) < 2 {
		return errors.New("Remote directory is required.")
	}
	if len(args) > 2 {
		return errors.New("Too many arguments.")
	}

	return client.Copy(args[0], args[1])
}
예제 #4
0
func StartElevated() (err error) {
	// The command gets put into an interpolated string in the PS script,
	// so we need to escape any embedded quotes.
	cmd = strings.Replace(cmd, "\"", "`\"", -1)

	elevatedScript, err := createCommandText()

	if err != nil {
		return err
	}

	// Upload the script which creates and manages the scheduled task
	winrmcp, err := winrmcp.New(fmt.Sprintf("%s:%d", hostname, port), &winrmcp.Config{
		Auth:                  winrmcp.Auth{user, pass},
		OperationTimeout:      time.Second * 60,
		MaxOperationsPerShell: 15,
	})
	tmpFile, err := ioutil.TempFile(os.TempDir(), "packer-elevated-shell.ps1")
	log.Printf("Temp file: %s", tmpFile.Name())

	writer := bufio.NewWriter(tmpFile)
	if _, err := writer.WriteString(elevatedScript); err != nil {
		return fmt.Errorf("Error preparing shell script: %s", err)
	}

	if err := writer.Flush(); err != nil {
		return fmt.Errorf("Error preparing shell script: %s", err)
	}

	tmpFile.Close()

	err = winrmcp.Copy(tmpFile.Name(), "${env:TEMP}/packer-elevated-shell.ps1")

	if err != nil {
		log.Printf("Error copying shell script: %s", err)
		return err
	}

	// Run the script that was uploaded
	command := fmt.Sprintf("powershell -executionpolicy bypass -file \"%s\"", "%TEMP%\\packer-elevated-shell.ps1")
	log.Printf("Running script: %s", command)
	client, err = winrm.NewClientWithParameters(&winrm.Endpoint{Host: hostname, Port: port, HTTPS: false, Insecure: true, CACert: nil}, user, pass, winrm.NewParameters(timeout, "en-US", 153600))
	_, err = client.RunWithInput(command, os.Stdout, os.Stderr, os.Stdin)
	return err
}
예제 #5
0
func (c *Communicator) newCopyClient() (*winrmcp.Winrmcp, error) {
	addr := fmt.Sprintf("%s:%d", c.endpoint.Host, c.endpoint.Port)

	config := winrmcp.Config{
		Auth: winrmcp.Auth{
			User:     c.connInfo.User,
			Password: c.connInfo.Password,
		},
		Https:                 c.connInfo.HTTPS,
		Insecure:              c.connInfo.Insecure,
		OperationTimeout:      c.Timeout(),
		MaxOperationsPerShell: 15, // lowest common denominator
	}

	if c.connInfo.CACert != nil {
		config.CACertBytes = *c.connInfo.CACert
	}

	return winrmcp.New(addr, &config)
}