简体   繁体   English

Powershell&Plink-如何捕获“访问被拒绝”消息

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

I have a list of ips in a text file. 我在文本文件中有一个IP列表。 The script reads the list of ips and attempts a connection to each of the ips. 该脚本读取ip列表,并尝试连接到每个ip。 This part is working. 这部分正在工作。

When the script finds an IP that does not have access to the user some messages are returned. 当脚本找到无法访问用户的IP时,将返回一些消息。 I want to capture the string "Access denied" to create a list of ips that do not have access to the my user. 我想捕获字符串“拒绝访问”以创建对我的用户无权访问的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

Inside the file (ips.txt) contains: 文件(ips.txt)内包含:

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

The script.ps1 脚本.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
    }      

Hi David. 嗨,大卫。 I changed the code inside the repeating structure for 我将重复结构中的代码更改为

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

This line is interacting with the operating system. 该行正在与操作系统交互。 I do not see how to capture what it is returning. 我看不到如何捕获返回的内容。 The response is sent to the terminal, in other words, does not return to the script. 响应被发送到终端,换句话说,不返回脚本。

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

The following conditional is always EMPTY 以下条件始终为空

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

I changed the if statement to check the type of the variable 我更改了if语句以检查变量的类型

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

The result of the variable $response is always null 变量$ 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

Now I decided to test what would be the result returned by the variable $msg and changed lines of code to: 现在,我决定测试变量$ msg返回的结果,并将代码行更改为:

$msg = Invoke-Expression $PlinkCommand
return $msg

Example of the loop(for) changed to test the return of the variable $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
  } 
}

Example of what is displayed in the if, after the function call. 函数调用后if中显示的内容示例。 Never return "Access denied" 从不返回“访问被拒绝”

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:

I haven't tried your code, but presumably, the response is captured by your variable $msg , so it seems you need to add a return statement to your function, eg 我没有尝试过您的代码,但是想必响应是通过$msg变量捕获的,因此似乎您需要在函数中添加return语句,例如

return $msg

Then you need to do something with that return in your main program, eg 然后,您需要对主程序中的返回值执行某些操作,例如

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
}   

I have same problem. 我有同样的问题。 Unfortunatly i find out that: "Transcripts don't capture any output from EXEs." 不幸的是,我发现:“脚本无法捕获EXE的任何输出。” This is old post from https://rkeithhill.wordpress.com/2007/09/16/effective-powershell-item-7-understanding-output/ but i think it is still actual. 这是来自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