Tuesday, December 31, 2013

Change ssh banner / motd message file on all esxi servers using powercli


I got task to update ssh banner message on all the all esxi servers. (Which shows while logging through ssh). This is an easy task using host profiles. But I thought give a shot with powercli to update all esxi servers.

Here idea is to modify /etc/motd file on esxi server and put your banner text. all this you need to using ssh into esxi host from windows.

Default banner page:
Here I have used plink.exe utility from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html which is command line tool for ssh from windows. I downloaded it and copied it on my c:\windows\system32.

Next I created one text file called motd.txt and put it under c:\scripts folder for easy access and the content of the files are as in the screenshot.
#Note:  You can add line cp /etc/motd /etc/motd.bac on first line to backup motd file.
first line echo "      +=======" > /etc/motd (Be aware - only one greater than sign will replace/overwrite everything in the /etc/motd file)
rest of the lines echo " |        |" >> /etc/motd" (double greater than sign will append data)

Now to ssh into esxi server SSH service must be running, which I have enabled through powercli

Note: I have tested this script in my lab environment first.Make sure password for the all esxi are same.
 #####################################   
  ## http://kunaludapi.blogspot.com   
  ## Version: 1   
  ## Tested this script on   
  ## 1) Powershell v3   
  ## 2) Powercli v5.5   
  ## 3) Vsphere 5.x   
  ####################################  
 $cred = Get-Credential -Message "Vcenter or esxi username and password"  
 Connect-Viserver vcenterserver -Credential $cred  
 Add-PSSnapin vmware.vimautomation.core  
 $command = "C:\scripts\motd.txt"  
 $esxiHosts = Get-VMHost   
 $root = "root"  
 $Passwd = "Newpassword"  
 foreach ($esxiHost in $esxiHosts) {  
   $SSHservice = $esxiHost | Get-VMHostService | where {$psitem.key -eq "tsm-ssh"}  
   if ($SSHservice.Running -eq $False) {  
     $esxiHost | Get-VMHostService | where {$psitem.key -eq "tsm-ssh"} | Start-VMHostService      
   }  
   Write-Output "yes" | plink.exe -ssh root@$esxihost -P 22 -pw $passwd -m $command  
   $esxiHost | Get-VMHostService | where {$psitem.key -eq "tsm-ssh"} | Stop-VMHostService -Confirm:$false  
 }  

Output:

No comments: