Jump to content
Sign in to follow this  
support

Validate Password for Linux Account (by Click Studios)

Recommended Posts

Script Name


Validate Password for Linux Account


 


Description


Checks if a Linux Account Password is correct


 


System Requirements


  • SSH access to the remote Host

  • Firewall allowing access

Successfully Tested Against


  • Redhat

  • Ubuntu

  • Fedora

  • Suse

  • CentOS

Failed Testing Against


  • None reported 

 


PowerShell Script



<#
.SYNOPSIS
Connect to a Linux host via SSH, and change the password for an account.
#>
function Validate-LinuxHostPassword
{
[CmdletBinding()]
param (
[String]$HostName,
[int]$Port,
[String]$UserName,
[String]$CurrentPassword
)

try
{
#Make a connection to the host
Add-Type -Path "[PasswordstateBinFolderPath]\Renci.SshNet.dll" #Include SSH.NET Assembly
$connectionInfo = New-Object Renci.SshNet.PasswordConnectionInfo($HostName, $Port, $UserName, $CurrentPassword)
$connectionInfo.Timeout = New-TimeSpan -Seconds 30

$sshclient = New-Object Renci.SshNet.SshClient($connectionInfo)
$sshclient.Connect()
$sshclient.SendKeepAlive()

if ($sshclient.IsConnected)
{
Write-Output "Success"
}
else
{
Write-Output "Failed to connect to the Host '$HostName' to validate the password for the account '$UserName'. Please check the Host is online, and accessible on the network."
}

#Dispose of SSH Client
$sshclient.Disconnect()
$sshclient.Dispose()
}
catch
{
switch -wildcard ($error[0].Exception.ToString().ToLower())
{
"*Permission denied (password)*" { Write-Output "Failed to validate password for account '$UserName' on Host '$HostName'. Please check the UserName and Password are correct."; break }
"*failed to respond*" { Write-Output "Failed to connect to the Host '$HostName' to validate the password for the account '$UserName'. Please check the Host is online and accessible on the network."; break }
"*No such host is known*" { Write-Output "Failed to connect to the Host '$HostName' to validate the password for the account '$UserName'. Host not found."; break }
"*Socket read operation has timed out*" { Write-Output "Failed to connect to the Host '$HostName' to validate the password for the account '$UserName'. Please check the port number for the Host is correct."; break }
#Add other wildcard matches here as required
default { Write-Output "Failed to validate password for account '$UserName' on Host '$HostName'. Error = " $error[0].Exception }
}
}
}

#Make a call to the Validate-LinuxHostPassword function
Validate-LinuxHostPassword -HostName '[HostName]' -Port '[RemoteConnectionPort]' -UserName '[UserName]' -CurrentPassword '[CurrentPassword]'

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×