Exemplo n.º 1
0
func init() {
	//如果没有日志目录则创建日志目录
	_, err := os.Open("log")
	if err != nil && os.IsNotExist(err) {
		os.Mkdir("log", 0777)
	}
	//初始化日志
	ApiLog = logs.NewLogger(10000)
	ApiLog.SetLogger("file", `{"filename":"log/api.log"}`)
	//初始化邮箱
	mail = utils.NewEMail(`{"username":"******","password":"******"EmailPassword") + `","host":"smtp.isletsacg.com","port":25}`)
}
func SendTextMail(subject string, content string, to []string) {

	config, err := mailConfig()
	if nil != err {
		return
	}

	mail := utils.NewEMail(config)
	mail.Subject = subject
	mail.Text = content
	mail.To = to

	mail.Send()
}
func SendHtmlMail(subject string, content string, to []string) {

	beego.Info("content", content)
	config, err := mailConfig()
	if nil != err {
		return
	}

	mail := utils.NewEMail(config)
	mail.Subject = subject
	mail.HTML = content
	mail.To = to
	mail.Send()
}
Exemplo n.º 4
0
func init() {
	//如果没有日志目录则创建日志目录
	_, err := os.Open("log")
	if err != nil && os.IsNotExist(err) {
		os.Mkdir("log", 0777)
	}
	//初始化日志
	ApiLog = logs.NewLogger(10000)
	ApiLog.SetLogger("file", `{"filename":"log/api.log"}`)
	//初始化邮箱
	mail = utils.NewEMail(`{"username":"******"EmailAccount") + `","password":"******"EmailPassword") + `","host":"` + beego.AppConfig.String("EmailSmtp") + `","port":` + beego.AppConfig.String("EmailPort") + `}`)
	//初始化又拍云
	u = upyun.NewUpYun("wokugame", beego.AppConfig.String("UpyunAccount"), beego.AppConfig.String("UpyunPassword"))
	u.SetApiDomain("v0.api.upyun.com")
}
Exemplo n.º 5
0
func SendMail(address, name, subject, content string, cc []string) bool {
	mail := utils.NewEMail(config)
	mail.To = []string{address}
	mail.Subject = subject
	mail.HTML = content
	if len(cc) > 0 {
		mail.Cc = cc
	}

	select {
	case sendCh <- mail:
		return true
	case <-time.After(time.Second * 3):
		return false
	}
}
Exemplo n.º 6
0
Arquivo: mssh.go Projeto: rprp/mssh
func main() {
	flag.Usage = usage
	flag.Parse()

	//	SetLog()
	//	defer log4go.Close()

	args := flag.Args()
	argnums := flag.NArg()

	if flag.NArg() < 1 && flag.NFlag() < 1 {
		usage()
	}

	if argnums >= 1 {
		switch args[0] {
		case "help":
			usage()
		case "version":
			version()
			os.Exit(2)
		case "&", "|", "<", ">":

		default:
			if args[argnums-1] == "&" {
				args = args[:argnums-1]
			}

		}
	}

	var err error

	if *infile == "" {
		fmt.Println("please input your infile which included 'hostaddr username password command' one per line")
		usage()
	} else if !com.IsExist(*infile) {
		fmt.Printf("not found input file: %s\n", *infile)
		usage()
	}

	if *cfg == "" {
		gCfg = "./mssh.conf"
	} else {
		gCfg = *cfg
	}

	if !parseconf(gCfg) {
		fmt.Printf("init conf file %s failed, exit\n", *cfg)
		os.Exit(2)
	}

	if *cmd != "" {
		if len(args) > 0 {
			gCommand = fmt.Sprintf("%s %s", *cmd, strings.Join(args, " "))
		} else {
			if *shellmode {
				curpath, _ := os.Getwd()
				if !strings.Contains(*cmd, "/") {
					*cmd = curpath + "/" + *cmd
				}
				gCommand = fmt.Sprintf("%s %s", "/bin/bash", *cmd)
			} else {
				gCommand = *cmd
			}
		}
	}

	gCommand = helper.Abs(gCommand)
	vprintf("gCommnad: %s\n", gCommand)
	vprintf("gUsername: %s\n", gUsername)
	vprintf("gPassword: %s\n", gPassword)
	vprintf("gTimeout: %v\n", gTimeout)

	sshobjs, err := parse(*infile)
	if err != nil {
		log.Fatal(err)
	}
	vprintf("%#v\n", sshobjs)

	if *n < 1 || *n > 10000 {
		*n = runtime.NumCPU()
	}

	cmdout, _, err := com.ExecCmd("logname")
	vprintf("cmdout: %s", cmdout)
	if err == nil {
		cmdout = strings.TrimSpace(cmdout)
		if len(gMaillist) == 0 {
			gMaillist = fmt.Sprintf("*****@*****.**", cmdout)
		}
	}
	vprintf("gMaillist: %s\n", gMaillist)

	//	basename := filepath.Base(*cmd)
	var r = []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_")
	randomfname := string(helper.RandomCreateBytes(32, r...)) + ".sh"
	vprintf("randomfname: %s\n", randomfname)

	chFirst := make(chan SshClient, *n)
	//	chSecond := make(chan SshClient, *n)
	oldtime := time.Now()
	scpTout := fmt.Sprintf("ConnectTimeout=%d", gTimeout)

	go func() {
		defer close(chFirst)
		for i, _ := range sshobjs {
			if *israndompwd {
				sshobjs[i].Cmd = fmt.Sprintf("echo %s | passwd --stdin %s", sshobjs[i].Password, sshobjs[i].Username)
				vprintf("%#v\n", sshobjs[i])
			} else if *shellmode {

				com.ExecCmd("scp", "-o", scpTout, "-o", "BatchMode=yes", "-r", *cmd, fmt.Sprintf("%s:/tmp/%s", helper.GetHost(sshobjs[i].Hostaddr, ":"), randomfname))
				sshobjs[i].Cmd = fmt.Sprintf("/bin/bash /tmp/%s && /bin/rm -f /tmp/%s", randomfname, randomfname)
				vprintf("%#v\n", sshobjs[i])
			}
			chFirst <- sshobjs[i]
		}
	}()

	wg1 := new(sync.WaitGroup)
	wg2 := new(sync.WaitGroup)
	tryslice := make([]SshClient, 0)
	sem := make(chan struct{}, *n)
	mux := new(sync.Mutex)

	for i := 0; i < *n; i++ {
		wg1.Add(1)
		go func(ch chan SshClient) {
			defer wg1.Done()

			for v := range ch {
				vprintf("%#v\n", v)
				func(sshitem SshClient) {
					err, _, stdout, _ := sshutils.SshExec(sshitem.Hostaddr, sshitem.Username, sshitem.Password, sshitem.Cmd, gTimeout)
					if err != nil {
						mux.Lock()
						tryslice = append(tryslice, sshitem)
						mux.Unlock()
						return
					}

					mux.Lock()
					cnt++
					fmt.Println(strings.Repeat("*", 40), "[", cnt, "]", strings.Repeat("*", 40))
					fmt.Println(fmt.Sprintf("%s:\n%s\n", sshitem.Hostaddr, stdout))
					mux.Unlock()
				}(v)
			}
		}(chFirst)
	}

	wg1.Wait()

	//for /usr/bin/ssh execute shell cmds

	buf.WriteString("\nrunning results(error host lists):\n\n")

	for _, v := range tryslice {
		sem <- struct{}{}
		wg2.Add(1)
		go func(sshitem SshClient) {
			defer wg2.Done()
			err, _, stdout, _ := sshutils.SshCmdExec(sshitem.Hostaddr, sshitem.Username, sshitem.Password, sshitem.Cmd, gTimeout)
			if err != nil {
				log.Println("[/usr/bin/ssh]", sshitem.Hostaddr, err)
				//				log4go.Error("[/usr/bin/ssh] %s %v", sshitem.Hostaddr, err)
				buf.WriteString(sshitem.Hostaddr + "\n")
				<-sem
				return
			}

			mux.Lock()
			cnt++
			fmt.Println("[", cnt, "]", strings.Repeat("*", 80))
			fmt.Println(fmt.Sprintf("%s %s:\n%s\n", "[/usr/bin/ssh]", sshitem.Hostaddr, stdout))
			//			log4go.Info(fmt.Sprintf("%s %s:\n%s\n", "[/usr/bin/ssh]", sshitem.Hostaddr, stdout))
			mux.Unlock()
			<-sem
		}(v)

	}

	wg2.Wait()

	//kill ssh process which is timeout
	vprintf("Kill ssh for timeout\n")
	kill(sshutils.Cmds)

	errMsg := buf.String()
	htmlErrMsg := strings.Replace(errMsg, "\n", "<br>", -1)
	now := time.Now().String()
	title := gTitle
	mode := gMode
	execTime := time.Since(oldtime)
	totalTime := execTime.String()
	var body string
	if mode == "html" {
		body = now + "<br>" + gBody + "<br>" + htmlErrMsg + "<br>total time: " + totalTime
	} else {
		body = now + "\n" + gBody + "\n" + errMsg + "\ntotal time: " + totalTime
	}
	maillist := []string{gMaillist}

	config := fmt.Sprintf(`{"username":"******","password":"******","host":"%s","port":%d}`, mailUsername, mailPassword, mailHost, mailPort)

	mail := utils.NewEMail(config)
	mail.To = maillist
	mail.From = gFr_addr
	mail.Subject = title

	if mode == "html" {
		mail.HTML = body
	} else {
		mail.Text = body
	}

	if *ismail {
		err = mail.Send()
		if err != nil {
			log.Println("send mail failed:", err)
			goto LEAVE
		}

		fmt.Println("send mail successful")
	}

LEAVE:
	newtime := time.Since(oldtime)
	fmt.Println("run time:", newtime)

}