Skip to content

Instantly share code, notes, and snippets.

@til
Created May 5, 2012 13:09
Show Gist options
  • Save til/2602276 to your computer and use it in GitHub Desktop.
Save til/2602276 to your computer and use it in GitHub Desktop.
Simple script to diagnose network problems of local computer
#!/usr/bin/env ruby
#
# Simple script to diagnose network problems of the local computer:
# runs a couple of checks and aborts on first failure. This should
# help to find the cause of the problem quicker.
def headline(name)
puts
puts "--- #{name} ".ljust(80, '-')
end
def run(cmd)
puts
puts cmd
output = `#{cmd}`
puts output
if $?.success?
output
else
abort 'FAIL'
end
end
headline "Wlan"
wlan = run "iwconfig wlan0"
if wlan =~ /Link Quality=([1-9][0-9]\/\d\d)/
puts "OK, link quality #{$1}"
else
puts "No Wlan, who cares"
end
headline "Route"
routes = run "route -n"
gw = routes.split("\n").grep(/^\s*0\.0\.0\.0/).first.split(" ")[1]
puts "OK, gateway is #{gw}"
headline "Ping gateway"
run "ping -q -c 1 #{gw}"
puts "OK"
headline "Ping outside IP"
run "ping -q -c 1 141.1.1.1"
puts "OK"
headline "Nameserver configured"
resolv = run "cat /etc/resolv.conf"
puts resolv
if resolv =~ /nameserver\s+(\S+)/
nameserver = $1
puts "OK, nameserver is #{nameserver}"
else
abort 'Not found'
end
headline "Ping nameserver"
run "ping -q -c 1 #{nameserver}"
puts "OK"
headline "Ping heise"
run "ping -q -c 1 heise.de"
puts "OK"
headline "Ping tils.net"
run "ping -q -c 1 tils.net"
puts "OK"
@til
Copy link
Author

til commented May 5, 2012

Sample output:

/home/tils/bin/n

--- Wlan -----------------------------------------------------------------------

iwconfig wlan0
wlan0     IEEE 802.11abgn  ESSID:"Arrr"  
          Mode:Managed  Frequency:2.452 GHz  Access Point: 00:C0:A8:DA:51:F1   
          Bit Rate=54 Mb/s   Tx-Power=14 dBm   
          Retry  long limit:7   RTS thr:off   Fragment thr:off
          Power Management:off
          Link Quality=56/70  Signal level=-54 dBm  
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:599   Missed beacon:0

OK, link quality 56/70

--- Route ----------------------------------------------------------------------

route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.220.1   0.0.0.0         UG    303    0        0 wlan0
192.168.220.0   0.0.0.0         255.255.255.0   U     303    0        0 wlan0
OK, gateway is 192.168.220.1

--- Ping gateway ---------------------------------------------------------------

ping -q -c 1 192.168.220.1
PING 192.168.220.1 (192.168.220.1) 56(84) bytes of data.

--- 192.168.220.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 1.236/1.236/1.236/0.000 ms
OK

--- Ping outside IP ------------------------------------------------------------

ping -q -c 1 141.1.1.1
PING 141.1.1.1 (141.1.1.1) 56(84) bytes of data.

--- 141.1.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 19.735/19.735/19.735/0.000 ms
OK

--- Nameserver configured ------------------------------------------------------

cat /etc/resolv.conf
# Generated by dhcpcd from wlan0
nameserver 141.1.1.1
domain samsung.router
nameserver 192.168.220.1
# /etc/resolv.conf.tail can replace this line
# Generated by dhcpcd from wlan0
nameserver 141.1.1.1
domain samsung.router
nameserver 192.168.220.1
# /etc/resolv.conf.tail can replace this line
OK, nameserver is 141.1.1.1

--- Ping nameserver ------------------------------------------------------------

ping -q -c 1 141.1.1.1
PING 141.1.1.1 (141.1.1.1) 56(84) bytes of data.

--- 141.1.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 19.781/19.781/19.781/0.000 ms
OK

--- Ping heise -----------------------------------------------------------------

ping -q -c 1 heise.de
PING heise.de (193.99.144.80) 56(84) bytes of data.

--- heise.de ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 20.287/20.287/20.287/0.000 ms
OK

--- Ping tils.net --------------------------------------------------------------

ping -q -c 1 tils.net
PING tils.net (213.129.229.130) 56(84) bytes of data.

--- tils.net ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 43.626/43.626/43.626/0.000 ms
OK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment