Showing posts with label esxcli. Show all posts
Showing posts with label esxcli. Show all posts

2012-02-23

How to Set CDP on a vSwitch–the #PowerCLI Way

We all know that you get CDP information in a number of ways (also PowerCLI), KB 1007069 provides a number of ways to do it.

But how would you go about setting CDP on the vSwitch?

That can be done on the host itself in a number of ways:

esxcfg-vswitch –B both vSwitch0

but also with esxcli

esxcli network switch standard set –c both –v vSwitch0

That got me thinking…. esxcli… where had I used that before – of course Netstat for ESXi.

So why not try and do the same here?

The quick and dirty way (I will update later with a proper function)

$esxcli = Get-EsxCli -VMHost $myhost
$esxcli.network.vswitch.standard.set("both","1500","vSwitch0")

Why all 3 Parameters and where did they come from?

$esxcli.network.vswitch.standard | gm

The set method is described as follows:

Name          MemberType    Definition
set              CodeMethod   boolean set(string cdpstatus, long mtu, string vswitchname)

And where do get the values that are valid for “cdpstatus” ? esxcli of course

# esxcli network vswitch standard set

Error: Missing required parameter -v|--vswitch-name

Usage: esxcli network vswitch standard set [cmd options]

Description:
  set                   This command sets the MTU size and CDP status of a given virtual switch.

Cmd options:
  -c|--cdp-status=<str> The CDP status of the given virtual switch. It can be 'down', 'listen', 'advertise' or 'both'
  -m|--mtu=<long>       The MTU size of the given virtual switch.
  -v|--vswitch-name=<str>
                        The name of virtual switch to apply the configurations. (required)

Update: March 06, 2012

I promised to update with a proper function on how to do this, and here it is:

Function Set-VirtualSwitchCDP {

      <#

            .SYNOPSIS
                  Set the CDP setting on a Standard Virtual Switch.
            .DESCRIPTION
                  Using the Get-Esxcli cmdlet you can changed the CDP Settings 
                  on virtual switch on an ESX host.
            .NOTES Author: Maish Saidel-Keesing
            .PARAMETER  VMHost
                  ESX server to perform the function.
            .PARAMETER  MTU
                  The MTU settings of the vSwitch, default is 1500.Valid values are 
                  between 1500-9000
            .PARAMETER  CDP
                  The CDP setting for the vSwitch. 
                  Valid values are 'down', 'listen', 'advertise' or 'both'.
            .PARAMETER  vSwitch
                  The Name of the Standard vSwitch on which to perform the action.
            .EXAMPLE
                  PS C:\> Set-VirtualSwitchCDP -VMhost esx1 -MTU 1500 -CDP both -vSwitch vSwitch0
            .EXAMPLE
                  PS C:\> Get-VMhost Myhost | Set-VirtualSwitchCDP -vSwitch vSwitch0
            .LINK
                  http://technodrone.blogspot.com/2012/02/how-to-set-cdp-on-vswitchthe-powercli.html
      #>

    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='High')]
    Param(
    [Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True)]
    [String]
    $VMHost,

    [Parameter(Position=1)]
    [ValidateRange(1500,9000)]
    [Int]
    $MTU = 1500,

    [Parameter(Position=2)]
    [ValidateSet("down","listen","advertise","both")]
    [String]
    $CDP = "both",

    [Parameter(Position=3,Mandatory=$True)]
    [String]
    $vSwitch
    )

     

      Process
      {
       if ($pscmdlet.ShouldProcess($VMHOST,"Updating $vSwitch with MTU $MTU and CDP setting of $CDP"))      {
                  foreach ($hostobject in (Get-VMHost $VMHost)) {
                        $esxcli = Get-EsxCli -VMHost $hostobject
                        $esxcli.network.vswitch.standard.set($CDP,$MTU,$vswitch)
                        $esxcli.network.vswitch.standard.list()
                  }
            }
      }
}

2011-09-13

netstat for ESXi

The title of the post is actually misleading - on purpose - because there is no netstat for ESXi. The reason that I bring this up today is because of a Twitter conversation from today regarding SSH access and VMkernel interfaces. I was looking to see which ports were open and what interfaces were listening.
But that is a different post.

What is netstat? according to Wikipedia:

netstat (network statistics) is a command-line tool that displays network connections (both incoming and outgoing), routing tables, and a number of network interface statistics. It is available on Unix, Unix-like, and Windows NT-based operating systems.

Why would you use it? For one thing for example, to check if a host has an open connection on a certain port, if it is listening on a certain port - for troubleshooting purposes would be the proper answer.

So how do you get that information on ESXi?

Trying netstat on an ESXi host does not work - because that command is not there - see the screenshot below.

No netstat

Well that is not good - if the command is not in the busybox console then how would you go about getting that information? Well of course the clever people at VMware have already thought about this and have exposed all this information through esxcli. William Lam wrote a great set of posts on esxcli
esxcli Part1 - What is esxcli?, esxcli Part2 - Automating esxcli using vMA and esxcli Part3 - Automating esxcli using PowerShell

This is how you would go about getting the information from esxcli. (Be aware the command differ according to the different versions - 4.x is not the same as 5.x)

esxcli network ip connection list

esxcli1

That is fine and dandy - but to get that info you need to either:

  1. have access to the DCUI (and have it enabled of course)
    or
  2. access remotely with SSH (and also have it enabled of course)

But what if you do not want to enable neither of the above - that means you have to do it remotelyand for that you have two options, vCLI or PowerCLI.

The vCLI way

esxcli --server esx1.maishsk.local network ip connection list

vcli1

But me being more of PowerCLI guy I would do it like this.

The PowerCLI way

$esxcli = get-esxcli -vmhost esx1.maishsk.local

$esxcli.network.ip.connection.list() | ft

PowerCLI1

Output is almost identical - just that in the case of PowerCLI the values are returned as a set of objects - a  VMware.VimAutomation.ViCore.Impl.V1.EsxCli.EsxCliObjectImpl object to be precise. Once these presented as objects I can start to mold and dice my results to my liking.

For example - I would like to check if there is any connections open on port 80 (http) - with vCli - this is not so simple - because you are working essentially in a DOS window - so filtering is not the easiest with findstr. Using the console or SSH is easier - a simple grep will work as you can see below.

esxcli network ip connection list | grep :80

esxcli2

With PowerCLI

$esxcli.network.ip.connection.list() | where { $_.LocalAddress -like "*:80" } | ft

PowerCLI2

I hope you can see that the options this way are pretty much endless - like filtering all connections to show only those from a specific IP, or a complete subnet.

So that is how you netstat on ESXi….