# Searching for a method to detect NIC cable connected.



## Sopalajo (May 9, 2018)

As I have found during my researches, on Linux systems it is not easy to know from shell if the network cable is correctly plugged, since different method do not really probe the hardware.

The best I have found until now is ifplugd , via the 
	
	



```
ifplugstatus
```
 command, as related here. But it seems ifplugd has no BSD version available.

I don't know if compiling such hardware-interactive source code on my BSD system is expected to work. Could anyone advice me about it? My system is a pfSense FreeBSD 10.3 based one.

Furthermore, I think it would be good to know if there are some equivalent method (any other tool or so) on FreeBSD.

Thanks you.


----------



## phoenix (May 9, 2018)

Just look at the output of ifconfig(8)?  It'll tell you if a cable is plugged in and a link has been established.

```
igb0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
    options=6403bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,TSO6,VLAN_HWTSO,RXCSUM_IPV6,TXCSUM_IPV6>
    ether a0:36:9f:3d:d9:0d
    hwaddr a0:36:9f:3d:d9:0d
    inet 172.20.0.2 netmask 0xffffff00 broadcast 172.20.0.255
    nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
    media: Ethernet autoselect (1000baseT <full-duplex>)
    status: active
```

The last two lines (media/status) are what you are looking for.

If there's no cable plugged in (or the cable is plugged in but not connected to a switch):

```
igb0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
    options=403bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM,TSO4,TSO6,VLAN_HWTSO>
    ether 00:25:90:a4:28:24
    nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
    media: Ethernet autoselect
    status: no carrier
```

No need for external processes.

You can also use `ifconfig igb0 down` to mark a device as "down" while still have a working link.  The physical layer (link) will be active, but no packets will be transferred across that device as the upper layers are "administratively down".  Note the lack of *UP* in the flags line at the top:

```
igb0: flags=8c02<BROADCAST,OACTIVE,SIMPLEX,MULTICAST> metric 0 mtu 1500
    options=bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,VLAN_HWCSUM>
    ether 00:25:90:56:12:88
    inet 10.20.0.3 netmask 0xffff0000 broadcast 10.20.255.255
    nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
    media: Ethernet autoselect (1000baseT <full-duplex>)
    status: active
```

This is one of the nice things about FreeBSD.  All the functions you need tend to be within a single tool, instead of spread around a bazillion different tools all with different CLI interfaces and options syntaxes.  Need to manipulate or configure network interfaces?  The vast majority of what you need is in ifconfig(8).


----------



## Sopalajo (May 9, 2018)

Thanks you, Phoenix.

Indeed. As it is known: Linux has been expanded/mounted, BSD has been designed. 

Simple script to detect if a NIC cable has been disconnected:


```
ifconfig hme2 | grep "status" -i | grep "no carrier" -i
ifconfig | grep "status" -i | grep "no carrier" -i
```

Use your NIC name instead of "hme2". Exit code 0 if "hme2" is unplugged on first case. Exit code 0 if any NIC is unplugged on second case.

---
A bit off-topic, but maybe FreeBSD fits to the Charles Xavier's dialogue with Jean Grey, alias The Phoenix, on Marvel's X-Men comics:



> - I am the power, the knowledge and the passion.
> - Power without contention. Knowledge without wisdom. Passion without love.


----------

