Browsed by
Tag: scripts

Part 2 – How to list vSwitch “MAC Address table” on ESXi host?

Part 2 – How to list vSwitch “MAC Address table” on ESXi host?

The other way to list MAC addresses of open ports on vSwitches on the ESXi host is based on net-stats tool.

Use this one-liner.

		
for VSWITCH in $(vsish -e ls /net/portsets/ | cut -c 1-8); do net-stats -S $VSWITCH | grep \{\"name | sed 's/[{,"]//g' | awk '{$9=$10=$11=$12=""; print $0}'; done		
		
	

This is not a final word. 🙂

Part 1 – How to list vSwitch “MAC Address table” on ESXi host?

Part 1 – How to list vSwitch “MAC Address table” on ESXi host?

Sometimes You need to list MAC addresses loged on host’s vSwitches to eliminate VM’s MAC address duplicates.

  1. Create a shell script:
  2. vi mac_address_list.sh
  3. Copy and past the code listed below:
  4. 
    #!/bin/sh
    #vmrale
    for VSWITCH in `vsish -e ls /net/portsets/ | cut -c 1-8`
    do
            echo $VSWITCH
            for PORT in `vsish -e ls /net/portsets/$VSWITCH/ports | cut -c 1-8`
            do
                    CLIENT_NAME=`vsish -e get /net/portsets/$VSWITCH/ports/$PORT/status | grep clientName | uniq`
                    ADDRESS=`vsish -e get /net/portsets/$VSWITCH/ports/$PORT/status | grep unicastAdd | uniq`
                    echo -e "\t$PORT\t$CLIENT_NAME\t$ADDRESS"
            done
    done        
    
    
  5. Change the file’s permissions
  6. chmod 755 mac_address_list.sh
  7. Run the script
  8. ./mac_address_list.sh

Simple, but useful! 🙂

… but this is not the only one possible method 🙂

Alternative methods to create virtual switch.

Alternative methods to create virtual switch.

Creating virtual switch through GUI is well described in documentation and pretty intuitive using GUI. However, sometimes it might be useful to know how to do it with CLI or Powershell, thus making the process part of a script to automate initial configuration of ESXi after installation.

Here you will find commands which are necessary to create and configure a standard virtual switch using CLI and Powershell. Those examples will describe the process of vSwitch creation for vMotion traffic which involves VMkernel creation.

I. vSwitch configuration through CLI

  1. Create a vSwitch named “vMotion”

esxcli network vswitch standard add -v vMotion

  1. Check whether your newly created vSwitch was configured and is available on the list.

esxcli network vswitch standard list

  1. Add physical uplink (vmnic) to your vSwitch

esxcli network vswitch standard uplink add -u vmnic4 -v vMotion

  1. Designate an uplink to be used as active.

esxcli network vswitch standard policy failover set -a vmnic4 -v vMotion

  1. Add a port group named “vMotion-PG” to previously created vSwitch

esxcli network vswitch standard portgroup add -v vMotion -p vMotion-PG

  1. Add a VMkernel interface to a port group (Optional – not necessary if you are creating a vSwitch just for VM traffic)

esxcli network ip interface add -p vMotion-PG -i vmk9

  1. Configure IP settings of a VMkernel adapter.

esxcli network ip interface ipv4 set -i vmk9 -t static -I 172.20.14.11 -N 255.255.255.0

  1. Tag VMkernel adapter for a vMotion service. NOTE – service tag is case sensitive.

esxcli network ip interface tag add -i vmk9 -t vmotion

Done, your vSwitch is configured and ready to service vMotion traffic.

 

II. vSwitch configuration through PowerCLI

  1. First thing is to connect to vCenter server.

Connect-VIServer -Server vcsa.vclass.local -User administrator@vsphere.local -Password VMware1!

  1. Indicate specific host and create new virtual switch, assigning vmnic at the same time.

$vswitch1 = New-VirtualSwitch -VMHost sa-esx01.vclass.local -Name vMotion -NIC vmnic4

  1. Create port group and add it to new virtual switch.

New-VirtualPortGroup -VirtualSwitch $vswitch1 -Name vMotion-PG

  1. Create and configure VMkernel adapter.

New-VMHostNetworkAdapter -VMHost sa-esx01.vclass.local -PortGroup vMotion-PG -VirtualSwitch vMotion -IP 172.20.11.11 -SubnetMask 255.255.255.0 -vmotionTrafficEnabled $true

 

PowerCLI – useful tools

PowerCLI – useful tools

VMware PowerCLI is a powerful tool for daily task for every Admin. The pure console is most commonly used. However, there are a few alternatives to the simple console which could make the use of PowerCLI even more handy. I’ll describe them shortly in the next a few lines.

  1. Powershell ISE script editor, which provides a better user experience. It’s divided into two panes. The upper pane  is for viewing/editing script files, and the lower pane is for running individual commands and displaying their output (an analog of the standard PowerShell console).ISEYou can execute PowerCLI commands in the lower pane of PowerShell ISE or in the PowerShell console. It will be usefull also during the first steps with PowerCLI through analysing of some ready downloaded from Internet scripts.The most convenient way to do is to open a script in the upper pane of Powershell ISE. In this way you can select each individual command and executee by pressing F8 or “Run Selection” button. When the command execution is finished you will see “Completed” message at the bottom of the console.
  2. PowerGUI – it’s another script editor made by Quest Software which was acquired by Dell. powerguiIMHO it’s better organised than PowerShell ISE, the functions are rather simillar. However, instead of tabs with your scripts here you will be able to see the whole folder-tree with different kinds of scripts. It’s really helpful when you work with more than a few scripts.
PowerCLI course

PowerCLI course

I was always keen on getting deeper knowledge about PowerCLI or in other words – start to use it in daily administrative tasks. I decided to do something with it and I think it would be the best way to write my own guide in a form of structured notes and share it here with you. Perhaps someone would find it useful.

Therefore in PowerCLI & VMA tab you could find an agenda of this course which will be systematically updated with next parts.

There are planned fallowing parts:

  1. VMware PowerCLI – Introduction
  2. Useful Tools
  3. Basic commands to generate and export reports
  4. Monitoring VMs with PowerCLI
  5. Managing VMs using PowerCLI
  6. Managing multiple VMs based on their tags
  7. Monitoring ESXi hosts with PowerCLI
  8. Managing ESXi hosts using PowerCLI
  9. Managing virtual networks using PowerCLI
  10. Managing Cluster-wide settings using PowerCLI
  11. Complete ESXi configuration with a single script

 

If you have any sugestions what else should be included in such course, do not hesitate to contact me via comments.