主頁 > 知識(shí)庫 > PowerShell腳本開發(fā)之批量掃描IP和端口

PowerShell腳本開發(fā)之批量掃描IP和端口

熱門標(biāo)簽:如何根據(jù)經(jīng)緯度百度地圖標(biāo)注 印臺(tái)區(qū)呼叫中心外呼系統(tǒng) 新鄭電銷機(jī)器人一個(gè)月多少錢 電話機(jī)器人公司招聘 莫拉克電梯系統(tǒng)外呼怎么設(shè)置 騰訊地圖標(biāo)注中心怎么標(biāo)注 地圖標(biāo)注的圖案 萬全縣地圖標(biāo)注app 六寸地圖標(biāo)注點(diǎn)怎么刪除

前面的文章中曾經(jīng)發(fā)布了對(duì)指定IP進(jìn)行批量端口掃描的方法和腳本,過PowerShell收發(fā)TCP和UDP消息包的方法以及通過PowerShell嘗試登錄SQLServer服務(wù)的方法,這構(gòu)成了PSNet程序集用于通過PowerShell對(duì)網(wǎng)絡(luò)狀態(tài)進(jìn)行操作。最近在不斷嘗試之下,找到了對(duì)指定范圍的IP段進(jìn)行掃描和對(duì)端口進(jìn)行掃描的方法,本文將會(huì)介紹如何通過PowerShell批量掃描IP及其對(duì)應(yīng)的端口。

依然在PSNet程序集的基礎(chǔ)上進(jìn)行擴(kuò)展,首先在$env:PSSpace/PSNet/TCPOp下創(chuàng)建腳本文件Invoke-ScanIPPort.ps1,并在$env:PSSpace/PSNet/TCPOp/PSNet.psm1中添加對(duì)腳本文件的調(diào)用:

復(fù)制代碼 代碼如下:

. $env:PSSpace/PSNet/TCPOp/Invoke-ScanIPPort.ps1

首先對(duì)后面代碼中將會(huì)出現(xiàn)的變量進(jìn)行介紹:

復(fù)制代碼 代碼如下:

-StartAddress[掃描的起始IP地址],與-EndAddress配合使用,【此參數(shù)必須】
-EndAddress[掃描的結(jié)束IP地址],【此參數(shù)必須】
-ResolveHost[是否嘗試對(duì)主機(jī)名嘗試進(jìn)行解析]
-ScanPort[是否進(jìn)行端口掃描],如果要掃描端口此選項(xiàng)必須
-AllPort[是否對(duì)所有端口進(jìn)行掃描],范圍為1~65534(注意此選項(xiàng)掃描時(shí)間很長建議在選中單個(gè)IP的情況下進(jìn)行使用,并且盡量少使用)
-StartPort[掃描的起始端口端口],與-EndPort配合使用,如果此選項(xiàng)與-Ports選項(xiàng)同時(shí)存在則-Port參數(shù)失效
-EndPort[掃描的結(jié)束端口]
-Ports掃描時(shí)默認(rèn)掃描的端口,如果后續(xù)不帶參數(shù)則僅掃描21,22,23,53,69,71,80,98,110,139,111,389,443,445,1080,1433,2001,2049,
3001,3128,5222,6667,6868,7777,7878,8080,1521,3306,3389,5801,5900,5555,5901如果后續(xù)帶多個(gè)以逗號(hào)分割的多個(gè)數(shù)字則會(huì)掃描數(shù)字對(duì)應(yīng)的端口,如果只掃描默認(rèn)的端口,則不需此參數(shù)
-TimeOut超時(shí)時(shí)間,默認(rèn)值為100ms(毫秒)

此函數(shù)的調(diào)用方式如下:

復(fù)制代碼 代碼如下:

Invoke-ScanIPPort -StartAddress 192.168.10.1 -EndAddress 192.168.10.254#掃描IP段
Invoke-ScanIPPort -StartAddress 192.168.10.1 -EndAddress 192.168.10.254 –ResolveHost#掃描IP段,并嘗試解析IP對(duì)應(yīng)主機(jī)名
Invoke-ScanIPPort -StartAddress 192.168.10.1 -EndAddress 192.168.10.254 -ResolveHost –ScanPort#掃描IP段,并嘗試掃描默認(rèn)端口
Invoke-ScanIPPort -StartAddress 192.168.10.1 -EndAddress 192.168.10.254 -ResolveHost -ScanPort -TimeOut 50 #掃描IP段,嘗試掃描默認(rèn)端口,端口掃描50ms超時(shí)
Invoke-ScanIPPort -StartAddress 192.168.10.1 -EndAddress 192.168.10.254 -ResolveHost -ScanPort -Port 80 #掃描IP段,并嘗試掃描80端口
Invoke-ScanIPPort -StartAddress 192.168.10.1 -EndAddress 192.168.10.1 -ResolveHost -ScanPort –AllPort#掃描ip,并嘗試掃描所有1~65534之間端口
Invoke-ScanIPPort -StartAddress 192.168.10.1 -EndAddress 192.168.10.254 -ScanPort -StarPort 21 -EndPort 81#掃描IP段之間主機(jī)所有21至81之間的端口

