Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Enable/Disable LLDP on VMWare ESXi. Requires SSH access to ESXi. Doesn't require vCenter.
#!/bin/sh
# Enable/Disable LLDP on vSwitch ports on VMWare ESXi
# Tested with ESXi 6.0.0 3620759
# Doesn't need vCenter, only SSH access to the ESXi machine
# (c) Pekka "raspi" Jarvinen 2016 http://raspi.fi/
SWITCH=$1
OPERATION=$2
if [ "$SWITCH" = "" ] || [ "$OPERATION" = "" ]; then
echo "Enable/disable LLDP on vSwitch"
echo ""
echo "USAGE:"
echo "$0 <vSwitch> <operation>"
echo "Examples: "
echo "Enable LLDP: $0 vSwitch0 1"
echo "Disable LLDP: $0 vSwitch0 0"
exit 1
fi
case "$OPERATION" in
0) ;;
1) ;;
*) echo "Invalid operation: $OPERATION"; exit 1 ;;
esac
for PORT in `vsish -e ls /net/portsets/$SWITCH/ports | sed 's/\/$//'`
do
echo "Port: $PORT"
DATA=`vsish -e get /net/portsets/$SWITCH/ports/$PORT/status`
echo "$DATA" | grep -i "port index:"
echo "$DATA" | grep -i "clientName:"
echo "$DATA" | grep -i "clientType:"
echo "$DATA" | grep -i "portCfg:"
echo " Trying to change LLDP state to $OPERATION.."
vsish -e set /net/portsets/$SWITCH/ports/$PORT/lldp/enable $OPERATION &> /dev/null
LLDPSTATE=`vsish -e get /net/portsets/$SWITCH/ports/$PORT/lldp/enable`
if [ "$LLDPSTATE" = "$OPERATION" ]; then
echo " LLDP state successfully changed"
else
echo " ERROR: changing LLDP state failed"
fi
echo "------------------------------"
echo ""
done
@alexandrud

This comment has been minimized.

Copy link

alexandrud commented Nov 1, 2017

Hi,

Thanks for the script. It saved me some time. To change just the physical NICs I've added an additional if.

for PORT in vsish -e ls /net/portsets/$SWITCH/ports | sed 's/\/$//'
do
        DATA=vsish -e get /net/portsets/$SWITCH/ports/$PORT/status
        echo "$DATA" | grep -q "Physical NIC"
        if [ $? = 0 ];then
                echo "Port: $PORT"
                echo "$DATA" | grep -i "port index:"
                echo "$DATA" | grep -i "clientName:"
                echo "$DATA" | grep -i "clientType:"
                echo "$DATA" | grep -i "portCfg:"

                echo "  Trying to change LLDP state to $OPERATION.."
                vsish -e set /net/portsets/$SWITCH/ports/$PORT/lldp/enable $OPERATION &> /dev/null

                LLDPSTATE=vsish -e get /net/portsets/$SWITCH/ports/$PORT/lldp/enable
                if [ "$LLDPSTATE" = "$OPERATION" ]; then
                        echo "  LLDP state successfully changed"
                else
                        echo "  ERROR: changing LLDP state failed"
                fi

        echo "------------------------------"
        echo ""
        fi
done
@Manouchehri

This comment has been minimized.

Copy link

Manouchehri commented Dec 5, 2017

The above script is missing the `'s.

SWITCH=$1
OPERATION=$2

if [ "$SWITCH" = "" ] || [ "$OPERATION" = "" ]; then
	echo "Enable/disable LLDP on vSwitch"
	echo ""
	echo "USAGE:"
	echo "$0 <vSwitch> <operation>"
	echo "Examples: "
	echo "Enable LLDP: $0 vSwitch0 1"
	echo "Disable LLDP: $0 vSwitch0 0"

	exit 1
fi

case "$OPERATION" in
		0) ;;
		1) ;;
		*) echo "Invalid operation: $OPERATION"; exit 1 ;;
esac


for PORT in `vsish -e ls /net/portsets/$SWITCH/ports | sed 's/\/$//'`
do
	DATA=`vsish -e get /net/portsets/$SWITCH/ports/$PORT/status`
	echo "$DATA" | grep -q "Physical NIC"
	if [ $? = 0 ];then
		echo "Port: $PORT"
		echo "$DATA" | grep -i "port index:"
		echo "$DATA" | grep -i "clientName:"
		echo "$DATA" | grep -i "clientType:"
		echo "$DATA" | grep -i "portCfg:"

		echo "  Trying to change LLDP state to $OPERATION.."
		vsish -e set /net/portsets/$SWITCH/ports/$PORT/lldp/enable $OPERATION &> /dev/null

		LLDPSTATE=`vsish -e get /net/portsets/$SWITCH/ports/$PORT/lldp/enable`
		if [ "$LLDPSTATE" = "$OPERATION" ]; then
			echo "  LLDP state successfully changed"
		else
			echo "  ERROR: changing LLDP state failed"
		fi

	echo "------------------------------"
	echo ""
	fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.