I've checked the tutorials but i keep on struggling with this bash script I wrote, for some reason bash is thinking some variables that I defined are function and complains that it does not exist, and if I prefix it with a let, it complains about = something....
Anyway here's the offensive bash script below...
Code:
#!/bin/sh
#
# This currently will only activate for eth1, to test this functionality
#
# Echo and log the $reason value being given
echo 'dhclient-exit-hooks: ' $reason
logger 'dhclient-exit-hooks: ' $reason
# Programs
IPTABLES="/sbin/iptables"
# Enable Ip_forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
# Script variables
eth0_oldFile="/var/run/eth0.oldIP"
eth1_oldFile="/var/run/eth0.oldIP"
oldIP=""
oldRoute=""
newIP=""
newRoute=""
get_old_variables () {
# Check which interface is being called
if [[ "$interface" == "eth0" ]]; then
# Check if the eth0_oldFile file exists
if [[ -e $eth0_oldFile ]]; then
# it exists, so load old IP and router address
$oldIP = `cat $eth0_oldFile | awk '{print $1}'`
$oldRoute = `cat $eth0_oldFile | awk '{print $2}'`
else
# It does not exist, create it and place the
# newIP & newRoute into it
echo $newIP $newRoute > $eth0_oldFile
fi
elif [[ "$interface" == "eth1" ]]; then
# Check if the eth1_oldFile file exists
if [[ -e $eth1_oldFile ]]; then
# it exists, so load old IP and router address
$oldIP = `cat $eth1_oldFile | awk '{print $1}'`
$oldRoute = `cat $eth1_oldFile | awk '{print $2}'`
else
# It does not exist, create it and place the
# newIP & newRoute into it
echo $newIP $newRoute > $eth1_oldFile
fi
fi
echo 'dhclient-exit-hooks: (get_old_variables): ' $interface
logger 'dhclient-exit-hooks: (get_old_variables): ' $interface
echo 'dhclient-exit-hooks: (get_old_variables): ' $oldIP
logger 'dhclient-exit-hooks: (get_old_variables): ' $oldIP
echo 'dhclient-exit-hooks: (get_old_variables): ' $oldRoute
logger 'dhclient-exit-hooks: (get_old_variables): ' $oldRoute
}
flush_NAT () {
$IPTABLES -P FORWARD ACCEPT
$IPTABLES -t nat -P PREROUTING ACCEPT
$IPTABLES -t nat -P POSTROUTING ACCEPT
$IPTABLES -t nat -F
$IPTABLES -t nat -X
echo 'dhclient-exit-hooks: (flush_NAT)'
logger 'dhclient-exit-hooks: (flush_NAT)'
}
create_NAT () {
$IPTABLES -t nat -A PREROUTING -d $newIP -j DNAT --to-destination 172.20.0.3
$IPTABLES -t nat -A POSTROUTING -s 172.20.0.3 -j SNAT --to-source $newIP
echo 'dhclient-exit-hooks: (create_NAT)'
logger 'dhclient-exit-hooks: (create_NAT)'
}
flush_routing () {
ip rule del from 172.20.0.3
ip route flush table 101
echo 'dhclient-exit-hooks: (flush_routing)'
logger 'dhclient-exit-hooks: (flush_routing)'
}
create_routing () {
ip rule add from 172.20.0.3 table 101
ip route add default via $newRoute dev eth1 table 101
ip route flush cache
echo 'dhclient-exit-hooks: (create_routing)'
logger 'dhclient-exit-hooks: (create_routing)'
}
update () {
# This assumes the old ip/routing and new ip/routing is already set
# Check to see if they are not equal, if not equal then process it
if [[ "$newIP" != "$oldIP" || "$newRoute" != "$oldRoute" ]]; then
# the IP or routing is not the same so update the NAT and routing
flush_NAT
flush_routing
create_NAT
create_routing
# Update the oldFile
if [[ "$interface" == "eth0" ]]; then
echo $newIP $newRoute > $eth0_oldFile
elif [[ "$interface" == "eth1" ]]; then
echo $newIP $newRoute > $eth1_oldFile
fi
fi
echo 'dhclient-exit-hooks: (update): ' $newIP $oldIP
logger 'dhclient-exit-hooks: (update): ' $newIP $oldIP
echo 'dhclient-exit-hooks: (update): ' $newRoute $oldRoute
logger 'dhclient-exit-hooks: (update): ' $newRoute $oldRoute
}
# This block determite which $reason code is passed to this script
case "$reason" in
"MEDIUM" | "PREINIT" | "TIMEOUT" ) # Ignore, not sure what to do with "TIMEOUT" but ignore for now
exit 0
;;
"EXPIRE" | "FAIL" ) # flush NAT and routing
flush_NAT
flush_routing
;;
"BOUND" | "RENEW" | "REBIND" | "REBOOT" ) # Do some processing
# Assign the IP/Routes to proper variables
$newIP = $new_ip_address
$newRoute = $new_routers
if [[ "$interface" == "eth1" ]]; then
get_old_variables
update
fi
;;
esac
I've tried a dozen thing for the stupid variable assignment
I've tried:
Code:
newIP = $new_ip_address
newIP = "$new_ip_address"
newIP = `echo $new_ip_address'
$newIP = $new_ip_address
$newIP = "$new_ip_address"
$newIP = `echo $new_ip_address'
let newIP = $new_ip_address
let newIP = "$new_ip_address"
let newIP = `echo $new_ip_address'
let $newIP = $new_ip_address
let $newIP = "$new_ip_address"
let $newIP = `echo $new_ip_address'
And every single time without fail, bash screws up, here's an example of its screwup
Code:
* Stopping eth1
* Bringing down eth1
* Stopping dhclient on eth1 ... [ ok ]
* Shutting down eth1 ... [ ok ]
* Starting eth1
* Bringing up eth1
* dhcp
* Running dhclient ...
dhclient-exit-hooks: PREINIT
dhclient-exit-hooks: REBOOT
/etc/dhcp/dhclient-exit-hooks: line 149: =: command not found
/etc/dhcp/dhclient-exit-hooks: line 150: =: command not found
/etc/dhcp/dhclient-exit-hooks: line 48: =: command not found
/etc/dhcp/dhclient-exit-hooks: line 49: =: command not found
dhclient-exit-hooks: (get_old_variables): eth1
dhclient-exit-hooks: (get_old_variables):
dhclient-exit-hooks: (get_old_variables):
dhclient-exit-hooks: (update):
dhclient-exit-hooks: (update): [ ok ]
* eth1 received address 192.168.60.2/24