【问题标题】:Used PowerShell to change my RHEL root passwords via PuTTY, but I don't know what I changed my password to使用 PowerShell 通过 PuTTY 更改我的 RHEL 根密码,但我不知道我将密码更改为什么
【发布时间】:2019-11-23 16:23:10
【问题描述】:

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

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

我尝试将安全字符串替换为常规字符串,或者使用 telnet 代替 SSH 并通过数据包捕获来确定发送的确切内容,但到目前为止,这些都没有奏效。

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
# Displays prompt
Write-Host "This will update the root password on the Linux Servers"

# Get the running directory
$rundirectory = Split-Path $MyInvocation.MyCommand.Path
#$rundirectory = Split-Path $rundirectory

# Get old root credential 
$oldrootPassword = Read-Host "Enter old root password" -AsSecureString
$oldrootCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "root", $oldrootPassword

# Get new root credential
$newrootPassword = Read-Host "Enter new root password" -AsSecureString
$newrootCredential = New-Object -TypeName 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

# $gc = get-content \linuxservers.txt
if ($newrootCredential.GetNetworkCredential().Password -ceq $newrootCredential2.GetNetworkCredential().Password) {
    $templogfile = $rundirectory + "\Temp\log.txt"
    $tempchfile = $rundirectory + "\Temp\pwd_changes.txt"
    $log = $rundirectory + "\Logs\RHEL\Password_Changes_$(Get-Date -f MMddyyyy).log"
    $newrootPassword = $newrootCredential.GetNetworkCredential().Password
    $serverlist = $rundirectory + "\linuxservers.txt"

    Get-Content $serverlist | %{
        # Connects to host and stores SSH key in case it does not have one already
        echo y | plink.exe -ssh -pw $oldrootCredential.GetNetworkCredential().Password root@$_ exit
        # Opens a session to the server to use for disaster recovery
        putty.exe -ssh -pw $oldrootCredential.GetNetworkCredential().Password root@$_
        # Adds delay to complete login before password is changed
        Start-Sleep -Milliseconds 900
        # Command sent to host to change password that is then logged
        echo y | plink.exe -ssh -v -pw $oldrootCredential.GetNetworkCredential().Password root@$_ "echo root:'$newrootPassword' | chpasswd" 2>&1 >> $templogfile
        # Parses file and stores output in variable
        $outpt = cat $templogfile | Select-String "Session sent command exit status"
        # Adds server name and variable to changes file
        echo `n $_.ToUpper() `n$outpt `n "------------------------------------" >> $tempchfile
        # Removes the log file to be used again in loop
        Remove-Item $templogfile
        # Opens second PuTTY session to make sure password works
        putty.exe -ssh -pw $newrootCredential.GetNetworkCredential().Password root@$_
    }
} else {
    $writehost = "ERROR: New root passwords do not match. Exiting..."
}

if ($writehost -ceq "ERROR: New root passwords do not match. Exiting...") {
    Write-Host "ERROR: New root passwords do not match. Exiting..."
} else {
    # Places contents of results file in variable
    $pwresults = cat $tempchfile
    # Adds comment at top of file and creates new results file
    echo "Investigate all servers that do not have a command exit status of 0" $pwresults >> $log
    # Removes the changes file
    Remove-Item $tempchfile
    # Opens results file for administrator to investigate
    Invoke-Item $log
}

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

【问题讨论】:

  • 附带说明:当您需要明文密码时,使用安全字符串有什么意义?
  • AFAICS 代码应该做你想做的事,除非你通过 SSH 运行的命令存在引用问题。请尝试设置一个不带引号的密码来检查。
  • 看起来它在没有引号的情况下可以工作,但它不能与引号一起工作。
  • 看起来它在没有引号的情况下可以工作,但它不能与引号一起工作。如果我尝试 a1b2c3d4e5f6g7 它可以正常工作并将其设置为密码。如果我尝试 a1b2c3d"4e5f6g7,它会将密码更改为我不知道的密码。
  • 向外部命令发送双引号对于 powershell 来说一直很棘手。这甚至对我在 osx 中都不起作用:/bin/ls 'hi"there' stackoverflow.com/questions/6714165/…

标签: powershell redhat putty rhel


【解决方案1】:

试试这个。反斜杠双引号。您可能已将其更改为没有双引号的密码。如果它们不起作用,您需要一些方法来撤消这些操作。

$newrootPassword = $newrootpassword -replace '"','\"'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2018-08-06
    • 2011-06-28
    • 1970-01-01
    • 2017-12-20
    • 2022-01-27
    • 1970-01-01
    相关资源
    最近更新 更多