簡體   English   中英

Powershell&Plink-如何捕獲“訪問被拒絕”消息

[英]Powershell & Plink - How to capture the message “Access Denied”

我在文本文件中有一個IP列表。 該腳本讀取ip列表,並嘗試連接到每個ip。 這部分正在工作。

當腳本找到無法訪問用戶的IP時,將返回一些消息。 我想捕獲字符串“拒絕訪問”以創建對我的用戶無權訪問的ip列表。

    PS X:\PATH\USER> .\script.ps1
    XXX.XXX.XXX.XXX
    Access denied
    Access denied
    Access denied
    Access denied
    Access denied
    FATAL ERROR: Server sent disconnect message
    type 2 (protocol error):
    "Too many authentication failures for XXXX"
    Using username "XXXX".
    Access denied

文件(ips.txt)內包含:

    xxx.xxx.xxx.xx1
    xxx.xxx.xxx.xx2
    xxx.xxx.xxx.xx3
    xxx.xxx.xxx.xx4
    .
    .
    .
    xxx.xxx.xxx.xxn

腳本.ps1

    $ipsarray=(get-content ips.txt)
    $size=(get-content ips.txt).Length

    Function Invoke-SSHCommands {
        Param($Hostname,$Username,$Password, $CommandArray, $PlinkAndPath, $ConnectOnceToAcceptHostKey = $true)

        $Target = $Username + '@' + $Hostname

        $plinkoptions = "-ssh $Target -pw $Password"

        $CommandArray += "exit"
        $remoteCommand = ""

        if($ConnectOnceToAcceptHostKey)
        {
            $PlinkCommand  = [string]::Format('echo y | & "{0}" {1} exit', $PlinkAndPath, $plinkoptions )
            $msg = Invoke-Expression $PlinkCommand
        }

        $PlinkCommand = [string]::Format('& "{0}" {1} "{2}"', $PlinkAndPath, $plinkoptions , $remoteCommand)
        $msg = Invoke-Expression $PlinkCommand
    }

    $PlinkAndPath = "XXXX\plink.exe"
    $Username = "XXX"
    $Password = "XXX"

    for ($i=0; $i -lt $size; $i++) {
        $Hostname=$ipsarray[$i]
        Write-Host $Hostname
        Invoke-SSHCommands -User $Username -Hostname $Hostname -Password $Password -PlinkAndPath $PlinkAndPath -CommandArray $Commands
    }      

嗨,大衛。 我將重復結構中的代碼更改為

for ($i=0; $i -lt $tamanho; $i++) {
  $Hostname=$vetor[$i]
  Write-Host $Hostname
  $response = Invoke-SSHCommands -User $Username -Hostname $Hostname -Password $Password -PlinkAndPath $PlinkAndPath -CommandArray $Commands

  if ($null -ieq $response) {
    write-host "EMPTY"
  }
  else {
    write-host $response
  } 
}

該行正在與操作系統交互。 我看不到如何捕獲返回的內容。 響應被發送到終端,換句話說,不返回腳本。

$response = Invoke-SSHCommands -User $Username -Hostname $Hostname -Password $Password -PlinkAndPath $PlinkAndPath -CommandArray $Commands

以下條件始終為空

  if ($null -ieq $response) {
    write-host "EMPTY"
  }
  else {
    write-host $response
  } 

我更改了if語句以檢查變量的類型

if ($null -ieq $response) {
  $response.GetType().Name
  write-host "EMPTY"
}

變量$ response的結果始終為null

You can not call a method on a null expression.
X:\PATH\USER\script.ps1:xx caractere:xx
+     $response.GetType <<<< ().Name
+ CategoryInfo          : InvalidOperation: (GetType:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

現在,我決定測試變量$ msg返回的結果,並將代碼行更改為:

$msg = Invoke-Expression $PlinkCommand
return $msg

更改循環(for)的示例以測試變量$ msg的返回。

for ($i=0; $i -lt $tamanho; $i++) {
  $Hostname=$vetor[$i]
  Write-Host $Hostname
  $response = Invoke-SSHCommands -User $Username -Hostname $Hostname -Password $Password -PlinkAndPath $PlinkAndPath -CommandArray $Commands

  if ($null -eq $response) {
    write-host "NULL"
  }
  else {
   write-host $response
  } 
}

函數調用后if中顯示的內容示例。 從不返回“訪問被拒絕”

user@xxx.xxx.xxx.xxx's password:  user@xxx.xxx.xxx.xxx's password:  user@xxx.xxx.xxx.xxx's password:  user@xxx.xxx.xxx.xxx's password:  root@xxx.xxx.xxx.xxx's password:

我沒有嘗試過您的代碼,但是想必響應是通過$msg變量捕獲的,因此似乎您需要在函數中添加return語句,例如

return $msg

然后,您需要對主程序中的返回值執行某些操作,例如

for ($i=0; $i -lt $size; $i++) {
    $Hostname=$ipsarray[$i]
    Write-Host $Hostname
    $response = Invoke-SSHCommands -User $Username -Hostname $Hostname -Password $Password -   PlinkAndPath $PlinkAndPath -CommandArray $Commands
    # Do something here with $response
}   

我有同樣的問題。 不幸的是,我發現:“腳本無法捕獲EXE的任何輸出。” 這是來自https://rkeithhill.wordpress.com/2007/09/16/effective-powershell-item-7-understanding-output/的舊帖子,但我認為它仍然是實際的。

暫無
暫無

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

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