func selfUpdate() { baseFileContent, err := kmgHttp.UrlGetContent("http://kmgtools.qiniudn.com/v1/installKmg.bash?v=" + kmgRand.MustCryptoRandToAlphaNum(16)) kmgConsole.ExitOnErr(err) baseFilePath := "/tmp/installKmg.bash" kmgFile.MustDeleteFile(baseFilePath) kmgFile.MustAppendFile(baseFilePath, baseFileContent) kmgCmd.MustRunInBash(baseFilePath) }
// 证实可用 func SetIpForwardOn() { if !kmgPlatform.IsLinux() { panic("[SetIpForwardOn] only support linux now") } kmgFile.MustWriteFile("/proc/sys/net/ipv4/ip_forward", []byte("1")) // 已经证实,多次写入不会出现任何问题. // TODO 正确解析/etc/sysctl.conf 如果后面又加一条 = 0 估计就挂了. if !bytes.Contains(kmgFile.MustReadFile("/etc/sysctl.conf"), []byte("\nnet.ipv4.ip_forward = 1")) { kmgFile.MustAppendFile("/etc/sysctl.conf", []byte("\nnet.ipv4.ip_forward = 1")) } }
func MustRpcSshCmd(ip string, cmd ...string) []byte { if len(cmd) == 0 { return []byte{} } if ip == "" { return []byte{} } cmdCombine := strings.Join(cmd, "&&") out, err := kmgCmd.CmdString("ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no root@" + ip + " " + cmdCombine).RunAndReturnOutput() logPath := "/tmp/rpcSshCmd-" + ip kmgFile.MustAppendFile(logPath, []byte(strings.Join([]string{cmdCombine, kmgTime.DefaultFormat(time.Now())}, "\n"))) kmgFile.MustAppendFile(logPath, out) if err != nil { // _, ok := err.(*exec.ExitError) // if ok { // return out // } kmgFile.MustAppendFile(logPath, []byte(err.Error())) panic(err) } return out }
func RunCmdWithPassword(cmd, password string) { cmdTpl := `#!/usr/bin/expect -f spawn %s expect "assword:" send "%s\r" interact` cmd = fmt.Sprintf(cmdTpl, cmd, password) tmpName := kmgRand.MustCryptoRandToReadableAlphaNum(5) tmpPath := "/tmp/" + tmpName kmgFile.MustAppendFile(tmpPath, []byte(cmd)) defer kmgFile.MustDelete(tmpPath) os.Setenv("HOME", kmgSys.GetCurrentUserHomeDir()) kmgCmd.CmdSlice([]string{tmpPath}).MustStdioRun() }
func log(msg string) { wMsg := fmt.Sprintf("%s %s\n", time.Now(), msg) os.Stdout.WriteString(wMsg) kmgFile.MustAppendFile(logFilePath, []byte(wMsg)) }