help with script to bring network up and down please

New to Debian (Or Linux in general)? Ask your questions here!

help with script to bring network up and down please

Postby Struggling » 2010-02-12 06:38

I like to disconnect the internet when I want to and connect when I want to. I've been using Etch but have now installed Lenny. This is what I've done:

Created in kwrite and saved in /home/[my username]/bin as "ifdown":
#!/bin/bash
#script to bring network interface down
ifdown eth0

Clicked "is executable" on the "permissions" tab of the properties.

Changed the ownership to root (chown root:root ifdown)

My notes tell me that this is what I did in Etch to have it working (via a shortcut on the desktop). Either my notes are incomplete/wrong, things are different in Lenny, or I'm doing something else wrong.

Can anybody help me as to how to make it work, please?
Novice
Debian Lenny, KDE
Struggling
 
Posts: 143
Joined: 2008-04-20 06:00
Location: Australia

Re: help with script to bring network up and down please

Postby mojoman » 2010-02-12 07:23

There are a number of basic checks I'd start with.

Is ownership and permissions set alright? Check using:

Code: Select all
ls -l /home/[USERNAME]/bin


Also, check that you have bash as your shell:

Code: Select all
echo $SHELL


and that it is installed:

Code: Select all
whereis bash


Also, since your upgrade, is your interface named eth0 (it could be namned something else)

Code: Select all
ifconfig -a


best regards
mojoman
http://www.debianuserforums.org
By the community. For the community.
User avatar
mojoman
 
Posts: 600
Joined: 2006-10-15 18:43

Re: help with script to bring network up and down please

Postby Struggling » 2010-02-12 07:40

Thanks for the reply. Here are the responses to what you've suggested:

Ownership and permissions:
total 8
-rwxrwxrwt 1 root root 63 2010-02-12 14:08 ifdown
-rwxr-xrwx 1 root root 59 2010-02-12 13:31 ifup

bash as shell returns:
/bin/bash

whereis bash:
bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz

Yes, the interface is eth0. If I just type ifdown eth0 in the super user terminal, it brings the network interface down. ifconfig -a returns eth0 and lo.
Novice
Debian Lenny, KDE
Struggling
 
Posts: 143
Joined: 2008-04-20 06:00
Location: Australia

Re: help with script to bring network up and down please

Postby mojoman » 2010-02-12 08:14

Wait, isn't the command:

Code: Select all
ifconfig down eth0
http://www.debianuserforums.org
By the community. For the community.
User avatar
mojoman
 
Posts: 600
Joined: 2006-10-15 18:43

Re: help with script to bring network up and down please

Postby Struggling » 2010-02-12 08:50

either "ifdown eth0" or "ifconfig eth0 down" (seem to :D ) work in a terminal window, but neither works from my script file.

"ifconfig down eth0" in a terminal comes back with:

