When you work in a company as an administrator you would often find yourself changing passwords regularly. Through the years it gets tiresome to change passwords multiple times across multiple hosts. I try to keep my passwords the same which is not best practice, but it helps me remember them better. Plus, I will change the passwords again in a month anyway. With added, VPN/firewall in place, I think it is not a big deal to keep your admin passwords the same for the month.
I have written a PowerShell script to change my UNIX password for this purpose. It requires the Posh-SSH Powershell module that you can get from the PSGallery.
To install the module run this command in your Powershell terminal:
Install-Module -Name Posh-SSH -Scope AllUsers
This script was tested and worked on Powershell 7.4.1.
Please note that when you run the script it will prompt you for your old and new password. This will be shown in clear text on the screen. This is on purpose so you can remember or write down the password for safekeeping.
For slower connections to the UNIX hosts, you can adjust the Start-Sleep duration. There are two Start-Sleep in the code, first is for the sleep in between the passwd dialog, and the 2nd is for how fast the host returns a prompt to you after you log in. Please adjust these two variables based on your environment.
## SCRIPT START ##
Clear-Host
$userName = "yourUNIXid"
$SleepBetweenPasswordDialog = 10
$SleepAfterLogin = 60
$oldPassInput = Read-Host "Please enter your old password"
$oldPassword = ConvertTo-SecureString -AsPlainText $oldPassInput -Force
$newPassInput = Read-Host "Please enter your new password"
$newPassword = ConvertTo-SecureString -AsPlainText $newPassInput -Force
$UNIXCred = New-Object System.Management.Automation.PSCredential -ArgumentList $userName,$oldPassword
$UNIXServerArray = @("unixHost1", "unixHost2")
Write-Output "`n`n"
foreach ($UNIXServer in $UNIXServerArray)
{
Write-Output "*** Changing password in $UNIXServer ***`n`n"
$session = New-SSHSession -ComputerName $UNIXServer -Credential $UNIXCred
$shellStream = New-SSHShellStream -SessionId $session.SessionId
# How fast your UNIX host take you to a prompt after login
Start-Sleep -Seconds $SleepAfterLogin
$command = "passwd"
$shellStream.WriteLine("$command")
Start-Sleep -Seconds $SleepBetweenPasswordDialog
#pass in old password
$shellStream.WriteLine([Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($oldPassword)))
Start-Sleep -Seconds $SleepBetweenPasswordDialog
#pass in current password
$shellStream.WriteLine([Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($newPassword)))
Start-Sleep -Seconds $SleepBetweenPasswordDialog
#pass in current password
$shellStream.WriteLine([Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($newPassword)))
Start-Sleep -Seconds $SleepBetweenPasswordDialog
# Clear buffer, should see "password updated" message
$shellStream.Read()
$shellStream.Dispose()
$session | Remove-SSHSession
Write-Output "`n`n`n"
}
## SCRIPT END ##