Skip to content

Instantly share code, notes, and snippets.

@henri
Last active September 24, 2021 04:22
Show Gist options
  • Save henri/a04732404b9173f8537844014b258970 to your computer and use it in GitHub Desktop.
Save henri/a04732404b9173f8537844014b258970 to your computer and use it in GitHub Desktop.
Enable Only Specific Network Interfaces (macOS)
#!/usr/bin/env ruby
# this script will enable only the specified network interface
# a list of interfaces which were enabled will be recorded into
# the file /tmp/network_interfaces_[datestamp].txt
# released under the GNU GPL version 3 or later
# copyright henri shustak 2021
# See URL below for information on setting up networksetup access for non admin users
# https://github.com/henri/handy-alias/wiki/Network-Service-Order-%3A-Mac-OS-X
# version 1.0 - intitial version
# version 1.1 - improved compatability with more recent versions of macOS around security.
# version 1.2 - bug fix
# version 1.2 - added comment support when loading a file
@SHOW_PATH_TO_SAVED_INTERFACES = true
@ACTIVE_INTERFACES = []
@JUST_LIST_INTERFACES = false
req_arg_num = 1
if ARGV[0] == "-l" then
req_arg_num = 2
end
if ARGV[0] == "-i" then
@JUST_LIST_INTERFACES = true
end
if ( ARGV.length < req_arg_num.to_i || ARGV.length > req_arg_num.to_i ) then
puts "Sorry, too few or two many arguments :("
puts " usage examples : "
puts " enable single interface : networkonly.ruby <network-to-keep-running>"
puts " enable interfaces listed in a file : networkonly.ruby -l <file-to-load-interfaces-from.txt>"
puts " show names of availible interfaces : networkonly.ruby -i"
exit
end
def check_if_network_interface_present(nip)
if `sudo /usr/sbin/networksetup -listallnetworkservices | tail -n +2 | tr -d "*" | grep "#{nip}" >> /dev/null ; echo $?`.chomp == "0" then
return 0
else
puts "The network interface \"#{nip.to_s}\" was not found on this system."
exit -10
end
end
def active_interfaces_update
active_interfaces_raw = `sudo /usr/sbin/networksetup -listallnetworkservices | grep -v "*"`
@ACTIVE_INTERFACES = active_interfaces_raw.split("\n")
end
def is_only_active_interface(interface_name)
active_interfaces_update
if @ACTIVE_INTERFACES.size == 1 then
if @ACTIVE_INTERFACES[0].to_s == interface_name.to_s then return true
else return false end else return false
end
end
def is_only_active_interface_with_exit(interface_name)
if is_only_active_interface("#{interface_name}") != true
puts " Requested interface is not the only active interface."
exit -1
end
end
def enable_interface(interface_name)
`networksetup -setnetworkserviceenabled "#{interface_name}" on`
puts `echo $?`
end
def make_active_only(make_active_array)
# check that all interfaces requested to be active exist
make_active_array.each do | interface |
check_if_network_interface_present(interface)
end
# build list avaible interfaces (we can turn off or on)
avalible_interfaces_raw = `sudo /usr/sbin/networksetup -listallnetworkservices | tail -n +2 | tr -d "*"`
availble_interfaces = avalible_interfaces_raw.split("\n")
# make only the - make_active_array - interface(s) active
availble_interfaces.each do | interface |
if not make_active_array.include?("#{interface}") then
# turn off the network interface
`sudo /usr/sbin/networksetup -setnetworkserviceenabled "#{interface}" off`
puts "turning off interface : #{interface}"
else
# turn on the network interface
`sudo /usr/sbin/networksetup -setnetworkserviceenabled "#{interface}" on`
puts "turning on interface : #{interface}"
end
end
end
#just show the available interfaces and exit
if @JUST_LIST_INTERFACES then
availible_interfaces_raw = `sudo /usr/sbin/networksetup -listallnetworkservices | tail -n +2 | tr -d "*"`
puts ""
puts availible_interfaces_raw
puts ""
exit 0
end
date_stamp = `date "+%Y-%m-%d_%H-%M-%S"`.chomp
active_interfaces_raw = `sudo /usr/sbin/networksetup -listallnetworkservices | grep -v "*" | tee "/tmp/network_interfaces_#{date_stamp}.txt"`
active_interfaces = active_interfaces_raw.split("\n")
if @SHOW_PATH_TO_SAVED_INTERFACES then
puts ""
puts "active interfaces saved to : /tmp/network_interfaces_#{date_stamp}.txt"
puts ""
end
if req_arg_num.to_i == 1 then
# just turning things off
keep_on = ARGV[0]
make_active_only([keep_on.to_s])
active_interfaces_update
if is_only_active_interface("#{keep_on}") != true
puts "Some other interfaces are still active!"
exit -1
end
else
# load files and making them active and making everything else inactive
interfaces_to_activate = []
File.open("#{ARGV[1]}") do |load_file|
load_file.each_line do |line|
if ! line.strip.start_with?("#") then
file_line = line.chomp.split('#')
to_activate = file_line.first.strip
puts to_activate.inspect
interfaces_to_activate << to_activate.chomp
end
end
end
make_active_only(interfaces_to_activate)
end
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment