-
Notifications
You must be signed in to change notification settings - Fork 3
/
sorah-is-network-stable
executable file
·42 lines (32 loc) · 1.06 KB
/
sorah-is-network-stable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env ruby
unless RUBY_PLATFORM =~ /darwin/
exit 0
end
stable_wifi_aps = %w(sorah soraher)
if File.exist?(File.expand_path('~/.sorah_stable_aps'))
stable_wifi_aps.concat File.read(File.expand_path('~/.sorah_stable_aps')).each_line.map(&:chomp)
end
list = `networksetup -listallhardwareports`
active_iface_raw = `route get default`.match(/^\s*interface:\s*(.+)$/)[1]
iface_to_name = Hash[list.scan(/Hardware Port: (.+)\nDevice: (.+)\n/).map { |(name,iface)| [iface, name] }]
aliases = Hash[list.scan(/Parent Device: (.+)\nDevice (?:\(.+\))?: (.+)\n/).map { |(parent,iface)| [iface, parent] }]
active_iface = aliases[active_iface_raw] || active_iface_raw
if active_iface.start_with?('ppp') # VPN
exit 1
end
wifi_ap = begin
match = IO.popen(['networksetup', '-getairportnetwork', active_iface], 'r', &:read).
match(/^Current Wi-Fi Network: (.+)$/)
match && match[1]
end
if wifi_ap
exit stable_wifi_aps.include?(wifi_ap) ? 0 : 1
else
case iface_to_name[active_iface]
when /Bluetooth/i
exit 1
when /iPhone|iPad/i
exit 1
end
exit 0
end