gpt4 book ai didi

powershell - Powershell和Plink-如何捕获消息 “Access Denied”

转载 作者:行者123 更新时间:2023-12-03 00:20:16 26 4
gpt4 key购买 nike

我在文本文件中有一个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
}

关于powershell - Powershell和Plink-如何捕获消息 “Access Denied”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11996069/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com