您可以将私钥嵌入到Powershell winSCP脚本中吗?

问题描述 投票:1回答:1

是否可以将私钥嵌入Powershell winSCP脚本中,而不是调用.ppk文件?也许像这样吗?

# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Scp
    HostName = "domain.com"
    UserName = "username"
    SshHostKeyFingerprint = "ssh-rsa 2048 38741934871934871293471293487"
    SecurePrivateKeyPassphrase = "AAABAHH1......................"
}

$sessionOptions.AddRawSettings("KEX", "rsa,ecdh,dh-gex-sha1,dh-group14-sha1,WARN,dh-group1-sha1")

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Your code
}
finally
{
    $session.Dispose()
}
powershell winscp
1个回答
0
投票

WinSCP .NET程序集仅支持从文件读取密钥。

但是您可以通过脚本创建密钥文件:

$privateKeyPath = New-TemporaryFile
Set-Content -Path $privateKeyPath "PuTTY-User-Key-File-2: ssh-rsa
Encryption: none
Comment: ...
Public-Lines: 6
...
Private-Lines: 14
...
Private-MAC: ...
"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    ...
    SshPrivateKeyPath = $privateKeyPath
}

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Your code
}
finally
{
    Remove-Item $privateKeyPath
    $session.Dispose()
}
© www.soinside.com 2019 - 2024. All rights reserved.