Beispiel #1
0
func (d *Driver) mountSharedFolder(shareDir, shareName string) error {
	// create mountpoint and mount shared folder
	addr, err := d.GetSSHHostname()
	if err != nil {
		return err
	}

	port, err := d.GetSSHPort()
	if err != nil {
		return err
	}

	auth := &ssh.Auth{
		Keys: []string{d.GetSSHKeyPath()},
	}

	client, err := ssh.NewClient(d.GetSSHUsername(), addr, port, auth)
	if err != nil {
		return err
	}

	command := "[ ! -d " + shareDir + " ]&& sudo mkdir " + shareDir + "; sudo mount -t vmhgfs .host:/" + shareName + " " + shareDir

	log.Debugf("About to run SSH command:\n%s", command)
	output, err := client.Output(command)
	log.Debugf("SSH cmd err, output: %v: %s", err, output)
	return err
}
Beispiel #2
0
func (d *Driver) uploadKeyPair() error {
	auth := ssh.Auth{
		Passwords: []string{d.SSHPass},
	}
	port, err := d.GetSSHPort()
	if err != nil {
		return nil
	}

	ip, err := d.GetIP()
	if err != nil {
		return nil
	}

	sshCli, err := ssh.NewClient(d.GetSSHUsername(), ip, port, &auth)
	if err != nil {
		return err
	}
	pk := d.PublicKey[0 : len(d.PublicKey)-1]
	log.Debugf("upload --- command...")
	command := fmt.Sprintf("mkdir -p ~/.ssh; echo '%s' > ~/.ssh/authorized_keys", string(pk))
	output, err := sshCli.Output(command)

	log.Debugf("upload public key with err, output: %v: %s", err, output)

	if err != nil {
		return err
	}

	if strings.Contains(d.ImageId, "centos6") {
		output, err = sshCli.Output("chmod 644 /etc/sudoers; sed -r -i 's/^(Defaults\\s+requiretty)$/#\\1/' /etc/sudoers; chmod 400 /etc/sudoers")
		log.Debugf("enable sudo without tty with err, output: %v: %s", err, output)

		output, err = sshCli.Output("sed -i '$a ulimit -c unlimited' /etc/profile; source /etc/profile")
		log.Debugf("enable core dump with err, output: %v: %s", err, output)

		output, err = sshCli.Output("mkdir -p /corefile; echo \"/corefile/core-%e-%p-%t\" > /proc/sys/kernel/core_pattern")
		log.Debugf("modify core dump pattern with err, output: %v: %s", err, output)
	} else {
		output, err = sshCli.Output("route -n| grep -e '^172\\..*$'|awk '{print \"route del -net \"$1\" netmask \"$3\" dev \"$8|\"/bin/bash\"}'")
		log.Debugf("delete route rule with err, output: %v: %s", err, output)

		output, err = sshCli.Output("sed -r -i 's/^(up route add \\-net 172\\..*)$/#\\1/' /etc/network/interfaces")
		log.Debugf("fix route in /etc/network/interfaces with err, output: %v: %s", err, output)
	}

	return nil
}
Beispiel #3
0
func (c *ComputeUtil) executeCommands(commands []string, ip, sshKeyPath string) error {
	for _, command := range commands {
		auth := &ssh.Auth{
			Keys: []string{sshKeyPath},
		}

		client, err := ssh.NewClient(c.userName, ip, 22, auth)
		if err != nil {
			return err
		}

		if _, err := client.Output(command); err != nil {
			return err
		}
	}
	return nil
}
Beispiel #4
0
func (h *Host) CreateSSHClient() (ssh.Client, error) {
	addr, err := h.Driver.GetSSHHostname()
	if err != nil {
		return ssh.ExternalClient{}, err
	}

	port, err := h.Driver.GetSSHPort()
	if err != nil {
		return ssh.ExternalClient{}, err
	}

	auth := &ssh.Auth{
		Keys: []string{h.Driver.GetSSHKeyPath()},
	}

	return ssh.NewClient(h.Driver.GetSSHUsername(), addr, port, auth)
}
Beispiel #5
0
func GetSSHClientFromDriver(d Driver) (ssh.Client, error) {
	addr, err := d.GetSSHHostname()
	if err != nil {
		return nil, err
	}

	port, err := d.GetSSHPort()
	if err != nil {
		return nil, err
	}

	auth := &ssh.Auth{
		Keys: []string{d.GetSSHKeyPath()},
	}

	client, err := ssh.NewClient(d.GetSSHUsername(), addr, port, auth)
	return client, err

}
Beispiel #6
0
func (d *Driver) replaceInsecureSSHKey() error {
	addr, err := d.GetSSHHostname()
	if err != nil {
		return err
	}

	port, err := d.GetSSHPort()
	if err != nil {
		return err
	}

	tempssh, err := ioutil.TempFile(os.TempDir(), "appcatalyst-ssh-insecure-key")
	defer os.Remove(tempssh.Name())

	_, err = tempssh.WriteString(insecureSSHKey)
	if err != nil {
		return err
	}

	auth := &ssh.Auth{
		Keys: []string{tempssh.Name()},
	}

	client, err := ssh.NewClient(d.GetSSHUsername(), addr, port, auth)
	if err != nil {
		return err
	}

	pubkey, err := d.readSSHKey()
	if err != nil {
		return err
	}

	command := "echo '" + pubkey + "' > ~/.ssh/authorized_keys"

	log.Debugf("About to run SSH command:\n%s", command)
	output, err := client.Output(command)
	log.Debugf("SSH cmd err, output: %v: %s", err, output)
	return err
}
Beispiel #7
0
func (d *Driver) uploadKeyPair() error {

	ipAddr := d.IPAddress
	port, _ := d.GetSSHPort()
	tcpAddr := fmt.Sprintf("%s:%d", ipAddr, port)

	ssh.WaitForTCP(tcpAddr)

	auth := ssh.Auth{
		Passwords: []string{d.SSHPassword},
	}

	sshClient, err := ssh.NewClient(d.GetSSHUsername(), ipAddr, port, &auth)

	if err != nil {
		return err
	}

	command := fmt.Sprintf("mkdir -p ~/.ssh; echo '%s' > ~/.ssh/authorized_keys", string(d.PublicKey))

	log.Debugf("Upload the public key with command: %s", command)

	output, err := sshClient.Output(command)

	log.Debugf("Upload command err, output: %v: %s", err, output)

	if err != nil {
		return err
	}

	log.Debugf("Upload the public key with command: %s", command)

	fixRoutingRules(sshClient)

	return nil
}