在SSH中将文件从W10附加到Linux(Posh-SSH模块)

时间:2019-07-24 09:32:16

标签: linux powershell ssh append

我在W10环境和Powershell中。 我想将本地ssh公钥(在W10中)附加到Linux服务器上的authorized_keys中,而无需手动输入密码。 为此,我使用了:

Get-Content C:\Users\xxxxx\.ssh\id_rsa.pub | ssh pi@localhost -p 2222 "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys || exit 1"

没关系。但是我无法通过ssh password作为参数。是个问题。

所以我使用了Posh-SSH模块,它提供了这个未来:

New-SSHSession -ComputerName $ComputerName -Credential $Credentials -Port $Port

连接正常。但是我不明白如何转换此命令:

Get-Content C:\Users\xxxxx\.ssh\id_rsa.pub | ssh pi@localhost -p 2222 "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys || exit 1"

或这一个(相同,但在Linux中使用“ <”):

ssh pi@localhost -p 2222 "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys || exit 1" < C:\Users\xxxxx\.ssh\id_rsa.pub

通过使用Posh-SSH ...

这是我的代码(当然,最后一行不起作用,这正是我尝试过的):

Import-Module Posh-SSH
$Password = "raspwd"
$User = "pi"
$ComputerName = Read-Host -Prompt 'Input Raspberry IP'
$Port = Read-Host -Prompt 'Input Raspberry ssh port'
$Command = "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys || exit 1"

$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)

$SessionID = New-SSHSession -ComputerName $ComputerName -Credential $Credentials -Port $Port #Connect Over SSH

Get-Content C:\Users\xxxxx\.ssh\id_rsa.pub | Invoke-SSHCommand -Index $sessionid.sessionid -Command $Command # Invoke Command Over SSH

0 个答案:

没有答案