eth0: Unknown host
ifconfig: `--help' gives usage information.

and doesn't work from the script file either.
Novice
Debian Lenny, KDE
Struggling
 
Posts: 143
Joined: 2008-04-20 06:00
Location: Australia

Re: help with script to bring network up and down please

Postby mojoman » 2010-02-12 09:11

sorry, wrong order. My bad.

Run the script from a terminal and check the output (if any):

Code: Select all
cd /home/USER/bin


Code: Select all
./ifdown


Try running it as root and user. I'm not very good at scripts but I assume that if it should be run as root (and you probably need to as ifconfig is a command normal users do not have access to) you might have to use sudo in your script (and give sudo rights to your user accordingly).
http://www.debianuserforums.org
By the community. For the community.
User avatar
mojoman
 
Posts: 600
Joined: 2006-10-15 18:43

Re: help with script to bring network up and down please

Postby Struggling » 2010-02-12 09:42

It doesn't work trying to run it from the terminal - either the normal terminal or the root terminal. I think that it definitely needs to run as root, I have in my notes from before that it needs to be root, but changing the ownership to root before seemed to be sufficient in Etch to make it run.

I don't know how to bestow sudo rights. I'll do some googling and give that a go. Thanks for the suggestion.
Novice
Debian Lenny, KDE
Struggling
 
Posts: 143
Joined: 2008-04-20 06:00
Location: Australia

Re: help with script to bring network up and down please

Postby mojoman » 2010-02-12 09:44

The ownership of the file is not sufficient for it to be executed as by the owner. You'll need sudo.
http://www.debianuserforums.org
By the community. For the community.
User avatar
mojoman
 
Posts: 600
Joined: 2006-10-15 18:43

Re: help with script to bring network up and down please

Postby Struggling » 2010-02-12 11:48

I tried following some tutorials for doing the sudoers file, then I ended up putting (my username) ALL=(ALL) ALL (the same as the root permissions) and uncommented out the line that makes no password necessary and added "sudo" before "ifdown eth0", but the script still didn't work. I've changed it all back now. In theory, should that have worked if sudo was the answer to getting it to work? I must admit, I'm quite confused over configuring the sudoers file and if I'm wrong and the test was insufficient, I'll need further help with it.
Novice
Debian Lenny, KDE
Struggling
 
Posts: 143
Joined: 2008-04-20 06:00
Location: Australia

Re: help with script to bring network up and down please

Postby smallchange » 2010-02-12 12:25

Is ~/bin/in your PATH before /sbin? Maybe /sbin is not in your path at all? Change your script to
Code: Select all
#!/bin/bash
#script to bring network interface down
/sbin/ifdown eth0

and see if it doe not work.
smallchange
 
Posts: 1740
Joined: 2009-05-04 15:56

Re: help with script to bring network up and down please

Postby Struggling » 2010-02-12 12:43

I made that change, but unfortunately still no joy.
Novice
Debian Lenny, KDE
Struggling
 
Posts: 143
Joined: 2008-04-20 06:00
Location: Australia

Re: help with script to bring network up and down please

Postby mojoman » 2010-02-12 12:57

Struggling wrote:I tried following some tutorials for doing the sudoers file, then I ended up putting (my username) ALL=(ALL) ALL (the same as the root permissions) and uncommented out the line that makes no password necessary and added "sudo" before "ifdown eth0", but the script still didn't work. I've changed it all back now. In theory, should that have worked if sudo was the answer to getting it to work? I must admit, I'm quite confused over configuring the sudoers file and if I'm wrong and the test was insufficient, I'll need further help with it.


I have these lines in my sudoers file:

Under the heading Cmd Alias:
Code: Select all
Cmnd_Alias      SHUTDOWN = /sbin/shutdown


Under the heading user privilege specifications:
Code: Select all
MY_USER_NAME ALL=SHUTDOWN


I strongly advice you not to hand all root privileges to you user, rather just for the command ifdown and ifup (or use ifconfig and just give it for the one command).

So, I'd try something like:

Code: Select all
Cmnd_Alias      IFCONFIG = /sbin/ifconfig

Code: Select all
YOUR_USER_NAME ALL=IFCONFIG
http://www.debianuserforums.org
By the community. For the community.
User avatar
mojoman
 
Posts: 600
Joined: 2006-10-15 18:43

Re: help with script to bring network up and down please

Postby smallchange » 2010-02-12 12:58

Struggling wrote:I made that change, but unfortunately still no joy.


So it silently fails? No message, just returns the prompt and the network has not changed?
smallchange
 
Posts: 1740
Joined: 2009-05-04 15:56

Re: help with script to bring network up and down please

Postby Struggling » 2010-02-12 13:27

mojoman wrote:I strongly advice you not to hand all root privileges to you user, rather just for the command ifdown and ifup (or use ifconfig and just give it for the one command).
I tried the aliases and things from a tutorial, but it didn't work, so I just tried giving all permission to see whether it would make the script work, but it still didn't work. If it had worked I would have then worked again on getting the right format for the sudoers file to give me permission just to bring the interface up and down. I've changed it all back to how it was in the first place, so I don't have root privileges.

smallchange wrote:So it silently fails? No message, just returns the prompt and the network has not changed?

There's no prompt, because I'm just clicking on the file. I use Knemo which gives me a little monitor icon showing the network connection. If I use the command in a terminal window and the interface goes down, a red cross appears over the monitors and it plays a little noise. Nothing happens when I click on the file containing the script.
Novice
Debian Lenny, KDE
Struggling
 
Posts: 143
Joined: 2008-04-20 06:00
Location: Australia

Re: help with script to bring network up and down please

Postby mojoman » 2010-02-12 13:41

Like I said above, run it from a terminal. That will help us diagnose the problem (not to mention that GUI is for sissies :wink: )
http://www.debianuserforums.org
By the community. For the community.
User avatar
mojoman
 
Posts: 600
Joined: 2006-10-15 18:43

Next

Return to Beginners Questions

Who is online

Users browsing this forum: No registered users and 10 guests

fashionable