i have a script that calculate an ip address (using gateway address) than change,
the script work when i launch it manually with sudo
sudo ./changework.sh
i want this script to run during bootup time
i
sudo cp changework.sh /etc/init.d/changework
sudo chmod+x /etc/init.d/changework
sudo update-rc.d changework defaults
but it does not work,
also i tested it using
sudo crontab -e
then added
@reboot sleep 10 && /home/ubunu/changework.sh
it does not work either
changework.sh
#!/bin/bash
#set interface
interface="enp0s5"
#read current IP address on interface
current_ip=`ifconfig $interface 2>/dev/null|awk '/inet addr:/ {print $3}'|sed 's/Bcast://'`
IP=`cut -f1,2,3 -d"." <<< $current_ip`
lIP=`cut -f4 -d"." <<< $current_ip`
lIP=`expr $lIP - 34`
IP=$IP"."$lIP
#return default gateway
gateway=$(/sbin/ip route | awk '/default/ { print $3 }')
#check if IP is taken using ping
count=`ping -c 1 $IP | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }'`
if [ $count -eq 0 ]; then
#change IP if available
ifconfig $interface down
ifconfig $interface $IP up
ifconfig $interface
#change gateway
ip route add default via $gateway
else
#IP change not possible
echo "IP not available"
fi
would any one please have a solution or a workaround and thanks for any help or suggestion