Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Mar 8th, 2018 54 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. #!/bin/bash
  2.  
  3. IP="/sbin/ip"
  4.  
  5. function prepare_rt_table() {
  6.         local rttables=/etc/iproute2/rt_tables
  7.         local iface=$1
  8.         [[ "${iface}" = 'lo' ]] && return
  9.         if ! egrep -q "\s${iface}\s*"\$ $rttables; then
  10.                 idx=$(wc -l <$rttables)
  11.                 echo -e "$(( 300 + ${idx} ))\t${iface}" >> $rttables
  12.         fi
  13. }
  14.  
  15. function get_ipv4_addressses() {
  16.         local iface=$1
  17.         $IP -4 addr show dev ${iface} | sed -rn '/inet / s_^.*inet ([0-9.]*)/.*$_\1_p'
  18. }
  19.  
  20. function get_ipv6_addressses() {
  21.         local iface=$1
  22.         $IP -6 addr show dev ${iface} | sed -rn '/inet6 / s_^.*inet6 ([0-9a-f:]*)/.*$_\1_p'
  23. }
  24.  
  25. function transfer_routes_ipv4() {
  26.         local iface=$1
  27.         $IP -4 route flush table ${iface}
  28.         while read line; do
  29.                 [[ -z "$line" ]] && continue
  30.                 $IP -4 route add ${line} table ${iface}
  31.         done <<-EOF
  32.         $($IP -4 rou show table main | grep "dev ${iface}" |\
  33.                 sed -r 's_expires [^ ]*__' | sed -r 's_proto [^ ]*__' )
  34. EOF
  35. }
  36. function transfer_routes_ipv6() {
  37.         local iface=$1
  38.         $IP -6 route flush table ${iface}
  39.         while read line; do
  40.                 [[ -z "$line" ]] && continue
  41.                 $IP -6 route add ${line} table ${iface}
  42.         done <<-EOF
  43.         $($IP -6 rou show table main | grep "dev ${iface}" |\
  44.                 sed -r 's_expires [^ ]*__' | sed -r 's_proto [^ ]*__' )
  45. EOF
  46. }
  47.  
  48. function get_table_id() {
  49.         local rttables=/etc/iproute2/rt_tables
  50.         local table=$1
  51.         sed -rn "/^\s*[0-9]*\s*${table}/ s_^\s*([0-9]+)\s.*_\1_p" $rttables
  52. }
  53.  
  54. function replace_rules_ipv4 {
  55.         local iface=$1
  56.         local tableid=$(get_table_id $iface)
  57.         while $IP -4 rule show | egrep -q ^${tableid}; do
  58.                 $IP -4 rule del prio ${tableid}
  59.         done
  60.         for ipaddr in $(get_ipv4_addressses ${iface}); do
  61.                 $IP -4 rule add prio ${tableid} from ${ipaddr} table ${iface}
  62.         done
  63. }
  64.  
  65. function replace_rules_ipv6 {
  66.         local iface=$1
  67.         local tableid=$(get_table_id $iface)
  68.         while $IP -6 rule show | egrep -q ^${tableid}; do
  69.                 $IP -6 rule del prio ${tableid}
  70.         done
  71.         for ipaddr in $(get_ipv6_addressses ${iface}); do
  72.                 echo $ipaddr | egrep -q '^fe80:' && continue
  73.                 $IP -6 rule add prio ${tableid} from ${ipaddr} table ${iface}
  74.         done
  75. }
  76.  
  77. for ifpath in /sys/class/net/*; do
  78.         iface=${ifpath##*/}
  79.         [[ "${iface}" = 'lo' ]] && continue
  80.         prepare_rt_table ${iface}
  81.         transfer_routes_ipv4 ${iface}
  82.         replace_rules_ipv4 ${iface}
  83.         transfer_routes_ipv6 ${iface}
  84.         replace_rules_ipv6 ${iface}
  85. done
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top