Example #1
0
File: route6.go Project: novag/wok
func CRoute6(channel string, sender string, arg string) {
	status := 0

	lg, err := net.Dial("tcp", bot.GetLGServer())
	if err != nil {
		bot.Noticef(channel, "route6 %s: Error", arg)
		return
	}
	fmt.Fprintf(lg, "route6 %s\n", arg)

	for {
		res, err := bufio.NewReader(lg).ReadString('\n')
		if err == io.EOF {
			lg.Close()
			break
		}
		if strings.Contains(res, "via") {
			status = 1
			arg = res[:strings.Index(res, " ")]
			break
		}
	}
	if status == 1 {
		bot.Noticef(channel, "route6 %s: Network in table", arg)
	} else {
		bot.Noticef(channel, "route6 %s: Network not in table", arg)
	}
}
Example #2
0
File: trace.go Project: novag/wok
func CTrace(channel string, sender string, arg string) {
	status := 0
	var hops int

	lg, err := net.Dial("tcp", bot.GetLGServer())
	if err != nil {
		bot.Noticef(channel, "traceroute %s: Error", arg)
		return
	}
	fmt.Fprintf(lg, "traceroute %s\n", arg)

	buf := make([]byte, 1024)
	n, err := io.ReadFull(lg, buf)
	if err == io.EOF {
		lg.Close()
		return
	}
	strbuf := string(buf[:n])

	if strings.Contains(strbuf, "unreachable") {
		status = 1
	} else if strings.Contains(strbuf, "Name or service") {
		status = 2
	} else if strings.Contains(strbuf, "traceroute to") {
		if strings.Contains(strbuf, "*") {
			status = 3
		}
		hops = len(strings.Split(strbuf, "\n")) - 2
	} else {
		status = 4
	}
	if status == 1 {
		bot.Noticef(channel, "traceroute %s: Unreachable", arg)
	} else if status == 2 {
		bot.Noticef(channel, "traceroute %s: Name or service not known", arg)
	} else if status == 4 {
		bot.Noticef(channel, "traceroute %s: Unknown error occurred", arg)
	} else {
		if hops == 1 {
			bot.Noticef(channel, "traceroute %s: 1 hop", arg)
		} else if hops == 30 {
			bot.Noticef(channel, "traceroute %s: 30+ hops", arg)
		} else if status == 3 {
			bot.Noticef(channel, "traceroute %s: %d hops (Timeouts occured)", arg, hops)
		} else {
			bot.Noticef(channel, "traceroute %s: %d hops", arg, hops)
		}
	}
}
Example #3
0
File: ping.go Project: novag/wok
func CPing(channel string, sender string, arg string) {
	status := 0
	var average float64

	lg, err := net.Dial("tcp", bot.GetLGServer())
	if err != nil {
		bot.Noticef(channel, "ping %s: Error", arg)
		return
	}
	fmt.Fprintf(lg, "ping %s\n", arg)

	buf := make([]byte, 1024)
	n, err := io.ReadFull(lg, buf)
	if err == io.EOF {
		lg.Close()
		return
	}
	strbuf := string(buf[:n])

	if strings.Contains(strbuf, "unreachable") {
		status = 1
	} else if strings.Contains(strbuf, "Time to live exceeded") {
		status = 2
	} else if strings.Contains(strbuf, "statistics") {
		re := regexp.MustCompile(`time=(\d+\.?\d*)`)
		matches := re.FindAllStringSubmatch(strbuf, -1)
		if matches != nil {
			for _, ms := range matches[1:] {
				fms, _ := strconv.ParseFloat(ms[1], 64)
				average += fms
			}
			average = average / float64(len(matches[1:]))
		} else {
			status = 3
		}
	} else {
		status = 4
	}
	if status == 1 {
		bot.Noticef(channel, "ping %s: Unreachable", arg)
	} else if status == 2 {
		bot.Noticef(channel, "ping %s: Time to live exceeded", arg)
	} else if status == 3 {
		bot.Noticef(channel, "ping %s: 100%% packet loss", arg)
	} else if status == 4 {
		bot.Noticef(channel, "ping %s: Unknown error occurred", arg)
	} else {
		bot.Noticef(channel, "ping %s: %.1f ms", arg, average)
	}
}
Example #4
0
File: whois.go Project: novag/wok
func CWhois(channel string, sender string, arg string) {
	status := 0

	lg, err := net.Dial("tcp", bot.GetLGServer())
	if err != nil {
		bot.Noticef(channel, "whois %s: Error", arg)
		return
	}
	fmt.Fprintf(lg, "whois %s\n", arg)

	buf := make([]byte, 3072)
	n, err := io.ReadFull(lg, buf)
	if err == io.EOF {
		lg.Close()
		return
	}
	strbuf := string(buf[:n])

	if strings.Contains(strbuf, "No match found") {
		status = 1
	} else if strings.Contains(strbuf, "dns/") {
		re_admin := regexp.MustCompile(`admin-c:\s+([\w-]+)`)
		re_status := regexp.MustCompile(`status:\s+([\w\s#:']+)`)
		admin_matches := re_admin.FindAllStringSubmatch(strbuf, -1)
		status_matches := re_status.FindAllStringSubmatch(strbuf, -1)
		if admin_matches != nil && status_matches != nil {
			bot.Noticef(channel, "whois %s: Admin-C: %s, Status: %s", arg, admin_matches[len(admin_matches)-1][1], status_matches[len(status_matches)-1][1])
			return
		} else {
			status = 2
		}
	} else if strings.Contains(strbuf, "inetnum/") {
		re_admin := regexp.MustCompile(`admin-c:\s+([\w-]+)`)
		re_status := regexp.MustCompile(`bgp-status:\s+([\w\s#:']+)`)
		admin_matches := re_admin.FindAllStringSubmatch(strbuf, -1)
		status_matches := re_status.FindAllStringSubmatch(strbuf, -1)
		if admin_matches != nil && status_matches != nil {
			bot.Noticef(channel, "whois %s: Admin-C: %s, BGP: %s", arg, admin_matches[len(admin_matches)-1][1], status_matches[len(status_matches)-1][1])
			return
		} else {
			status = 2
		}
	} else if strings.Contains(strbuf, "person/") {
		re_email := regexp.MustCompile(`e-mail:\s+([\w@\.-]+)`)
		re_contact := regexp.MustCompile(`contact:\s+(.+)`)
		email_matches := re_email.FindAllStringSubmatch(strbuf, -1)
		contact_matches := re_contact.FindAllStringSubmatch(strbuf, -1)
		if email_matches != nil && contact_matches == nil {
			bot.Noticef(channel, "whois %s: E-Mail: %s", arg, email_matches[len(email_matches)-1][1])
			return
		} else if contact_matches != nil && email_matches == nil {
			bot.Noticef(channel, "whois %s: Contact: %s", arg, contact_matches[len(contact_matches)-1][1])
			return
		} else if contact_matches != nil && email_matches != nil {
			bot.Noticef(channel, "whois %s: E-Mail: %s, Contact: %s", arg, email_matches[len(email_matches)-1][1], contact_matches[len(contact_matches)-1][1])
			return
		} else {
			status = 2
		}
	} else if strings.Contains(strbuf, "aut-num/") {
		re := regexp.MustCompile(`admin-c:\s+([\w-]+)`)
		matches := re.FindAllStringSubmatch(strbuf, -1)
		if matches != nil {
			bot.Noticef(channel, "whois %s: Admin-C: %s", arg, matches[len(matches)-1][1])
			return
		} else {
			status = 2
		}
	} else {
		status = 2
	}
	if status == 1 {
		bot.Noticef(channel, "whois %s: No match found", arg)
	} else if status == 2 {
		bot.Noticef(channel, "whois %s: No channel preview available, please query me", arg)
	}
}