Skip to content

Instantly share code, notes, and snippets.

@rcmorano
Created April 12, 2018 16:22
Show Gist options
  • Save rcmorano/4136b6dd74aae1547799e2d614757429 to your computer and use it in GitHub Desktop.
Save rcmorano/4136b6dd74aae1547799e2d614757429 to your computer and use it in GitHub Desktop.
Snippet for configuring various Google Compute Engine network interfaces as described in official doc: https://cloud.google.com/vpc/docs/create-use-multiple-interfaces#configuring_policy_routing
#!/bin/bash
#
# License and Author
#
# Author:: Roberto C. Morano (<[email protected]>)
#
# Copyright:: 2018, Roberto C. Morano (<[email protected]>)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -x
IFACE="$1"
CURRENT_IP=$(ip a s|grep "inet .*scope.global.${IFACE}" | awk '{print $2}'|sed 's|/.*||g')
NET_PREFIX=$(echo $CURRENT_IP | awk -F. '{print $1"."$2"."$3}')
GATEWAY=${NET_PREFIX}.1
ROUTE_TABLE=${IFACE}_rt
grep -q "${IFACE}_rt" /etc/iproute2/rt_tables || echo "1 ${IFACE}_rt" | sudo tee -a /etc/iproute2/rt_tables # (sudo su - first if permission denied)
sudo ifconfig ${IFACE} ${CURRENT_IP} netmask 255.255.255.255 broadcast ${CURRENT_IP} mtu 1430
sudo ip route add ${GATEWAY} src ${CURRENT_IP} dev ${IFACE}
sudo ip route add default via ${GATEWAY} dev ${IFACE} table ${ROUTE_TABLE}
sudo ip rule add from ${CURRENT_IP}/32 table ${ROUTE_TABLE}
sudo ip rule add to ${CURRENT_IP}/32 table ${ROUTE_TABLE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment