簡體   English   中英

遠程執行 Shell 腳本到 Azure Linux VM

[英]Excute Shell Script remotely to Azure Linux VM

我正在嘗試執行 shell 腳本到 azure linux 虛擬機使用 Z2F2D395F0EA838474384

為什么我使用 powershell?

虛擬機具有從存儲帳戶復制的 VHD,這意味着它沒有 Azure VM 代理

所以我不能使用:

  • azure 虛擬機擴展
  • azure 虛擬機運行

我還嘗試使用帶有 ssh 模塊的自動化運行手冊並得到了這些錯誤:

Exception calling "Connect" with "0" argument(s): "Server HMAC algorithm not found" At C:\Modules\User\SSH\SSH.psm1:68 char:5 + $SSHConnection.Connect() + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SshConnectionException

Exception calling "RunCommand" with "1" argument(s): "Client not connected." At C:\Modules\User\SSH\SSH.psm1:69 char:5 + $ResultObject = $SSHConnection.RunCommand($ScriptBlock.ToString() ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SshConnectionException

這是基於我對缺少KexAlgorithms的vm的理解來描述SSH守護進程支持哪些方法

我現在要做的是將 ssh 放入 vm 並使用 powershell 執行命令

這是我現在得到的(一個 Powershell 腳本到 ssh 進入 vm 並執行命令):

$Password = "pwd"
$User = "pwd"
$ComputerName = "ip adress"
$Command = "touch tst.txt"

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

echo 'logging...'
$SessionID = New-SSHSession -ComputerName $ComputerName -Credential $Credentials #Connect Over SSH
echo 'Executing...'
$output = (Invoke-SSHCommand -Index $SessionID -Command $Command).Output
Remove-SSHSession -Name $SessionID | Out-Null

我收到此錯誤:

Invoke-SshCommand : A parameter cannot be found that matches parameter name 'Index'.
At C:\Users\octoadmin\Desktop\sign in.ps1:11 char:30
+ $output = (Invoke-SSHCommand -Index $SessionID -Command $Command).Out ...
+                              ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-SshCommand], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Invoke-SshCommand

我在互聯網上查看,但找不到任何東西。 顯然 ssh 調用命令找不到帶有索引 $SessionID 的 session 但我不知道問題到底出在哪里

希望有人能引導我走向正確的方向。

使用以下代碼更新 Posh-SSH 對我有用:

安裝 Posh-SSH:

Install-Module -Name Posh-SSH -RequiredVersion 2.1

劇本:

$Command = "fetch $scripturl; sh script.sh"
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)
$ComputerName = Get-AzPublicIpAddress -ResourceGroupName $RG -Name $IPName | Select-Object -ExpandProperty ipAddress
echo 'ip is : '
echo $ComputerName
echo 'logging...'
$SessionID = New-SSHSession -ComputerName $ComputerName -AcceptKey -Credential $Credentials 
echo 'Exucuting...'
$Query = (Invoke-SshCommand -SSHSession $SessionID  -Command $Command).Output
echo $Query
Remove-SSHSession -Name $SessionID | Out-Null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM