View previous topic :: View next topic |
Author |
Message |
jhs_bangkok n00b
Joined: 12 Feb 2003 Posts: 3
|
Posted: Wed Feb 12, 2003 1:59 pm Post subject: ethernet network media autodetection |
|
|
This post explains how I made my laptop automagically get on the network when I simply plug in the ethernet cable (a la windows 2000).
I looked but I don't see any programs or discussions about how to do this, so I thought that I'd add this. But my guess is this is so convenient that there must be a simple program or script and I just didn't find it and rolled my own. Quick and dirty and all the usual qualifications apply... I just run it in the background from local startup scripts.
Code: |
#!/usr/bin/perl -w
#
# ethdetectd - detects when I plug in an ethernet cable and brings up the
# interface
#
# You need to have the mii-diag package to use this
#
# last edited by jhs_bkk at yahoo.com - Wed Feb 12 19:36:22 ICT 2003
# -=-=-=-= You edit these -=-=-=-=-
@ifaces = ( 'eth0' ); # List of interfaces to poll
$sleep = 5; # Seconds to sleep between polls
$ifconfig = '/sbin/ifconfig'; # path to ifconfig
$mii_diag = '/sbin/mii-diag'; # path to mii-diag
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
$up = 1;
$down = 0;
# Populate the hash of our devices and their up status
foreach $interface ( @ifaces ) {
system "$ifconfig $interface 2> /dev/null | grep -q UP";
# $? is 0 (true) if the iface is up, 1 (false) if it's down
$ifaces{$interface} = ( $? == 0 ? $up : $down );
}
# Here we go..
while(1) {
foreach $interface ( keys %ifaces ) {
system "$mii_diag --status $interface > /dev/null";
# $? is 0 if plugged; 2 if unplugged
if($? == 0) {
# Cable plugged in. Bring the network up if it is down
if( down($interface) ) {
system "/etc/init.d/net.$interface start";
$ifaces{$interface} = $up;
}
}
else {
# Cable is unplugged. Bring the network down if it is up
if( up($interface) ) {
system "/etc/init.d/net.$interface stop" if up($interface);
$ifaces{$interface} = $down;
}
}
}
sleep 5;
}
sub up { return( $ifaces{$_[0]} == $up ); }
sub down { return( $ifaces{$_[0]} == $down ); }
# vim: sts=4:sw=4:noet
|
|
|
Back to top |
|
|
cyfred Retired Dev
Joined: 23 Aug 2002 Posts: 596
|
Posted: Wed Feb 12, 2003 3:45 pm Post subject: |
|
|
Thats weird that you actually have to do that at all...
My laptop will start up the network device and assign it a IP on boot up with or without a cable. I dont automount any NFS shares so there is no problem. All I have to do is plug in the cable and start browsing the net if I want to. |
|
Back to top |
|
|
jhs_bangkok n00b
Joined: 12 Feb 2003 Posts: 3
|
Posted: Wed Feb 12, 2003 4:22 pm Post subject: Interesting |
|
|
cyfred wrote: | My laptop will start up the network device and assign it a IP on boot up with or without a cable. |
Hm, are you using a static IP assignment? Maybe I should have said that my net.eth0 is configured for dhcp so I could plug in to e.g. a public network or the office or such and it will now grab a DHCP lease with no hassle. Sort of like how PCMCIA network cards work via cs. |
|
Back to top |
|
|
cyfred Retired Dev
Joined: 23 Aug 2002 Posts: 596
|
Posted: Wed Feb 12, 2003 10:49 pm Post subject: |
|
|
Yep im using static
I just tried this using my old laptop which was setup to use DHCP and besides the fact that there was no server to use it worked ... useful little script that is. |
|
Back to top |
|
|
Safrax Guru
Joined: 23 Apr 2002 Posts: 422
|
Posted: Fri Feb 21, 2003 3:58 pm Post subject: |
|
|
I found a small problem. Perl was complaining that the $sleep = 5; on line 12 was only being used once. So as a fix...
Should be changed to:
Code: |
}
}
}
sleep $sleep;
}
|
This allows the sleep delay to be configurable at the top. |
|
Back to top |
|
|
kosan n00b
Joined: 04 Jul 2002 Posts: 29
|
Posted: Thu Apr 03, 2003 8:44 pm Post subject: |
|
|
Quote: | I looked but I don't see any programs or discussions about how to do this, so I thought that I'd add this. But my guess is this is so convenient that there must be a simple program or script and I just didn't find it and rolled my own. |
You are right about "there must be a simple prgram" for this. It's called "ifplugd" so you can just emerge that and then add it to your default runlevel and it will configure the network when you actually plug a cable in. |
|
Back to top |
|
|
Enderson Retired Dev
Joined: 12 Nov 2003 Posts: 141 Location: Arapiraca/Maceió, AL, Brazil
|
Posted: Fri Mar 05, 2004 8:55 pm Post subject: |
|
|
OK.
I ,ve installed ifplugd, didn't tested yet, but I have a question.
I need that when I don't have dhcp avaliable, the eth0 stay with default IP like
192.168.0.1
Is it possible with ifplugd ?
'cause when I don't have dhcp the eth0 don't get up, and X stay very slow. _________________ --
Nothing to say here! |
|
Back to top |
|
|
|