コード例 #1
0
ファイル: main.go プロジェクト: idcos/osinstall-server
// 修改主机名
func changeHostname(hostname string) error {
	var cmd = `wmic ntdomain get Caption  /value`
	var r = `Caption=(.*)`
	var oldname string
	if output, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
		return nil
	} else {
		oldname = string(output)
	}
	reg := regexp.MustCompile(r)
	var regResult = reg.FindStringSubmatch(oldname)
	if regResult == nil || len(regResult) != 2 {
		return errors.New("not fount caption")
	}

	oldname = strings.TrimSpace(regResult[1])
	utils.Logger.Debug(oldname)

	cmd = fmt.Sprintf(`netdom renamecomputer %s /newname:%s /force`, strings.Trim(oldname, "\r\n"), hostname)
	utils.Logger.Debug(cmd)

	if output, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
		return nil
	} else {
		// fmt.Println(string(output))
		utils.Logger.Debug(string(output))
	}
	return nil
}
コード例 #2
0
ファイル: main.go プロジェクト: idcos/osinstall-server
func loadDrive() {
	var cmd = `Z:`
	var output string
	utils.Logger.Debug(cmd)
	if outputBytes, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
	} else {
		output = string(outputBytes)
	}
	utils.Logger.Info(output)
	//load drive
	dirs, err := utils.ListDir("Z:\\windows\\drivers\\winpe")
	if err != nil {
		utils.Logger.Error(err.Error())
	}
	for _, dir := range dirs {
		files, err := utils.ListFiles(dir, ".inf", true)
		if err != nil {
			utils.Logger.Error(err.Error())
		}
		for _, file := range files {
			//cd dir
			cmd = `cmd /c "cd /d ` + dir + ` && drvload ` + file + `"`
			utils.Logger.Debug(cmd)
			if outputBytes, err := utils.ExecCmd(scriptFile, cmd); err != nil {
				utils.Logger.Error(err.Error())
			} else {
				output = string(outputBytes)
				utils.Logger.Info(output)
			}
			/*
				//load
				cmd = `drvload ` + file
				utils.Logger.Debug(cmd)
				if outputBytes, err := utils.ExecCmd(scriptFile, cmd); err != nil {
					utils.Logger.Error(err.Error())
				} else {
					output = string(outputBytes)
					utils.Logger.Info(output)
				}
			*/
		}
	}
	//go back to X:
	cmd = `X:`
	utils.Logger.Debug(cmd)
	if outputBytes, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
	} else {
		output = string(outputBytes)
	}
	utils.Logger.Info(output)
	return
}
コード例 #3
0
ファイル: main.go プロジェクト: idcos/osinstall-server
func installWindows() error {
	// get setup.exe path from xmlPath
	var output []byte
	var err error
	if output, err = ioutil.ReadFile(xmlPath); err != nil {
		utils.Logger.Error(err.Error())
		return err
	}

	var r = `<Path>(.*)\\install.wim</Path>`
	reg := regexp.MustCompile(r)
	var regResult = reg.FindStringSubmatch(string(output))
	if regResult == nil || len(regResult) != 2 {
		return fmt.Errorf("Can't found %s", "install.wim")
	}
	utils.Logger.Debug("setup path: %s", regResult[1])

	var cmd = fmt.Sprintf(`%s\\setup.exe /unattend:unattended.xml /noreboot`, regResult[1])
	utils.Logger.Debug(cmd)
	if _, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
		return err
	}

	return nil
}
コード例 #4
0
ファイル: main.go プロジェクト: idcos/osinstall-server
func mountSamba() error {
	var cmd = `net use Z:`
	utils.Logger.Debug(cmd)
	if _, err := utils.ExecCmd(scriptFile, cmd); err == nil {
		return nil
	} else {
		utils.Logger.Debug(err.Error())
	}

	cmd = `net use Z: \\osinstall\image`
	utils.Logger.Debug(cmd)
	if _, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
		return err
	}

	return nil
}
コード例 #5
0
ファイル: main.go プロジェクト: idcos/osinstall-server
func reboot() error {
	var cmd = `wpeutil reboot`
	utils.Logger.Debug(cmd)
	if _, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
		return err
	}

	return nil
}
コード例 #6
0
ファイル: main.go プロジェクト: idcos/osinstall-server
func copyFirstBoot() error {
	var cmd = `xcopy /s /e /y /i Z:\windows\firstboot C:\firstboot`
	utils.Logger.Debug(cmd)
	if _, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
		return err
	}

	return nil
}
コード例 #7
0
ファイル: main.go プロジェクト: idcos/osinstall-server
// 修改注册表
func changeReg() error {
	var cmd1 = `reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t reg_sz /d 0 /f`
	utils.Logger.Debug(cmd1)
	if output, err := utils.ExecCmd(scriptFile, cmd1); err != nil {
		utils.Logger.Error(err.Error())
		return nil
	} else {
		utils.Logger.Debug(string(output))
	}

	var cmd2 = `reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v Defaultpassword /t reg_sz /d "" /f`
	utils.Logger.Debug(cmd2)
	if output, err := utils.ExecCmd(scriptFile, cmd2); err != nil {
		utils.Logger.Error(err.Error())
		return nil
	} else {
		utils.Logger.Debug(string(output))
	}
	return nil
}
コード例 #8
0
ファイル: main.go プロジェクト: idcos/osinstall-server
// 重启
func reboot() error {
	var cmd = fmt.Sprintf(`shutdown -f -r -t 10`)
	utils.Logger.Debug(cmd)
	if output, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
		return nil
	} else {
		utils.Logger.Debug(string(output))
	}

	return nil
}
コード例 #9
0
ファイル: main.go プロジェクト: idcos/osinstall-server
// 修改DNS
func changeDNS(nic, dns string) error {
	var cmd = fmt.Sprintf(`netsh interface ipv4 set dnsservers name="%s" static %s primary`, nic, dns)
	enc := mahonia.NewEncoder("gbk")
	cmd = enc.ConvertString(cmd)
	utils.Logger.Debug(cmd)
	if output, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
		return nil
	} else {
		utils.Logger.Debug(string(output))
	}

	return nil
}
コード例 #10
0
ファイル: main.go プロジェクト: idcos/osinstall-server
// 修改 IP
func changeIP(nic, ip, netmask, gateway string) error {
	var cmd = fmt.Sprintf(`netsh interface ipv4 set address name="%s" source=static addr=%s mask=%s gateway=%s`, nic, ip, netmask, gateway)
	enc := mahonia.NewEncoder("gbk")
	cmd = enc.ConvertString(cmd)
	utils.Logger.Debug(cmd)
	if output, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
		return nil
	} else {
		utils.Logger.Debug(string(output))
	}

	return nil
}
コード例 #11
0
ファイル: main.go プロジェクト: idcos/osinstall-server
// 网卡名称
func getNicInterfaceIndex(mac string) string {
	var cmd = fmt.Sprintf(`wmic nic where (MACAddress="%s" AND netConnectionStatus=2) get InterfaceIndex /value`, mac)
	var r = `InterfaceIndex=(.*)`
	var output string
	utils.Logger.Debug(cmd)
	if outputBytes, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
	} else {
		enc := mahonia.NewDecoder("gbk")
		output = enc.ConvertString(string(outputBytes))
		utils.Logger.Debug(output)
	}

	reg := regexp.MustCompile(r)
	var regResult = reg.FindStringSubmatch(output)
	if regResult == nil || len(regResult) != 2 {
		return ""
	}
	utils.Logger.Info("Nic Interface Index:" + regResult[1])
	// fmt.Println(strings.Trim(regResult[1], "\r\n"))
	return regResult[1]
}
コード例 #12
0
ファイル: main.go プロジェクト: idcos/osinstall-server
// 获取Mac地址
func getMacAddress() string {
	var cmd = `wmic nic where "NetConnectionStatus=2" get MACAddress /VALUE`
	var r = `(?i)MACAddress=(\S+)`
	var output string
	utils.Logger.Debug(cmd)
	if outputBytes, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
	} else {
		output = string(outputBytes)
		utils.Logger.Debug(output)
	}

	reg := regexp.MustCompile(r)
	var regResult = reg.FindStringSubmatch(output)
	if regResult == nil || len(regResult) != 2 {
		return ""
	}

	var result string
	result = strings.Trim(regResult[1], "\r\n")
	result = strings.TrimSpace(result)
	return result
}
コード例 #13
0
ファイル: main.go プロジェクト: idcos/osinstall-server
//是否是虚拟机
func isVirtualMachine() bool {
	var cmd = `systeminfo`
	var output string
	utils.Logger.Debug(cmd)
	if outputBytes, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
	} else {
		output = string(outputBytes)
		utils.Logger.Debug(output)
	}

	isValidate, err := regexp.MatchString(`(?i)VMware|VirtualBox|KVM|Xen|Parallels`, output)
	if err != nil {
		utils.Logger.Error(err.Error())
		return false
	}

	if isValidate {
		return true
	} else {
		return false
	}
}
コード例 #14
0
ファイル: main.go プロジェクト: idcos/osinstall-server
// DNS
func getDNS() string {
	var cmd = `echo | nslookup`
	var r = `Address:[:blank:]*(.+)`
	var output string
	utils.Logger.Debug(cmd)
	if outputBytes, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
	} else {
		enc := mahonia.NewDecoder("gbk")
		output = enc.ConvertString(string(outputBytes))
		utils.Logger.Debug(output)
	}

	reg := regexp.MustCompile(r)
	var regResult = reg.FindStringSubmatch(output)
	if regResult == nil || len(regResult) != 2 {
		return ""
	}

	var dns = strings.TrimSpace(regResult[1])
	// fmt.Println(dns)
	return dns
}
コード例 #15
0
ファイル: main.go プロジェクト: idcos/osinstall-server
// 查看本机 SN
func getSN() string {
	var cmd = `wmic bios get SerialNumber /VALUE`
	var r = `SerialNumber=(\S+)`
	var output string
	utils.Logger.Debug(cmd)
	if outputBytes, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
	} else {
		output = string(outputBytes)
		utils.Logger.Debug(output)
	}

	reg := regexp.MustCompile(r)
	var regResult = reg.FindStringSubmatch(output)
	if regResult == nil || len(regResult) != 2 {
		return ""
	}

	// fmt.Println(strings.Trim(regResult[1], "\r\n"))
	var result string
	result = strings.Trim(regResult[1], "\r\n")
	result = strings.TrimSpace(result)
	return result
}
コード例 #16
0
ファイル: main.go プロジェクト: idcos/osinstall-server
//get domain's lookup ip
func getDomainLookupIP(domain string) string {
	var cmd = `ping ` + domain
	var r = `(.+)(\s)(\d+)\.(\d+)\.(\d+)\.(\d+)([:|\s])(.+)TTL`
	var output string
	utils.Logger.Debug(cmd)
	if outputBytes, err := utils.ExecCmd(scriptFile, cmd); err != nil {
		utils.Logger.Error(err.Error())
	} else {
		output = string(outputBytes)
		utils.Logger.Debug(output)
	}

	reg := regexp.MustCompile(r)
	var regResult = reg.FindStringSubmatch(output)
	if regResult == nil || len(regResult) != 9 {
		return ""
	}

	var result = fmt.Sprintf("%s.%s.%s.%s", strings.TrimSpace(regResult[3]),
		strings.TrimSpace(regResult[4]),
		strings.TrimSpace(regResult[5]),
		strings.TrimSpace(regResult[6]))
	return result
}