Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "os/exec"
- "runtime" // Importa il pacchetto runtime per rilevare il sistema operativo
- "sync"
- )
- func executePing(host string, wg *sync.WaitGroup) bool {
- defer wg.Done() // Decrementa il contatore del WaitGroup quando la goroutine termina
- var cmd *exec.Cmd
- // Adatta il comando ping in base al sistema operativo
- if runtime.GOOS == "windows" {
- // Per Windows: -n 1 (count), -w 1000 (timeout in ms)
- cmd = exec.Command("ping", "-n", "1", "-w", "1000", host)
- } else {
- // Per Linux/macOS: -c 1 (count), -W 1 (timeout in secondi)
- cmd = exec.Command("ping", "-c", "1", "-W", "1", host)
- }
- err := cmd.Run()
- return err == nil
- }
- func main() {
- // Lista di host affidabili per i ping, più alcuni per dimostrare i fallimenti
- hosts := []string{
- "8.8.8.8",
- "1.1.1.1",
- "github.com",
- "example.com",
- "nonexistent.domain",
- "192.0.2.1",
- }
- fmt.Println("--- Monitor di Ping ---")
- var wg sync.WaitGroup
- // Avvia le goroutine
- for _, host := range hosts {
- wg.Add(1)
- go func(h string) { // h è una copia del valore di host per evitare race conditions
- if executePing(h, &wg) {
- fmt.Printf("Ping di %s: SUCCESS\n", h)
- } else {
- fmt.Printf("Ping di %s: FAILED\n", h)
- }
- }(host)
- }
- wg.Wait()
- fmt.Println("\nTutti i ping sono stati elaborati e il programma è terminato.")
- }
- //SECONDO MAIN, CON CHANNEL
- package main
- import (
- "fmt"
- "os/exec"
- "runtime" // Importa il pacchetto runtime per rilevare il sistema operativo
- "sync"
- )
- // PingResult struttura per contenere il risultato del ping.
- type PingResult struct {
- Host string
- Success bool
- Error error
- }
- // executePing esegue un comando ping e invia il risultato a un channel.
- func executePing(host string, results chan<- PingResult, wg *sync.WaitGroup) {
- defer wg.Done() // Decrementa il contatore del WaitGroup quando la goroutine termina
- var cmd *exec.Cmd
- // Adatta il comando ping in base al sistema operativo
- if runtime.GOOS == "windows" {
- // Per Windows: -n 1 (count), -w 1000 (timeout in ms)
- cmd = exec.Command("ping", "-n", "1", "-w", "1000", host)
- } else {
- // Per Linux/macOS: -c 1 (count), -W 1 (timeout in secondi)
- cmd = exec.Command("ping", "-c", "1", "-W", "1", host)
- }
- err := cmd.Run()
- result := PingResult{
- Host: host,
- Success: err == nil,
- Error: err,
- }
- results <- result // Invia il risultato al channel
- }
- func main() {
- // Lista di host affidabili per i ping, più alcuni per dimostrare i fallimenti
- hosts := []string{
- "8.8.8.8", // Google Public DNS (molto affidabile)
- "1.1.1.1", // Cloudflare Public DNS (molto affidabile)
- "github.com", // Di solito risponde
- "example.com", // Un dominio di esempio che quasi sempre risponde
- "nonexistent.domain", // Questo dovrebbe fallire
- "192.0.2.1", // Indirizzo IP riservato per la documentazione, dovrebbe fallire
- }
- resultsChan := make(chan PingResult) // Canale per inviare PingResult
- var wg sync.WaitGroup // Dichiarazione del WaitGroup
- fmt.Println("--- Monitor di Ping ---")
- // Avvia le goroutine
- for _, host := range hosts {
- wg.Add(1) // Incrementa il contatore del WaitGroup per ogni goroutine lanciata
- go executePing(host, resultsChan, &wg)
- }
- // Goroutine separata per chiudere il channel una volta che tutte le goroutine sono terminate.
- go func() {
- wg.Wait() // Blocca finché il contatore del WaitGroup non torna a zero
- close(resultsChan) // Chiude il channel, segnalando che non ci saranno più invii
- }()
- // Ricevi i risultati dal channel usando `range`.
- for result := range resultsChan {
- if result.Success {
- fmt.Printf("Ping di %-18s: SUCCESS\n", result.Host) // Formattazione per allineare
- } else {
- fmt.Printf("Ping di %-18s: FAILED (%v)\n", result.Host, result.Error)
- }
- }
- fmt.Println("\nTutti i ping sono stati elaborati e il programma è terminato.")
- }
Advertisement
Add Comment
Please, Sign In to add comment