PowerCLI: enable SSH and configure ESXi host Firewall

June 17, 2013

Table of Contents

Here is a post to help you be able to configure SSH to start automatically. This is done by PowerCLI scripts that hide the shell warning message and configure the ESXi firewall to allow connection from specific  IP addresses.

You first have to know where you can change the ESXi firewall settings. Here are the steps:

  • go to the configuration tab
  • select the security profile
  • select the rule you want to change and click on the firewall
  • select the option “Only allow connections from the following networks” and add the IP address or IP range you want to allow.

Below is the script that performs, you just have to change the $cluster and $ip variables. Copy the script to your PowerCLI session and run it.

$cluster = “<clusterName>”

$ip = “192.168.1.1”

foreach($vmHost in (Get-Cluster $cluster | Get-VMHost | Sort Name)){

    write-host “Configuring SSH on host: $($vmHost.Name)” -fore Yellow

    if((Get-VMHostService -VMHost $vmHost | where {$_.Key -eq “TSM-SSH”}).Policy -ne “on”){

        Write-Host “Setting SSH service policy to automatic on $($vmHost.Name)”

        Get-VMHostService -VMHost $vmHost | where { $_.key -eq “TSM-SSH” } | Set-VMHostService -Policy “On” -Confirm:$false -ea 1 | Out-null

    }

    if((Get-VMHostService -VMHost $vmHost | where {$_.Key -eq “TSM-SSH”}).Running -ne $true){

        Write-Host “Starting SSH service on $($vmHost.Name)”

        Start-VMHostService -HostService (Get-VMHost $vmHost | Get-VMHostService | Where { $_.Key -eq “TSM-SSH”}) | Out-null

    }   

    $esxcli = Get-EsxCli -VMHost $vmHost

    if($esxcli -ne $null){

        if(($esxcli.network.firewall.ruleset.allowedip.list(“sshServer”) | select AllowedIPAddresses).AllowedIPAddresses -eq “All”){

            Write-Host “Changing the sshServer firewall configuration”       

            $esxcli.network.firewall.ruleset.set($false, $true, “sshServer”)

            $esxcli.network.firewall.ruleset.allowedip.add(“$ip”, “sshServer”)

            $esxcli.network.firewall.refresh()

        }   

    }

    if(($vmHost | Get-AdvancedSetting | Where {$_.Name -eq “UserVars.SuppressShellWarning”}).Value -ne “1”){

        Write-Host “Suppress the SSH warning message”

        $vmHost | Get-AdvancedSetting | Where {$_.Name -eq “UserVars.SuppressShellWarning”} | Set-AdvancedSetting -Value “1” -Confirm:$false | Out-null

   }

}

The script is very handful, it checks if the SSH Service is running or not and will change the setting if necessary, also goes for the firewall configuration and the part to suppress the Shell warning message.

Home

Share on
Facebook
Twitter
LinkedIn
Pinterest
More posts

Dedicated Servers Quick Guide

What is a Dedicated Servers? Why bother using a dedicated server over a VPS or Shared Hosting?A dedicated server is a server 100% dedicated to your website/project or business needs.

How to be a PowerCLI Ninja

Many people are becoming curious about the powershell/powerCLI and how to become a Powershell/PowerCLI ninja and I think the following tips will help one go a long way to become

Physical Memory Limits for Windows Releases

Physical Memory Limits: Windows 7 The following table specifies the limits on physical memory for Windows 7. Version Limit on X86 Limit on X64 Windows 7 Ultimate 4 GB 192 GB Windows 7 Enterprise

Power combined: PowerCLI and OVFTool

Power combined: Combining two good things creates wonders. In this post I will show how to combine the automation abilities of PowerCLI with OVFTool. You first have to get some

vSphere 5.1 New Storage Features

VMFS File Sharing Limits In previous versions of vSphere, the maximum number of hosts which could share a read-only file on a VMFS volume was 8. The primary use case

Get 90% Discount

First 3 People gets the Bonus!
Don't Miss Out Our Big Sale

Get 0-90% On All
247Rack Services

247Rack

The Sale Is Until The End Of March