0

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

4 Answers 4

0

If you are on a jessie (or latter) i would suggest to follow this nice guide, since the it uses systemd approach.

4
  • Consider also that cron usually uses sh as default shell and not bash. To use bash you should put a SHELL=/bin/bash in the line before your cronjob Commented May 10, 2017 at 13:17
  • Is the cronjob actually executed? Watch out for the /home/ubunTu/changework.sh typo ... Commented May 10, 2017 at 13:26
  • after sudo crontab -e, SHELL=/bin/bash @reboot sleep 10 && /home/ubunu/changework.sh, but still nothing Commented May 10, 2017 at 13:47
  • Have you checked if the script is actually executed? Check in /var/log/syslog. Commented May 10, 2017 at 14:08
0
  • Don't use sudo if you are already root. And the only way to execute a script at system startup is being root.

  • Why all the copying the file into /etc/init.d? Copy it there once and keept it there. Then execute it from there. - Or go with a script being called from systemd as proposed by Sandro B.

  • Don't use commands only. Always give the full path. Especially at bootup and in cronjobs. E.g.: ifconfig is /sbin/ifconfig

  • You want to use "/bin/ip" instead of "ifconfig". The latter is outdated.

  • Why don't you use /etc/network/interfaces and /etc/network/interfaces.d ? (man interfaces) . Especially "pre-up" to make sure that the script is always executed before the interface is up.

  • If I get your intention right you want to use a dhcp server in your network. What would happen if the first IP you give to your server is already in use?

1
  • the reason why i'm not using /etc/network/interfaces is that the ip should be calculated and and not fixed, really thanks for answering, i'm starting by changing command to full path ifconfig => /sbin/ifconfig , ping => /bin/ping , ip => /sbin/ip, and i will test it Commented May 10, 2017 at 12:49
0

i changed my code

#!/bin/bash # /etc/init.d/changework #set interface interface="eth1" #read current IP address on interface current_ip=`/sbin/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=`/bin/ping -c 1 $IP | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }'` if [ $count -eq 0 ]; then #change IP if available /sbin/ifconfig $interface down /sbin/ifconfig $interface $IP up /sbin/ifconfig $interface #change gateway /bin/ip route add default via $gateway else #IP change not possible echo "IP not available" fi 

then

sudo crontab -e

and added

SHELL=/bin/bash

@reboot sleep 10 && /home/ubunu/changework.sh

but nothing any help please

0

i ended up doing changing the content of rc.local, and like @user2563336 we don't need root access because login script have root access

#!/bin/bash # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. writeinterfacefile() { cat << EOF > $1 # This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface allow-hotplug eth0 iface eth0 inet static address $2 #network 192.168.1.0 netmask $3 #broadcast 192.168.1.255 gateway $4 dns-nameservers 8.8.8.8 EOF #don't use any space before of after 'EOF' in the previous line } file="/etc/network/interfaces" current_ip=`sudo ifconfig eth0 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 - 36` ip=$IP"."$lIP mask=`sudo ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $4}'|sed 's/Mash://'` mask=`cut -f2 -d":" <<< $mask` gateway=$(/sbin/ip route | awk '/default/ { print $3 }') #/bin/rm $file /bin/mv $file /etc/network/BAKinterfaces /usr/bin/touch $file writeinterfacefile $file $ip $mask $gateway /etc/init.d/networking restart exit 0 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.