我在文本文件中有一个ips列表。该脚本读取ips列表并尝试连接到每个ips。这部分正在运作。
当脚本找到无权访问用户的IP时,会返回一些消息。我想捕获字符串“Access denied”以创建一个无权访问我的用户的ips列表。
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
script.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
以下条件始终为EMPTY
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:
答案 0 :(得分:0)
我没有尝试过您的代码,但可能是您的变量$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
}
答案 1 :(得分:0)
我有同样的问题。不幸的是,我发现:&#34;成绩单不捕获EXE的任何输出。&#34;这是来自https://rkeithhill.wordpress.com/2007/09/16/effective-powershell-item-7-understanding-output/的旧帖子,但我认为它仍然是实际的。