Monday, June 13, 2022

Disconnecting USB-C hub kills network

 I have had several problems with my home network recently, where parts of the Ethernet connected devices were not reachable anymore. The problem seemed to be a recently new 8-port Ethernet switch. Turning off and on one of the switch solved the problem for a while.

I figured out (after a long time) that the switch would fail when I disconnect my DELL DA310u USB-C hub from my Macbook. When I plugged the hub back in (even after hours), the network switch came back. Interestingly, the network connection from the USB-C hub to the failing switch is through another switch.

I found this article that describes a pause frame to be sent across the network from the USB-C hub. Man, that sucks....

Sending emails about live/dead server

This code checks if a host on a network is reachable via ping; if not, it sends an email through ssmtp (which needs to be configured beforehand). All parameters are declared in the first few lines.

TODO: remove duplicate code

#!/bin/bash

NUMBER_OF_PINGS=3

HOST_NAME="myhostname.local"

FLAG_FILE=/home/pi/$HOST_NAME.isdead

EMAIL_SENDER=foo@gmail.com

EMAIL_RECEIVER=bar@gmail.com


#ping returns 0 if target could be reached


if ping -c $NUMBER_OF_PINGS $HOST_NAME &> /dev/null

then # host is reachable

  if test -f $FLAG_FILE  # flag file exists

  then

  { echo To: $EMAIL_RECEIVER

    echo From: $EMAIL_SENDER

    echo Subject: 👍 $HOST_NAME is alive again

    echo Congrats! $HOST_NAME is alive again. 

  } | /usr/sbin/ssmtp $EMAIL_RECEIVER

  rm $FLAG_FILE   # delete flag file and send email

  else # nothing to do

    :

  fi

else # host is NOT reachable

  if test -f $FLAG_FILE  # flag file exists, which means that an email has already been sent

  then  # nothing to do

    :

  else  # send an email otherwise and create flag file

  { echo To: $EMAIL_RECEIVER

    echo From: $EMAIL_SENDER

    echo Subject: ☠️  $HOST_NAME is dead

    echo My condolences. I tried to ping $HOST_NAME $NUMBER_OF_PINGS times, but did not get a response. 

  } | /usr/sbin/ssmtp $EMAIL_RECEIVER

  touch $FLAG_FILE

  fi

fi