LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   if( ethernet-cable-is-plugged-in ) { ... (https://www.linuxquestions.org/questions/linux-networking-3/if-ethernet-cable-is-plugged-in-%7B-88791/)

mrtwice 09-03-2003 08:21 AM

if( ethernet-cable-is-plugged-in ) { ...
 
In windows, it will tell you if you have a network cable plugged in or not. How would you do this in Linux? Thanks.

david_ross 09-03-2003 03:02 PM

Take a look at the output of "ifconfig" It shows "RUNNING" for that device if the cable is connected.

mrtwice 09-03-2003 04:43 PM

It does, however it is pretty slow. It took almost a minute for it to show that it was back up. That is not acceptable for what I want to do with it. I am trying to set up my laptop to not wait for a dhcp lease on startup when the eth0 cable is unplugged. If anyone else has an idea, I am all ears.

dubman 09-03-2003 04:55 PM

try checking your messages at /var/log/messages or dmesg

--dubman

tang 09-03-2003 09:14 PM

--------------------------------------------------------------------------------------
I am trying to set up my laptop to not wait for a dhcp lease on startup when the eth0 cable is unplugged. If anyone else has an idea, I am all ears.
--------------------------------------------------------------------------------------

I just turned off start network at boot (Mandrake Control Center ->Network and Internet -> DrakConnect -> Expert mode -> Configure LAN -> started on boot: no)

boot time is much faster since it doesn't look for dhcp and when you need net access just click on Activate (same location as above)

mrtwice 09-04-2003 08:34 AM

Checking the messages isn't really what I am after. I want to be able to run some kind of command that will check. Then, I will use that command in my startup script to not start that device if the ethernet cord is not plugged in.

tang: not everyone runs mandrake my friend. I run slackware. I appreciate your input though.

tang 09-04-2003 08:45 AM

didn't think about that ;) it erks me sometimes when i read a post that mentions 'go to blah and do blah' without explaining how to get to blah in the first place (assuming if you're posting cause you don't know how to fix something, you probably don't know how to get to where you're supposed to fix it)

you as in anyone, not you-you

will pay more attention to what i'm posting, thanks for the tip
hope you find what you're looking for :)

david_ross 09-06-2003 07:54 AM

I've only just found this one but this may be better for you:
mii-tool -w

horlix 09-06-2003 10:46 AM

perhaps you could reduce the amount of time the dhcp client will wait for a reply from your dhcp server so that even with no ethernet cable your startup time will not greatly increase.

think theres a timeout setting in /etc/dhclient.conf or thereabouts.

mrtwice 09-08-2003 08:21 AM

david_ross:

Thaks for the help. That was exactly what I was looking for.

mrtwice 09-08-2003 10:38 AM

Actually, mii-tool was not quite what I was looking for. It did not provide an option (that I could see) that would allow me to just run the command and then look at the return value to see what was returned.

However, mii-tool is actually derived from mii-diag which did provide the option i was looking for (by passing -s as an argument). So, here is my rc.inet1 (slackware network start up script):

<----------------------rc.inet1------------------------------>

rsyring@dell:~$ cat /etc/rc.d/rc.inet1
#! /bin/sh
# /etc/rc.d/rc.inet1
# This script starts up the base networking system.
#
# Version:
# @(#)/etc/rc.d/rc.inet1 8.1 Tue May 28 15:27:39 PDT 2002 (pjv)

# Edit these values to set up your first Ethernet card (eth0):
IPADDR="127.0.0.1" # REPLACE with YOUR IP address!
NETMASK="255.255.255.0" # REPLACE with YOUR netmask!
# Or, uncomment the following lines to set up eth0 using DHCP:
USE_DHCP=yes
# If using DHCP, do media check first to prevent slow start-ups:
MEDIA_CHECK=yes

# If your provider requires a DHCP hostname, uncomment and edit below:
#DHCP_HOSTNAME="CCHOSTNUM-A"

# Edit these values to set up your second Ethernet card (eth1),
# if you have one. Otherwise leave it configured to 127.0.0.1,
# or comment it out, and it will be ignored at boot.
IPADDR2="127.0.0.1" # REPLACE with YOUR IP address!
NETMASK2="255.255.255.0" # REPLACE with YOUR netmask!
# Or, uncomment the following lines to set up eth1 using DHCP:
USE_DHCP2=yes
# If using DHCP, do media check first to prevent slow start-ups,
MEDIA_CHECK2=yes

# If your provider requires a DHCP hostname, uncomment and edit below:
#DHCP_HOSTNAME2="CCHOSTNUM-A"

# Edit the next line to point to your gateway:
GATEWAY="" # REPLACE with YOUR gateway!

# You shouldn't need to edit anything below here.

# Set up the loopback interface:
/sbin/ifconfig lo 127.0.0.1
/sbin/route add -net 127.0.0.0 netmask 255.0.0.0 lo

#set-up a variable so DHCP does not run when eth0 media is not ready
MEDIA_STATUS=OK
if [ "$MEDIA_CHECK" = "yes" -a "$USE_DHCP" = "yes" ]; then # do media check
echo "Getting status of eth0 interface..."
/sbin/mii-diag -s eth0 >/dev/null 2>&1
if [ ! $? = 0 ]; then # eth0 is not ready for DHCP
MEDIA_STATUS="NOT_OK"
echo "DHCP will not run on eth0 because the media is not ready!"
fi
fi


# Set up the eth0 interface:
if [ "$USE_DHCP" = "yes" -a "$MEDIA_STATUS" = "OK" ]; then # use DHCP to set everything up:
echo "Attempting to configure eth0 by contacting a DHCP server..."
# Add the -h option to the DHCP hostname:
if [ ! "$DHCP_HOSTNAME" = "" ]; then
DHCP_HOSTNAME="-h $DHCP_HOSTNAME"
fi
/sbin/dhcpcd -t 10 ${DHCP_HOSTNAME} -d eth0
elif [ ! "$IPADDR" = "127.0.0.1" -a ! "$IPADDR" = "" ]; then # set up IP statically:
# Determine broadcast and network addresses from the IP address and netmask:
BROADCAST=`/bin/ipmask $NETMASK $IPADDR | cut -f 1 -d ' '`
NETWORK=`/bin/ipmask $NETMASK $IPADDR | cut -f 2 -d ' '`
# Set up the ethernet card:
echo "Configuring eth0:"
echo "ifconfig eth0 ${IPADDR} broadcast ${BROADCAST} netmask ${NETMASK}"
/sbin/ifconfig eth0 ${IPADDR} broadcast ${BROADCAST} netmask ${NETMASK}
# If that didn't succeed, give the system administrator some hints:
if [ ! $? = 0 ]; then
echo "Your eth0 card was not initialized properly. Here are some reasons why this"
echo "may have happened, and the solutions:"
echo "1. Your kernel does not contain support for your card. Including all the"
echo " network drivers in a Linux kernel can make it too large to even boot, and"
echo " sometimes including extra drivers can cause system hangs. To support your"
echo " ethernet, either edit /etc/rc.d/rc.modules to load the support at boot time,"
echo " or compile and install a kernel that contains support."
echo "2. You don't have an ethernet card, in which case you should run netconfig"
echo " and configure your machine for loopback. (Unless you don't mind seeing this"
echo " error...)"
fi
fi # set up eth0

...ditto for eth1, etc ...

beanerjo 10-08-2006 05:32 AM

Very helpful
 
This is exactly what I have been looking for. Thanks!!!

Note: "mii-tool eth0" does it too (I think). mii-tool is contained in the Slackware tcpip package (at least in version 11.0).

osor 10-08-2006 07:56 AM

What about netplugd or ifplugd

ARC1450 10-08-2006 02:57 PM

Quote:

Originally Posted by osor
What about netplugd or ifplugd

Vote for netplug here. . .works very well, and I've never had issues with it.


All times are GMT -5. The time now is 06:34 AM.