上圖來一張掃描過程中的圖片

掃描結(jié)束后的結(jié)果:

代碼如下:

復(fù)制代碼 代碼如下:

 =====文件名:Invoke-ScanIPPort.ps1=====
function Invoke-ScanIPPort {
  Param(
    [parameter(Mandatory = $true,
      Position = 0)]
    [ValidatePattern("\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")]
    [string]$StartAddress,
    [parameter(Mandatory = $true,
      Position = 1)]
    [ValidatePattern("\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")]
    [string]$EndAddress,
    [switch]$ResolveHost,
    [switch]$ScanPort,
    [switch]$AllPort,
    [int]$StartPort,
    [int]$EndPort,
    [int[]]$Ports = @(21,22,23,53,69,71,80,98,110,139,111,389,443,445,1080,1433,2001,`
2049,3001,3128,5222,6667,6868,7777,7878,8080,1521,3306,3389,5801,5900,5555,5901),
    [int]$TimeOut = 100
  )
  Begin {
    $ping = New-Object System.Net.Networkinformation.Ping
  }
  Process {
    foreach($a in ($StartAddress.Split(".")[0]..$EndAddress.Split(".")[0])) {
      foreach($b in ($StartAddress.Split(".")[1]..$EndAddress.Split(".")[1])) {
        foreach($c in ($StartAddress.Split(".")[2]..$EndAddress.Split(".")[2])) {
          foreach($d in ($StartAddress.Split(".")[3]..$EndAddress.Split(".")[3])) {
            $ip = "$a.$b.$c.$d"
            write-progress -activity "ScanIP Ping" -status "$ip" -percentcomplete (($d/($EndAddress.Split(".")[3])) * 100)
            $pingStatus = $ping.Send("$ip",$TimeOut)
            if($pingStatus.Status -eq "Success") {
              if($ResolveHost) {
                write-progress -activity ResolveHost -status "$ip" -percentcomplete (($d/($EndAddress.Split(".")[3])) * 100) -Id 1
                $getHostEntry = [Net.DNS]::BeginGetHostEntry($pingStatus.Address, $null, $null)
              }
              if($ScanPort) {
                if($AllPort) {
                    $Ports = @(1..65534)
                }
                if($StartPort -ne $null -and $EndPort -ne $null){
                    $Ports = @($StartPort..$EndPort)
                }
                $openPorts = @()
                for($i = 1; $i -le $Ports.Count;$i++) {
                  $port = $Ports[($i-1)]
                  write-progress -activity "PortScan[$port]$result" -status "$ip" -percentcomplete (($i/($Ports.Count)) * 100) -Id 2
                  $client = New-Object System.Net.Sockets.TcpClient
                  $beginConnect = $client.BeginConnect($pingStatus.Address,$port,$null,$null)
                  if($client.Connected) {
                    $openPorts += $port
                  } else {
                    # Wait
                    Start-Sleep -Milli $TimeOut
                    if($client.Connected) {
                      $openPorts += $port
                      $length=$openPorts.length
                      $result="[find $length ports.Last port $port]"
                    }
                  }
                  $client.Close()
                }
              }
              if($ResolveHost) {
                $hostName = ([Net.DNS]::EndGetHostEntry([IAsyncResult]$getHostEntry)).HostName
              }
              # Return Object
              if ($openPorts -ne $null)
              {
              write-host "IPAddress" "$ip"
              if ($getHostEntry -ne $null)
              {write-host "HostName" $getHostEntry}
              write-host "Ports" $openPorts
              }
           }
          }
        }
      }
    }
  }
  End {
  }
}

您可能感興趣的文章:
  • PowerShell小技巧之發(fā)送TCP請(qǐng)求
  • PowerShell小技巧之嘗試ssh登錄
  • PowerShell腳本開發(fā)之收發(fā)TCP消息包
  • PowerShell腳本開發(fā)之收發(fā)UDP消息包
  • PowerShell腳本開發(fā)嘗試登錄SQL Server
  • PowerShell腳本開發(fā)之嘗試登錄ftp

標(biāo)簽:天水 襄陽 喀什 疫苗接種 南昌 湘潭 汕頭 臨汾

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PowerShell腳本開發(fā)之批量掃描IP和端口》,本文關(guān)鍵詞  PowerShell,腳本,開,發(fā)之,批量,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PowerShell腳本開發(fā)之批量掃描IP和端口》相關(guān)的同類信息!
  • 本頁收集關(guān)于PowerShell腳本開發(fā)之批量掃描IP和端口的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章