1 Star 0 Fork 54

Fork Bomb/project_13384759

forked from chuanjiao10/kasini3000 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
sxa刷新所有被控机对象的_os类型属性psv7.ps1 18.28 KB
一键复制 编辑 原始数据 按行查看 历史
chuanjiao10 提交于 2020-11-27 16:48 . 修复node_script复制失败问题
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
#建议保存编码为:bom头 + utf8
$开始 = Get-Date
$输入并发数组 = New-Object 'System.Collections.Concurrent.ConcurrentBag[object]'
foreach ($private:当前被控机3 in $global:所有被控机)
{
$null = $输入并发数组.add($private:当前被控机3)
}
$输出并发数组 = New-Object 'System.Collections.Concurrent.ConcurrentBag[object]'
1..($global:所有被控机.count + 3) | ForEach-Object -ThrottleLimit 30 -Parallel {
function fh返回被控机的_开放端口信息
{
param
(
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$目的ip地址,
[String]$目的端口
)
if ($IsLinux -eq $True)
{
switch (${目的端口})
{
'22'
{
if (Test-Connection -TargetName $目的ip地址 -TcpPort 22 -Quiet)
{
& "${using:PSScriptRoot}/zkj_从主控机到linux被控机_ssh_knowhost免yes.ps1"
$结果 = 'linux'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的22端口不通"
$结果 = "被控机 ${目的ip地址} 开放端口未知"
return $结果
}
}
'135'
{
if (Test-Connection -TargetName $目的ip地址 -TcpPort 135 -Quiet)
{
$结果 = 'win135'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的135端口不通"
$结果 = "被控机 ${目的ip地址} 开放端口未知"
return $结果
}
}
'5985'
{
if (Test-Connection -TargetName $目的ip地址 -TcpPort 5985 -Quiet)
{
$结果 = 'win5985'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的5985端口不通"
$结果 = "被控机 ${目的ip地址} 开放端口未知"
return $结果
}
}
'5986'
{
if (Test-Connection -TargetName $目的ip地址 -TcpPort 5986 -Quiet)
{
$结果 = 'win5986'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的5986端口不通"
$结果 = "被控机 ${目的ip地址} 开放端口未知"
return $结果
}
}
Default
{
if ( ${目的端口} -eq '')
{
#扫描端口,速度较慢
if (Test-Connection -TargetName $目的ip地址 -TcpPort 5985 -Quiet)
{
$结果 = 'win5985'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的5985端口不通"
}
if (Test-Connection -TargetName $目的ip地址 -TcpPort 5986 -Quiet)
{
$结果 = 'win5986'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的5986端口不通"
}
if (Test-Connection -TargetName $目的ip地址 -TcpPort 135 -Quiet)
{
$结果 = 'win135'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的135端口不通"
}
if (Test-Connection -TargetName $目的ip地址 -TcpPort 22 -Quiet)
{
& "${using:PSScriptRoot}/zkj_从主控机到linux被控机_ssh_knowhost免yes.ps1"
$结果 = 'linux'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的22端口不通"
}
Write-Verbose "被控机 ${目的ip地址} 开放端口未知"
$结果 = "被控机 ${目的ip地址} 开放端口未知"
return $结果
}
else
{
if (Test-Connection -TargetName $目的ip地址 -TcpPort $目的端口 -Quiet)
{
Write-Verbose "被控机 ${目的ip地址} 端口 $目的端口 通了"
$结果 = "被控机 ${目的ip地址} 端口 $目的端口 通了"
return $结果
}
else
{
Write-Verbose "被控机 ${目的ip地址} 开放端口未知"
$结果 = "被控机 ${目的ip地址} 开放端口未知"
return $结果
}
}
}
}
}
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
switch (${目的端口})
{
'5985'
{
if (& 'tcp--ping-v2.ps1' $目的ip地址 5985 -Quiet)
{
$结果 = 'win5985'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的5985端口不通"
$结果 = "被控机 ${目的ip地址} 开放端口未知"
return $结果
}
}
'5986'
{
if (& 'tcp--ping-v2.ps1' $目的ip地址 5986 -Quiet)
{
$结果 = 'win5986'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的5986端口不通"
$结果 = "被控机 ${目的ip地址} 开放端口未知"
return $结果
}
}
'135'
{
if (& 'tcp--ping-v2.ps1' $目的ip地址 135 -Quiet)
{
$结果 = 'win135'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的135端口不通"
$结果 = "被控机 ${目的ip地址} 开放端口未知"
return $结果
}
}
'22'
{
if (& 'tcp--ping-v2.ps1' $目的ip地址 22 -Quiet)
{
& "${using:PSScriptRoot}/zkj_从主控机到linux被控机_ssh_knowhost免yes.ps1"
$结果 = 'linux'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的22端口不通"
$结果 = "被控机 ${目的ip地址} 开放端口未知"
return $结果
}
}
Default
{
if ( ${目的端口} -eq '')
{
#扫描端口,速度较慢
if (& 'tcp--ping-v2.ps1' $目的ip地址 5985 -Quiet)
{
$结果 = 'win5985'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的5985端口不通"
}
if (& 'tcp--ping-v2.ps1' $目的ip地址 5986 -Quiet)
{
$结果 = 'win5986'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的5986端口不通"
}
if (& 'tcp--ping-v2.ps1' $目的ip地址 135 -Quiet)
{
$结果 = 'win135'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的135端口不通"
}
if (& 'tcp--ping-v2.ps1' $目的ip地址 22 -Quiet)
{
& "${using:PSScriptRoot}/zkj_从主控机到linux被控机_ssh_knowhost免yes.ps1"
$结果 = 'linux'
return $结果
}
else
{
Write-Verbose "错误,被控机 ${目的ip地址} 的22端口不通"
}
Write-Verbose "被控机 ${目的ip地址} 开放端口未知"
$结果 = "被控机 ${目的ip地址} 开放端口未知"
return $结果
}
else
{
if (& 'tcp--ping-v2.ps1' $目的ip地址 $目的端口 -Quiet)
{
Write-Verbose "被控机 ${目的ip地址} 端口 $目的端口 通了"
$结果 = "被控机 ${目的ip地址} 端口 $目的端口 通了"
return $结果
}
else
{
Write-Verbose "被控机 ${目的ip地址} 开放端口未知"
$结果 = "被控机 ${目的ip地址} 开放端口未知"
return $结果
}
}
}
}
}
}
function 写输出并发数组
{
param
(
$输出对象,
[String]$被控机os关键字2
)
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Host ("【{0}】的os类型是:【{1}】" -f $输出对象.被控机显示名,$被控机os关键字2 )
}
else
{
Write-Host ("【{0}】OSTYPE IS:【{1}】" -f $输出对象.被控机显示名,$被控机os关键字2 )
}
$输出对象.被控机os类型 = $被控机os关键字2
$输出并发数组2 = $using:输出并发数组
$输出并发数组2.add($输出对象)
}
#region main2
$输入并发数组2 = $using:输入并发数组
if ($输入并发数组2.Count -gt 0)
{
for ($private:iii = 1;$private:iii -lt 21;$private:iii++)
{
$private: = $null
if ($输入并发数组2.TryTake([ref]$private:))
{
break
}
Start-Sleep -Milliseconds 67
}
[System.Net.IPAddress]$ip2 = 0
if ( [System.Net.IPAddress]::TryParse($private:.ip,[ref]$ip2) )
{
[string]$private:被控机的端口信息是 = fh返回被控机的_开放端口信息 -目的ip地址 $private:.ip -目的端口 $private:.端口
# if ($env:LANG -eq 'zh_CN.UTF-8')
# {
# Write-Host ("被控机【{0}】的端口关键字是:【{1}】" -f $private:值.被控机显示名,${private:被控机的端口信息是} )
# }
# else
# {
# Write-Host ("NODE:【{0}】PORT INFO IS:【{1}】" -f $private:值.被控机显示名,${private:被控机的端口信息是} )
# }
if ( $private:被控机的端口信息是.Contains('未知') -or $private:被控机的端口信息是.Contains('不通') )
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
$private:被控机os关键字 = '【被控机】开放端口未知!'
}
else
{
$private:被控机os关键字 = 'node port closed'
}
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
if ( $private:被控机的端口信息是.Contains('通了') )
{
$private:被控机os关键字 = '自定义端口,请联系开发者'
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
$private:linux版本字串 = 'centos8','centos7','centos6','debian8','debian9','debian10','ubuntu1404','ubuntu1604','ubuntu1804','centos8','alpine','ubuntu2004','Amazon Linux 2','kylin v10'
[scriptblock]$private:判断linux发行版 =
{
if (Select-String -Pattern 'centos-8' -Path '/etc/*-release' -SimpleMatch -Quiet)
{
return 'centos8'
}
if (Select-String -Pattern 'centos-7' -Path '/etc/*-release' -SimpleMatch -Quiet)
{
return 'centos7'
}
if (Select-String -Pattern 'centos' -Path '/etc/*-release' -SimpleMatch -Quiet)
{
return 'centos6'
}
if (Select-String -Pattern 'jessie' -Path '/etc/*-release' -SimpleMatch -Quiet)
{
return 'debian8'
}
if (Select-String -Pattern 'stretch' -Path '/etc/*-release' -SimpleMatch -Quiet)
{
return 'debian9'
}
if (Select-String -Pattern 'buster' -Path '/etc/*-release' -SimpleMatch -Quiet)
{
return 'debian10'
}
if (Select-String -Pattern 'Trusty Tahr' -Path '/etc/*-release' -SimpleMatch -Quiet)
{
return 'ubuntu1404'
}
if (Select-String -Pattern 'Xenial Xerus' -Path '/etc/*-release' -SimpleMatch -Quiet)
{
return 'ubuntu1604'
}
if (Select-String -Pattern 'Bionic Beaver' -Path '/etc/*-release' -SimpleMatch -Quiet)
{
return 'ubuntu1804'
}
if (Select-String -Pattern 'Focal Fossa' -Path '/etc/*-release' -SimpleMatch -Quiet)
{
return 'ubuntu2004'
}
if (Select-String -Pattern 'Alpine' -Path '/etc/*-release' -SimpleMatch -Quiet)
{
return 'Alpine'
}
if (Select-String -Pattern 'Karoo' -Path '/etc/*-release' -SimpleMatch -Quiet)
{
return 'Amazon Linux 2'
}
if (Select-String -Pattern 'Tercel' -Path '/etc/*-release' -SimpleMatch -Quiet)
{
return 'kylin v10'
}
}
$private:win版本字串 = 'win2008r2','win7','win8','win10','win2012r2','win2016','win2019'
[scriptblock]$private:判断win版本 =
{
if ( (Get-WmiObject -Class Win32_OperatingSystem).Caption -like "*2008r2*" )
{
return 'win2008r2'
}
if ( (Get-WmiObject -Class Win32_OperatingSystem).Caption -like "*7*" )
{
return 'win7'
}
if ( (Get-WmiObject -Class Win32_OperatingSystem).Caption -like "*8*" )
{
return 'win8'
}
if ( (Get-WmiObject -Class Win32_OperatingSystem).Caption -like "*10*" )
{
return 'win10'
}
if ( (Get-WmiObject -Class Win32_OperatingSystem).Caption -like "*2012 r2*" )
{
return 'win2012r2'
}
if ( (Get-WmiObject -Class Win32_OperatingSystem).Caption -like "*2016*" )
{
return 'win2016'
}
if ( (Get-WmiObject -Class Win32_OperatingSystem).Caption -like "*2019*" )
{
return 'win2019'
}
}
if ($IsLinux -eq $True)
{
if ( ($private:被控机的端口信息是 -eq 'win5985') -or ($private:被控机的端口信息是 -eq 'win5986') )
{
$private:被控机os关键字 = "从linux【主控机】中,经winrm协议,控制win【被控机】,无法实现!"
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
if ($private:被控机的端口信息是 -eq 'win135')
{
$private:被控机os关键字 = "从linux【主控机】中,经wmic协议+135端口,控制win【被控机】,无法实现!"
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
if ($private:被控机的端口信息是 -eq 'linux')
{
$l2l = & 'run_linux2linux_key_pwd.ps1' -目的ip地址 $private:.ip -powershell代码块 $private:判断linux发行版
if ($l2l -in $private:linux版本字串)
{
$private:被控机os关键字 = $l2l
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
elseif ($l2l[-1] -in $private:linux版本字串)
{
$private:被控机os关键字 = $l2l[-1]
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
else
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
$private:被控机os关键字 = '【被控机】linux发行版未知'
}
else
{
$private:被控机os关键字 = 'node linux version unknown'
}
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
}
}
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
if ($private:被控机的端口信息是 -eq 'win5985')
{
$w2w = & 'run_win2win5985_pwd.ps1' -目的ip地址 $private:.ip -powershell代码块 $private:判断win版本
if ($w2w -in $private:win版本字串)
{
$private:被控机os关键字 = $w2w
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
elseif ($w2w[-1] -in $private:win版本字串)
{
$private:被控机os关键字 = $w2w[-1]
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
else
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
$private:被控机os关键字 = '【被控机】win版本未知'
}
else
{
$private:被控机os关键字 = 'node win version unknown'
}
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
}
if ($private:被控机的端口信息是 -eq 'win5986')
{
$w2w = & 'run_win2win5986_pwd.ps1' -目的ip地址 $private:.ip -端口 5986 -powershell代码块 $private:判断win版本
if ($w2w -in $private:win版本字串)
{
$private:被控机os关键字 = $w2w
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
elseif ($w2w[-1] -in $private:win版本字串)
{
$private:被控机os关键字 = $w2w[-1]
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
else
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
$private:被控机os关键字 = '【被控机】win版本未知'
}
else
{
$private:被控机os关键字 = 'node win version unknown'
}
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
}
if ($private:被控机的端口信息是 -eq 'win135')
{
$private:被控机os关键字 = "从win【主控机】中,经wmic协议+135端口,控制win【被控机】,无法实现!"
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
if ($private:被控机的端口信息是 -eq 'linux')
{
$w2l = & 'run_win2linux_key_pwd.ps1' -目的ip地址 $private:.ip -powershell代码块 $private:判断linux发行版
if ($w2l -in $private:linux版本字串)
{
$private:被控机os关键字 = $w2l
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
elseif ($w2l[-1] -in $private:linux版本字串)
{
$private:被控机os关键字 = $w2l[-1]
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
else
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
$private:被控机os关键字 = '【被控机】开放端口未知!'
}
else
{
$private:被控机os关键字 = 'node linux version unknown'
}
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
}
}
}
else
{
Write-Error "nodelist文件中找到这个ip地址: $($当前被控机.ip),但ip不合法"
$private:被控机os关键字 = '非法ip地址'
写输出并发数组 -输出对象 $private: -被控机os关键字2 $private:被控机os关键字
}
}
#endregion main2
}
#写输出
$private:被控机的_os关键字_已经处理完成数 = 0
while ($global:所有被控机.count -gt $private:被控机的_os关键字_已经处理完成数)
{
Start-Sleep -Seconds 1
$秒++
if ($秒 -gt 10)
{
Write-Error '错误:刷新被控机os类型,等待时间太长。刷新失败。错误码21'
$private:被控机的_os关键字_已经处理完成数
exit 21
}
while ($输出并发数组.Count -gt 0)
{
$private:2 = $null
if ($输出并发数组.TryTake([ref]$private:2))
{
$private:当前被控机 = $global:所有被控机 | Where-Object { $_.ip -eq $private:2.ip }
Add-Member -InputObject ${private:当前被控机} -MemberType NoteProperty -Name '被控机os类型' -Value $private:2.被控机os类型 -Force
$private:被控机的_os关键字_已经处理完成数++
}
}
Write-Verbose "写耗时【$秒】秒,处理完了【$private:被控机的_os关键字_已经处理完成数】"
}
$结束 = Get-Date
$总耗时 = ($结束 - $开始).Seconds
Write-Verbose "总耗时【$总耗时】秒"
exit 0
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PowerShell
1
https://gitee.com/forkbomb/kasini3000.git
[email protected]:forkbomb/kasini3000.git
forkbomb
kasini3000
project_13384759
master

搜索帮助