You are not logged in.

#1 2009-10-27 20:57:16

iamsmrt
Member
Registered: 2009-08-12
Posts: 138

Disabling/enabling network via command line

I'm using wicd for managing my network connections but I'm writing a bash script and want to be able to disable/enable my network connections via command line. I tried

/etc/rc.d/network stop[start]

but it doesn't work, which makes sense because I'm using the wicd daemon, not the network/networkmanager. So I tried:

/etc/rc.d/wicd stop[start]

but that seems to just disable the gui and not the network. Is there some way I can do this while keeping wicd as my network manager?

-SMRT

Offline

#2 2009-10-27 21:02:20

evr
Arch Linux f@h Team Member
Registered: 2009-01-23
Posts: 554

Re: Disabling/enabling network via command line

ifconfig eth0 down/up? (or other interface instead of eth0)

Offline

#3 2009-10-27 21:23:41

iamsmrt
Member
Registered: 2009-08-12
Posts: 138

Re: Disabling/enabling network via command line

evr wrote:

ifconfig eth0 down/up? (or other interface instead of eth0)

Yay, I <3 you.

Is there a way to query for which interface is connected and then disable/enable it?

EDIT: I've got another problem, for some reason I'm getting the error "line 16: ifconfig: command not found", no idea why because the command works perfectly fine in a terminal. Here's my script

#!/bin/bash                                                                                                                                                    

# trap CTRL-C and restart the network                                                                                                                          

trap 'ifconfig eth0 up && ifconfig wlan0 up' INT

# reset CTRL-C functionality                                                                                                                                   

trap - INT

# Study loop                                                                                                                                                   

COUNTER=0

while [ $COUNTER -lt 10 ]; do
    ifconfig eth0 down #&& ifconfig wlan0 down                                                                                                                 
#    sleep 30m                                                                                                                                                 
#   ifconfig eth0 up && ifconfig wlan0 up                                                                                                                      
#    sleep 10m                                                                                                                                                 
done

Last edited by iamsmrt (2009-10-27 21:36:02)

Offline

#4 2009-10-27 21:58:37

Misfit138
Misfit Emeritus
From: USA
Registered: 2006-11-27
Posts: 4,170

Re: Disabling/enabling network via command line

hmmmmm...Bringing an interface up requires root privileges, so if you're trying to bring it up as non-root, you might get that error..

Offline

#5 2009-10-27 22:11:50

iamsmrt
Member
Registered: 2009-08-12
Posts: 138

Re: Disabling/enabling network via command line

That's the error I get when I try (study is the name of the script):

sudo study

If I don't have the sudo, it gives a different error

SIOCSIFFLAGS: Permission denied

which is the error I get when trying "ifconfig eth0 down" without the sudo on the command line.

Offline

#6 2009-10-27 22:54:52

amranu
Member
Registered: 2008-09-25
Posts: 94

Re: Disabling/enabling network via command line

chmod +x study

Offline

#7 2009-10-27 23:33:17

iamsmrt
Member
Registered: 2009-08-12
Posts: 138

Re: Disabling/enabling network via command line

Yea, it's already an executable and in my path.

Last edited by iamsmrt (2009-10-27 23:33:28)

Offline

#8 2009-10-27 23:49:07

fsckd
Forum Moderator
Registered: 2009-06-15
Posts: 3,497

Re: Disabling/enabling network via command line

ifconfig normally resides in /sbin. Check to see if ifconfig is in your path.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#9 2009-10-28 00:56:00

iamsmrt
Member
Registered: 2009-08-12
Posts: 138

Re: Disabling/enabling network via command line

fsckd wrote:

ifconfig normally resides in /sbin. Check to see if ifconfig is in your path.

Yup, it's in my path.

Offline

#10 2009-10-28 01:48:32

iamsmrt
Member
Registered: 2009-08-12
Posts: 138

Re: Disabling/enabling network via command line

I put "sudo ifconfig..." into the script and it worked, why is that? The behavior seems to be that sudo *script* doesn't actually perform all the commands with root priveleges.

Offline

#11 2009-10-28 01:52:56

gog
Member
Registered: 2009-10-13
Posts: 103

Re: Disabling/enabling network via command line

do you have noexec in the sudoers file?

Offline

#12 2009-10-28 01:56:47

iamsmrt
Member
Registered: 2009-08-12
Posts: 138

Re: Disabling/enabling network via command line

gog wrote:

do you have noexec in the sudoers file?

Nope.

# sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
# Failure to use 'visudo' may result in syntax or file permission errors
# that prevent sudo from running.
#
# See the sudoers man page for the details on how to write a sudoers file.
#

# Host alias specification

# User alias specification

# Cmnd alias specification

# Defaults specification

Defaults env_keep="VISUAL"

# Runas alias specification

# User privilege specification
root    ALL=(ALL) ALL
brian   ALL=(ALL) ALL

# Uncomment to allow people in group wheel to run all commands
# %wheel        ALL=(ALL) ALL

# Same thing without a password
# %wheel        ALL=(ALL) NOPASSWD: ALL

# Samples
# %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users  localhost=/sbin/shutdown -h now

Offline

#13 2009-10-28 02:59:52

iamsmrt
Member
Registered: 2009-08-12
Posts: 138

Re: Disabling/enabling network via command line

Boy, this is really annoying. It prompts me for my password everytime my script plays with the interface (30 min, 10 min, 30 min, 10 min, etc.), I'd really like a way to get around that but it's difficult with the weird sudo functionality.

Offline

#14 2009-10-28 03:10:48

gog
Member
Registered: 2009-10-13
Posts: 103

Re: Disabling/enabling network via command line

Oh I found out why, I think.

Trap is a weird shell function thing. It's not a bin so sudo doesn't like it.

Either specify what environment values to keep in sudoers or do sudo -E scriptname.

Last edited by gog (2009-10-28 03:11:22)

Offline

#15 2009-10-28 03:54:29

iamsmrt
Member
Registered: 2009-08-12
Posts: 138

Re: Disabling/enabling network via command line

gog wrote:

Oh I found out why, I think.

Trap is a weird shell function thing. It's not a bin so sudo doesn't like it.

Either specify what environment values to keep in sudoers or do sudo -E scriptname.

I figured it out, instead of "sudo ifconfig" I used the full path "/sbin/ifconfig". This fixes it all smile.

Offline

#16 2009-10-29 16:05:35

otisranson
Member
Registered: 2008-01-31
Posts: 33

Re: Disabling/enabling network via command line

iamsmrt wrote:

Is there a way to query for which interface is connected and then disable/enable it?

#ifconfig -a

The -a flag will show you All interfaces.  You can see if they are connected if they have an address.

Offline

Board footer

Powered by FluxBB