#!/usr/bin/ruby -w def log( message, prefix = "" ) open( "/var/log/switch-network.log", "a" ) { |f| message.split( /\n/ ).each { |e| f.puts prefix + " " + e.rstrip } } end log( "------ switch-network run at: " + `date` ) $situations = Array.new interfaces_directory = "/etc/network/" Dir.new(interfaces_directory).entries.each do |e| if e =~ /^interfaces\.(.*[^~])$/ $situations.push $1 end end $situations.sort! def usage puts "Usage: switch-network " puts "... where is one of:" $situations.each do |s| puts " * " + s end end unless ARGV.length == 1 usage log( "Bad usage: switch-network #{ARGV.join(' ')}", '-' ) exit( -1 ) end log( "Available situations:", '-' ) $situations.each do |s| log( s, '*' ) end new_situation = ARGV[0] unless $situations.index( new_situation ) usage exit( -1 ) end log( "Switching to network: #{new_situation}", "#" ) unless system( "/sbin/ifdown", "-a" ) log "Warning: Couldn't lower all the interfaces.", '-' puts "Warning: Couldn't lower all the interfaces." end # Sometimes it seems it's better to unload the wireless modules # as well when switching network. unless new_situation == "loopback" log( "Removing ipw2200 module...", '-' ) system "/sbin/rmmod", "ipw2200" log( "Modprobing ipw2200 module...", '-' ) system "/sbin/modprobe", "ipw2200" end log( "Copying interfaces file...", '-' ) system( "/bin/cp", "#{interfaces_directory}/interfaces.#{new_situation}", "#{interfaces_directory}/interfaces" ) log( "Raising interfaces again...", '-' ) system "/sbin/ifup", "-a" log( "Done switching networks.", '-' )