talatham
2/4/2015 - 10:33 AM

Disable and stop SSH service on all hosts in a specified vCenter. Input parameter: VC:sdkConnection

Disable and stop SSH service on all hosts in a specified vCenter.

Input parameter: VC:sdkConnection

//Disable SSH on all hosts in a vCenter
//Tom Latham (04/02/2015)

//Get all host systems from vCenter
var hosts = vCenter.getAllHostSystems(null,null);
System.log (hosts.length + " hosts found in " + vCenter.name);
var countSSHEnabled = 0; //Count of servers with misconfigured SSH
var countSSHRemediated = 0;

//Iterate across all hosts
for each (var host in hosts)
{
	//DEBUG: Display all host names - System.log (host.name);
	
	//Iterate across each service on a host
	for each (var service in host.configManager.serviceSystem.serviceInfo.service)
	{
		//If the SSH service...
		if (service.key == 'TSM-SSH')
		{
			//... is not disabled
			if (service.policy != 'off')
			{
				countSSHEnabled ++;
				System.log ("Disabling SSH on " + host.name);
				
				try{
					host.configManager.serviceSystem.updateServicePolicy('TSM-SSH','off');
					countSSHRemediated ++;
				} catch(ex){
					System.error ("Error disabling SSH (" + ex + ")");
				}
			}
			
			//... is running
			if (service.running)
			{
				countSSHEnabled ++;
				System.log ("Stopping SSH on " + host.name);
				
				try{
					host.configManager.serviceSystem.stopService('TSM-SSH');
					countSSHRemediated ++;
				} catch(ex){
				
				}
			}
		}
	}
}

System.log (countSSHEnabled + " hosts have SSH enabled or running. " + countSSHRemediated + " have been remediated.");