<commit_before>package main

import (
	"fmt"
	"log"
	"net"
	"os/exec"
	"strings"
	"time"
)

const (
	Interface  = "en0"
	statusUp   = "UP"
	statusDown = "DOWN"
)

type Host struct {
	IP       string
	MAC      string
	Hostname string
	Data     []string
}

func (h Host) String() string {
	return fmt.Sprintf("Host %s has IP %s, MAC: %s", h.Hostname, h.IP, h.MAC)
}

func handleErr(err error) {
	log.Println("Error: ", err)
}

func getIPAndMAC(line string) (string, string) {
	lines := strings.Split(line, "(")
	tmp := strings.Split(lines[1], ")")
	IP := tmp[0]
	MAC := strings.Split(tmp[1], " ")[2]
	return IP, MAC
}

func arpEntries() []Host {
	var hosts []Host
	out, err := exec.Command("arp", "-na").Output()
	if err != nil {
		handleErr(err)
	}
	strOut := string(out)
	lines := strings.Split(strOut, "\n")
	for _, line := range lines {
		if strings.Contains(line, Interface) {
			h := Host{}
			h.IP, h.MAC = getIPAndMAC(line)
			hosts = append(hosts, h)
		}
	}
	return hosts
}

func display(hs []Host) {
	for idx, h := range hs {
		log.Printf("%d. %s", idx+1, h)
	}
	return
}

func myLANIP() (string, error) {
	ifis, err := net.Interfaces()
	if err != nil {
		handleErr(err)
	}
	for _, ifi := range ifis {
		if ifi.Name == Interface {
			addrs, err := ifi.Addrs()
			if err != nil {
				handleErr(err)
			}
			log.Println(addrs)
			for _, addr := range addrs {
				ip, _, err := net.ParseCIDR(addr.String())
				if err != nil {
					handleErr(err)
					continue
				}
				if ip.To4() != nil {
					return addr.String(), nil
				}
			}
		}
	}
	return "", err
}

func nmapScanAll() {
	myIP, err := myLANIP()
	handleErr(err)
	out, err := exec.Command("nmap", "-A", "-T4", myIP).Output()
	handleErr(err)
	log.Println(string(out))
}
func nmapEntries() {
	myIP, err := myLANIP()
	handleErr(err)
	out, err := exec.Command("nmap", "-sP", "-PA21,22,25,3389", myIP).Output()
	handleErr(err)
	log.Println(string(out))
}

type Event struct {
	When time.Time
	Up   bool
}

func (e Event) String() string {
	var s string
	if e.Up {
		s = statusUp
	} else {
		s = statusDown
	}

	return fmt.Sprintf("Event %s at %s", s, e.When)
}
func checkPing(h Host) Event {
	out, err := exec.Command("nmap", "-sP", h.IP).Output()
	if err == nil {
		if strings.Contains(string(out), "1 host up") {
			return Event{When: time.Now(), Up: true}
		}
	}
	return Event{When: time.Now(), Up: false}
}

func checkAll(hosts []Host, ch chan Event, chost chan Host) {
	for _, host := range hosts {
		ch <- checkPing(host)
		chost <- host
	}
	close(ch)
	close(chost)
}

func main() {
	hosts := arpEntries()
	ch := make(chan Event)
	chost := make(chan Host)
	go checkAll(hosts, ch, chost)
	for i := range ch {
		log.Println(<-chost, i)<commit_msg>rename channel vars<commit_after>package main

import (
	"fmt"
	"log"
	"net"
	"os/exec"
	"strings"
	"time"
)

const (
	Interface  = "en0"
	statusUp   = "UP"
	statusDown = "DOWN"
)

type Host struct {
	IP       string
	MAC      string
	Hostname string
	Data     []string
}

func (h Host) String() string {
	return fmt.Sprintf("Host %s has IP %s, MAC: %s", h.Hostname, h.IP, h.MAC)
}

func handleErr(err error) {
	log.Println("Error: ", err)
}

func getIPAndMAC(line string) (string, string) {
	lines := strings.Split(line, "(")
	tmp := strings.Split(lines[1], ")")
	IP := tmp[0]
	MAC := strings.Split(tmp[1], " ")[2]
	return IP, MAC
}

func arpEntries() []Host {
	var hosts []Host
	out, err := exec.Command("arp", "-na").Output()
	if err != nil {
		handleErr(err)
	}
	strOut := string(out)
	lines := strings.Split(strOut, "\n")
	for _, line := range lines {
		if strings.Contains(line, Interface) {
			h := Host{}
			h.IP, h.MAC = getIPAndMAC(line)
			hosts = append(hosts, h)
		}
	}
	return hosts
}

func display(hs []Host) {
	for idx, h := range hs {
		log.Printf("%d. %s", idx+1, h)
	}
	return
}

func myLANIP() (string, error) {
	ifis, err := net.Interfaces()
	if err != nil {
		handleErr(err)
	}
	for _, ifi := range ifis {
		if ifi.Name == Interface {
			addrs, err := ifi.Addrs()
			if err != nil {
				handleErr(err)
			}
			log.Println(addrs)
			for _, addr := range addrs {
				ip, _, err := net.ParseCIDR(addr.String())
				if err != nil {
					handleErr(err)
					continue
				}
				if ip.To4() != nil {
					return addr.String(), nil
				}
			}
		}
	}
	return "", err
}

func nmapScanAll() {
	myIP, err := myLANIP()
	handleErr(err)
	out, err := exec.Command("nmap", "-A", "-T4", myIP).Output()
	handleErr(err)
	log.Println(string(out))
}
func nmapEntries() {
	myIP, err := myLANIP()
	handleErr(err)
	out, err := exec.Command("nmap", "-sP", "-PA21,22,25,3389", myIP).Output()
	handleErr(err)
	log.Println(string(out))
}

type Event struct {
	When time.Time
	Up   bool
}

func (e Event) String() string {
	var s string
	if e.Up {
		s = statusUp
	} else {
		s = statusDown
	}

	return fmt.Sprintf("Event %s at %s", s, e.When)
}
func checkPing(h Host) Event {
	out, err := exec.Command("nmap", "-sP", h.IP).Output()
	if err == nil {
		if strings.Contains(string(out), "1 host up") {
			return Event{When: time.Now(), Up: true}
		}
	}
	return Event{When: time.Now(), Up: false}
}

func checkAll(hosts []Host, ec chan Event, hc chan Host) {
	for _, host := range hosts {
		ec <- checkPing(host)
		hc <- host
	}
	close(ec)
	close(hc)
}

func main() {
	hosts := arpEntries()
	eventChan := make(chan Event)
	hostChan := make(chan Host)
	go checkAll(hosts, eventChan, hostChan)
	for i := range eventChan {
		log.Println(<-hostChan, i)
{'fraction_non_alphanumeric': 0.1781450872359963, 'fraction_numerical': 0.006978879706152434, 'mean_word_length': 3.186010760953113, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6209587', 'n_tokens_mistral': 2354, 'n_tokens_neox': 2189, 'n_words': 567}