大网数据广告活动
服务器运维问答

Used PowerShell to change my RHEL root passwords via PuTTY, but I don't know what I changed my password to

· 52CE 我爱测

Basically the title. My friend provided me a script to batch change RHEL passwords via Powershell and PuTTY, but the new password I entered doesn't work when I try to log in. I think the issue is that it doesn't escape one of the…

问题原文

原帖链接 · portland_admin · CC BY-SA 4.0

Basically the title. My friend provided me a script to batch change RHEL passwords via Powershell and PuTTY, but the new password I entered doesn't work when I try to log in. I think the issue is that it doesn't escape one of the special characters that's in the new password, but I can't figure out what the new password would have been.

The "new password" I used was similar to this: a1b2c3d"4e5f6g7

I attempted to replace the secure strings for regular strings, or use telnet instead of SSH with a packet capture to determine what exactly is being sent, but none of that has worked thus far.

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 

I expected the new password to be a1b2c3d"4e5f6g7; however, this does not work upon login.

社区高分解答

原帖链接 · Malcolm McCaffery · CC BY-SA 4.0

The problem is you are trying to pass SecureString into something that expects a standard string. The Password property is in SecureString format, you won't be able to pass this to plink, it will just get translated as System.Security.SecureString If the password change actually worked this is what password would have been set.

To translate a SecureString to text format suitable for plink command you need to use function like this example from here

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 { }
}

You can test your commands by using Write-Host to output command line value before testing with actual plink.exe. Or you can run ProcMon and filter on Operation is Process Create, then when you see plink.exe launched you can use properties to see the full actual command line that is getting passed.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-07-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。