View Full Version : A bash script to turn eth0 down then up

December 26th, 2009, 03:28 PM
Hi I have a task i have to do to get internet connection on my laptop. I conect to another pc for internet but need to config eth0 connection each day, I do this in terminal with command sudo ifconfig eth0 down then sudo ifconfig eth0 up, then i,m good to go with internet, I have read that you can have a script automate it for you. This would be my 1st tme at attempting soeting like this

December 26th, 2009, 04:19 PM
Could put this file in /etc/init.d/ and update the symlinks to it via update-rc.d . I've named it internetgo. You'll need root privelages to put it there and you'll have to chmod +x the file to make it executable. After putting it in /etc/init.d/ run, from the terminal,

update-rc.d internetgo defaults

# Script to enable my internet
# Usage: internetgo
ifconfig eth0 down
ifconfig eth0 up
exit 0


The Secret
December 26th, 2009, 04:21 PM
sudo ifconfig eth0 down
sudo ifconfig eth0 up

Save it in /usr/local/bin as auto-ifconfig ( you can change the name of the file ) and you should be ready to go.

December 26th, 2009, 04:28 PM
Hm, you might want to try restarting networking with the "/etc/init.d/networking restart" command instead. Anyway, I tend to use zenity quite a bit to have graphical dialogs with scripts without too much effort. Open up a file using a text editor (gedit will be fine), and give it a .sh extension. eg. "restart-eth0.sh" and save the following script.

# The line above tells the shell to use the program /bin/bash to run this script

# Get password from user using Zenity
passwd=$(zenity --entry --hide-text --text "Please enter your password to restart eth0")

# Execute ifconfig. "echo $passwd" means that the password's being piped to sudo so you don't
# have to type it in again. sudo will then execute ifconfig.
echo $passwd | sudo -S ifconfig eth0 down
echo $passwd | sudo -S ifconfig eth0 up

# Check if it ran correctly. If it did, the last command should return 0
# You can check the status of the last command using the $? variable.
if [[ $? != 0 ]] ; then
zenity --error --text "Hm, something went wrong..."

That's just an example - it *should* work but I haven't tested it! I hope the comments make sense. All lines beginning with a hash are comments. When you saved the file, right click then click properties, go to the permissions tab and tick "allow executing file as a program". From then on just double click the file, and hit run to run the script.


December 26th, 2009, 05:16 PM
Hm, you might want to try restarting networking with the "/etc/init.d/networking restart" command instead.

+1 to that, although I prefer:

sudo service networking restart

(I discovered the service command the other day, it saves you some typing, and the autocompletion is better)

December 26th, 2009, 05:24 PM
Hi I,m Glad u replied I have seen something similar to what You have put Secret, the only prblemi have it needs to wait inbetween down & up, i tried from command line sudo ifconfig eth0 down && sudo ifconfig eth0 up dosen't seem to work so have to wait till connection accutally stops before using UP, as i said i,m using scripts for the 1st time so installing them & then executing them is all new to me, i appreciate your help

December 26th, 2009, 05:27 PM
Can thks for reply i was just posting missed your post, have you come across this problem? not tried the restart methord, I have tried eth0 down && eth0 up is that the same comand as restart?

December 26th, 2009, 06:14 PM
Hi I have a bash script that The Secret done for me i need to add a delay between ifconfig eth0 down & eth0 up

sudo ifconfig eth0 down
delay s5
sudo ifconfig eth0 up

will this give me a delay of 5sec?

The Secret
December 26th, 2009, 06:21 PM
sleep <seconds>

December 26th, 2009, 06:29 PM
sleep 5

Will make it wait 5 seconds before proceeding.

EDIT: Totally Ninja'd.

December 26th, 2009, 06:30 PM
thks every1 for your help will give it a try will let you know