Skip to content

Instantly share code, notes, and snippets.

@mumubin
Last active April 2, 2019 07:15
Show Gist options
  • Save mumubin/3fc3b78f39325e7c4b3dbc9e96dde28e to your computer and use it in GitHub Desktop.
Save mumubin/3fc3b78f39325e7c4b3dbc9e96dde28e to your computer and use it in GitHub Desktop.
change_gateway_centos.sh
#!/bin/bash
LOG_FILE="/tmp/user_script_log"
logit()
{
echo "[${USER}][`date`] - ${*}" >> ${LOG_FILE}
}
logit "Start to user script to update default gw"
IP=$(/sbin/ip route | awk '/default/ { print $3 }')
# .4 address is gw on netscaler
GATEWAY_IP=`echo $IP | awk -F '.' '{print $1"."$2"."$3"."4}'`
logit "original gw is $IP"
logit "new gw is $GATEWAY_IP"
# Check if gateway ip start from 10.X.Y.Z
start=$(echo "$GATEWAY_IP"| awk -F '.' '{print $1}')
if [ $start == '10' ]
then
logit "$GATEWAY_IP is 10.X.Y.Z internal IP"
else
logit "$GATEWAY_IP is not 10.X.Y.Z internal IP"
exit
fi
ping -q -c3 $GATEWAY_IP > /dev/null
if [ $? -eq 0 ]
then
logit "ping $GATEWAY_IP success"
else
logit "ping $GATEWAY_IP error"
exit
fi
if [ "$GATEWAY_IP" ]
then
ip route replace default via $GATEWAY_IP
grep -q "GATEWAY=$GATEWAY_IP" /etc/sysconfig/network || echo "GATEWAY=$GATEWAY_IP" >> /etc/sysconfig/network
logit "Updated /etc/sysconfig/network and default route"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment