ip rule
) according to which traffic enters one or another routing table (for example, by source and / or by label from iptables). ## : ## iproute2, conntrack, python-yaml ## : $ git clone git://github.com/flant/netgwm.git netgwm ## ( ): $ cd netgwm && sudo make install ## , NetGWM $ sudo sh -c "echo '100 netgwm_check' >> /etc/iproute2/rt_tables" ## cron root netgwm , ## ## (, ): $ sudo crontab -e */1 * * * * /usr/lib/netgwm/newtgwm.py
## : $ sudo wget https://apt.flant.ru/apt/flant.trusty.common.list \ -O /etc/apt/sources.list.d/flant.common.list ## : $ wget https://apt.flant.ru/apt/archive.key -O- | sudo apt-key add - ## HTTPS- — , : $ sudo apt-get install apt-transport-https ## netgwm: $ sudo apt-get update && sudo apt-get install netgwm
netgwm
service, the init script of which starts as a daemon a small shell script /usr/bin/netgwm
, which, in turn, reads the value of the INTERVAL
parameter (in seconds) from the /etc/default/netgwm
file /etc/default/netgwm
) and with the specified periodicity itself calls netgwm.py
. iptables -t mangle -A PREROUTING -i eth1 -m conntrack --ctstate NEW,RELATED -j CONNMARK --set-xmark 0x1/0x3 iptables -t mangle -A PREROUTING -i eth2 -m conntrack --ctstate NEW,RELATED -j CONNMARK --set-xmark 0x2/0x3 iptables -t mangle -A PREROUTING -i eth3 -m conntrack --ctstate NEW,RELATED -j CONNMARK --set-xmark 0x3/0x3 iptables -t mangle -A PREROUTING -j CONNMARK --restore-mark --nfmask 0xffffffff --ctmask 0xffffffff iptables -t mangle -A OUTPUT -o eth1 -m conntrack --ctstate NEW,RELATED -j CONNMARK --set-xmark 0x1/0x3 iptables -t mangle -A OUTPUT -o eth2 -m conntrack --ctstate NEW,RELATED -j CONNMARK --set-xmark 0x2/0x3 iptables -t mangle -A OUTPUT -o eth3 -m conntrack --ctstate NEW,RELATED -j CONNMARK --set-xmark 0x3/0x3 iptables -t mangle -A OUTPUT -j CONNMARK --restore-mark --nfmask 0xffffffff --ctmask 0xffffffff iptables -t mangle -A POSTROUTING -o eth1 -m conntrack --ctstate NEW,RELATED -j CONNMARK --set-xmark 0x1/0x3 iptables -t mangle -A POSTROUTING -o eth2 -m conntrack --ctstate NEW,RELATED -j CONNMARK --set-xmark 0x2/0x3 iptables -t mangle -A POSTROUTING -o eth3 -m conntrack --ctstate NEW,RELATED -j CONNMARK --set-xmark 0x3/0x3 iptables -t mangle -A POSTROUTING -j CONNMARK --restore-mark --nfmask 0xffffffff --ctmask 0xffffffff
/etc/network/interfaces
on the post-up
event on the lo interface: #!/bin/bash /sbin/ip rule flush # operator 1 /sbin/ip rule add priority 8001 iif eth1 lookup main /sbin/ip rule add priority 10001 fwmark 0x1/0x3 lookup operator1 /sbin/ip rule add from 88.88.88.88 lookup operator1 # operator 2 /sbin/ip rule add priority 8002 iif eth2 lookup main /sbin/ip rule add priority 10002 fwmark 0x2/0x3 lookup operator2 /sbin/ip rule add from 99.99.99.99 lookup operator2 # operator 3 /sbin/ip rule add priority 8002 iif eth3 lookup main /sbin/ip rule add priority 10002 fwmark 0x3/0x3 lookup operator3 /sbin/ip rule add from 100.100.100.100 lookup operator3
/etc/iproute2/rt_tables
: # : 255 local 254 main 253 default 0 unspec # , ( dpkg) : 100 netgwm_check # , : 101 operator1 102 operator2 103 operator3
netgwm.py
will look for the configuration file at /etc/netgwm/netgwm.yml
, but you can override this with the -c
. Configure the utility to work: # # () . 1 - # , # . ( # ) ( ). # # /etc/iproute2/rt_tables gateways: operator1: {ip: 88.88.88.88, priority: 1} operator2: {ip: 99.99.99.99, priority: 2} operator3: {ip: 100.100.100.100, priority: 3} # «-», # ( ) «». # ( ), # netgwm , min_uptime: 900 # , netgwm # . # , , # . , netgwm # ( AND) check_sites: - 8.8.8.8 # Google public DNS - 4.2.2.2 # Verizon public DNS # netgwm # . — .. # , true, netgwm # check_all_gateways: false
/etc/netgwm/post-replace.d/*
directory will be executed. In addition, 6 command line parameters will be transferred to each file:$1
- the name of the new operator;$2
- IP of the newly installed gateway or NaN, if the new gateway could not be established;$3
- the device name of the new gateway or NaN, if the gateway could not be established;$4
- the name of the old operator or NaN, if the gateway is installed for the first time;$5
- IP of the old operator or NaN, if the gateway is installed for the first time;$6
- the device name of the old operator or NaN, if the gateway is being installed for the first time. #!/bin/bash # , : netgwm if [ "$4" = 'NaN' ] && [ "$5" = 'NaN' ] then STATE='start' else STATE='switch' fi # case $STATE in 'start') /usr/bin/flant-integration --sms-send="NetGWM on ${HOSTNAME} has been started and now use gw: $1 - $2" ;; 'switch') /usr/bin/flant-integration --sms-send="NetGWM on ${HOSTNAME} has switched to new gw: $1 - $2 from gw: $4 - $5" ;; *) /usr/bin/logger -t netgwm "Unknown NetGWM state. Try restarting service fo fix it." ;; esac exit
netgwm
service in Ubuntu, if you installed the deb package: $ sudo service netgwm start
/var/log/netgwm
: $ tail -n 3 /var/log/netgwm.log 2017-07-14 06:25:41,554 route replaced to: via 88.88.88.88 2017-07-14 06:27:09,551 route replaced to: via 99.99.99.99 2017-07-14 07:28:48,573 route replaced to: via 88.88.88.88
Source: https://habr.com/ru/post/335030/
All Articles