From af4923c98aa644f3b15fc977f44593f12b35babc Mon Sep 17 00:00:00 2001 From: Marcel Haazen Date: Mon, 22 Oct 2018 16:59:45 +0200 Subject: [PATCH] Added the Network Monitor Code --- monitor.cr | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 monitor.cr diff --git a/monitor.cr b/monitor.cr new file mode 100644 index 0000000..96a08f7 --- /dev/null +++ b/monitor.cr @@ -0,0 +1,27 @@ +# Kitten Info +# Required Modules +require "http/client" + +# Network Status +client = HTTP::Client.new("google.com") + +# Set Timeouts to 1.5 seconds +client.connect_timeout = 1.5 +client.dns_timeout = 1.5 + +begin + response = client.get("/") + network = "Connected" +rescue Socket::Error + # Disconnect due to DNS error and usually no network access + network = "Disconnected" +rescue IO::Timeout + # Network Timeout + network = "Timeout" +rescue errno + # Disconnect due unreachable host + network = "Disconnected" +end + +#Print Information +puts "Internet: " + network \ No newline at end of file