我想创建一个 powershell 脚本来执行 TCP 端口扫描程序,该扫描程序可以列出给定 IP 地址的开放 TCP 端口。\n这是我到目前为止所做的,这并不完美,我希望得到一些反馈和更正
\n\n port = (80)\n network = (192.168.0)\n ErrorActionPreference= \xe2\x80\x98silentlycontinue\xe2\x80\x99\n { $ip = \xe2\x80\x9c{0}.{1}\xe2\x80\x9d \xe2\x80\x93F $network,$add\n If(Test-Connection \xe2\x80\x93BufferSize 32 \xe2\x80\x93Count 1 \xe2\x80\x93quiet \xe2\x80\x93ComputerName $ip)\n { $socket = new-object System.Net.Sockets.TcpClient($ip, $port)\nRun Code Online (Sandbox Code Playgroud)\n\n问题是它没有扫描所有的 TCP 端口,我不知道如何让它做到这一点。
\n您可以利用一些模块来实现此用例。
\n\nFind-Module -Name \'*nmap*\' | \nFormat-Table -AutoSize\n\n<#\nVersion Name Repository Description \n------- ---- ---------- ----------- \n1.0.7 xNmap PSGallery Powershell DSC Configuration Script for installing Nmap versions 6.49 (Beta 4), 6.47, 6.46, 6.45, 6.40, and 6.25... \n0.6.0 PoshNmap PSGallery A wrapper for NMAP Network Discovery \n1.3.1 PSnmap PSGallery Svendsen Tech\'s PSnmap is an asynchronous Linux nmap look-alike for PowerShell. Ping sweeps and scans a network (accepts CIDR notation) for s...\n...\n#>\nRun Code Online (Sandbox Code Playgroud)\n\n为什么不针对此用例使用专门构建的 cmdlet?
\n\n# get function / cmdlet details\nGet-Command -Name Test-NetConnection -Syntax\n(Get-Command -Name Test-NetConnection).Parameters.Keys\nGet-help -Name Test-NetConnection -Full\nGet-help -Name Test-NetConnection -Online\nGet-help -Name Test-NetConnection -Examples\nRun Code Online (Sandbox Code Playgroud)\n\n注意事项:
\n\n如果这是您的用例,早期的 Windows PowerShell 版本没有 Test-NetConnection,但即便如此,为什么要从头开始执行此操作,而不是利用现有示例并根据需要进行调整?
\n\n好吧,除非这只是一个学习练习。即使如此,这并不意味着您不会首先查看其他示例。
\n\n\n \n\n\n
由搜索字符串提供的示例。
\n\n# Example 01\n<#\nCreating a Port Scanner with Windows PowerShell\nhttps://devblogs.microsoft.com/scripting/creating-a-port-scanner-with-windows-powershell\n#> \n# Creating a Port Scanner with Windows PowerShell\n$port = 80\n$net = \xe2\x80\x9c192.168.0\xe2\x80\x9d\n$range = 1..254\n\nforeach ($r in $range)\n{\n$ip = \xe2\x80\x9c{0}.{1}\xe2\x80\x9d -F $net,$r\n\nif(Test-Connection -BufferSize 32 -Count 1 -Quiet -ComputerName $ip)\n{\n $socket = new-object System.Net.Sockets.TcpClient($ip, $port)\n\n If($socket.Connected)\n {\n "$ip listening to port $port"\n $socket.Close() }\n }\n}\n\n\n# Example 02\n<#\nPort scan subnets with PSnmap for PowerShell\nhttps://www.powershelladmin.com/wiki/Port_scan_subnets_with_PSnmap_for_PowerShell\n#>\n# Port scan subnets with PSnmap for PowerShell\n#$computer, $port = $args[0,1] # assign values to these\n$mysock = new-object net.sockets.tcpclient\n$IAsyncResult = [IAsyncResult] $mysock.BeginConnect($computer, $port, $null, $null)\nmeasure-command { $succ = $iasyncresult.AsyncWaitHandle.WaitOne(3000, $true) } | % totalseconds\n$succ\n$mysock.Connected\n$mysock.Dispose()\n\n# Example 03:\n<# \nA Simple Network Port Scanner in PowerShell\nhttps://www.nextofwindows.com/a-simple-network-port-scanner-in-powershell\n#>\n# #requires -Version 1\nfunction Test-Port\n{\n Param([string]$ComputerName,$port = 5985,$timeout = 1000)\n\n try\n {\n $tcpclient = New-Object -TypeName system.Net.Sockets.TcpClient\n $iar = $tcpclient.BeginConnect($ComputerName,$port,$null,$null)\n $wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)\n if(!$wait)\n {\n $tcpclient.Close()\n return $false\n }\n else\n {\n # Close the connection and report the error if there is one\n\n $null = $tcpclient.EndConnect($iar)\n $tcpclient.Close()\n return $true\n }\n }\n catch \n {\n $false \n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
13679 次 |
| 最近记录: |