使用 PowerShell 通过 PuTTY 更改我的 RHEL root 密码,但我不知道我将密码更改为什么

por*_*min 8 linux powershell redhat root putty

基本上是标题。我的朋友提供了一个脚本来通过 Powershell 和 PuTTY 批量更改 RHEL 密码,但是当我尝试登录时,我输入的新密码不起作用。我认为问题是它没有转义一个特殊字符在新密码中,但我不知道新密码是什么。

我使用的“新密码”类似于:a1b2c3d"4e5f6g7

我尝试将安全字符串替换为常规字符串,或者使用 telnet 而不是 SSH 与数据包捕获来确定发送的究竟是什么,但到目前为止这些都没有奏效。

System.Management.Automation.PSCredential -argumentlist "root",$newrootPassword
     $newrootPassword2 = Read-Host "Retype new root password" -AsSecureString
     $newrootCredential2 = new-object -typename System.Management.Automation.PSCredential -argumentlist "root",$newrootPassword2


    putty.exe -ssh -pw $oldrootCredential.GetNetworkCredential().Password root@$_

    echo y | plink.exe -ssh -v -pw $oldrootCredential.GetNetworkCredential().Password root@$_ "echo root:'$newrootPassword' | chpasswd" 2>&1 
Run Code Online (Sandbox Code Playgroud)

我希望新密码是 a1b2c3d"4e5f6g7;但是,这在登录时不起作用。

Mal*_*ery 16

问题是您试图将 SecureString 传递给需要标准字符串的内容。Password 属性采用 SecureString 格式,您将无法将其传递给 plink,它只会被翻译为System.Security.SecureString如果密码更改确实有效,这就是将设置的密码。

要将 SecureString 转换为适合 plink 命令的文本格式,您需要使用此处的示例函数

function Get-PlainText()
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [System.Security.SecureString]$SecureString
    )
    BEGIN { }
    PROCESS
    {
        $bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString);

        try
        {
            return [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr);
        }
        finally
        {
            [Runtime.InteropServices.Marshal]::FreeBSTR($bstr);
        }
    }
    END { }
}
Run Code Online (Sandbox Code Playgroud)

Write-Host在使用实际的 plink.exe 进行测试之前,您可以通过使用输出命令行值来测试您的命令。或者,您可以运行ProcMon并筛选 Operation is Process Create,然后当您看到 plink.exe 启动时,您可以使用属性查看正在传递的完整实际命令行。

  • 亲爱的主。地球上谁会......这是很多级别的错误(不是你,PowerShell)。不管怎样,你有我的赞成票只是因为有勇气深入深渊。 (6认同)