【问题标题】:Can you embed the private key into a PowerShell WinSCP script?您可以将私钥嵌入到 PowerShell WinSCP 脚本中吗?
【发布时间】:2020-05-27 08:44:16
【问题描述】:

您能否将私钥嵌入到 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......................"
}

$session = New-Object WinSCP.Session

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

    # Your code
}
finally
{
    $session.Dispose()
}

【问题讨论】:

  • 有什么理由不能只引用一个文件?
  • 我只是将此脚本上传到远程计算机。

标签: powershell ssh scp winscp winscp-net


【解决方案1】:

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()
}

【讨论】:

    猜你喜欢
    • 2010-12-26
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    相关资源
    最近更新 更多