Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

PowerShell batch scan IP and Port

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Previous articles have published methods and scripts for batch port scanning of a specified IP, methods for sending and receiving TCP and UDP message packets over PowerShell, and methods for trying to log in to SQLServer services through PowerShell, which constitute the PSNet assembly for manipulating network state through PowerShell. Recently, after continuous attempts, we have found a way to scan the specified range of IP segments and ports. This article will introduce how to scan IP and its corresponding ports in batches through PowerShell.

Still extending the PSNet assembly, first create the script file Invoke-ScanIPPort.ps1 under $env:PSSpace/PSNet/TCPOp and add a call to the script file in $env:PSSpace/PSNet/TCPOp/PSNet.psm1:

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

First, introduce the variables that will appear in the later code:

-StartAddress [starting IP address of scan], used in conjunction with-EndAddress, [this parameter must]

-EndAddress [IP address of the end of the scan], [this parameter must]

-ResolveHost [whether to attempt to resolve the hostname attempt]

-ScanPort [whether to scan the port], if you want to scan the port, this option must be

-AllPort [whether to scan all ports], with a range of 1mm 65534 (note that this option takes a long time to scan and is recommended to be used when a single IP is selected, and use it as little as possible)

-StartPort [start port for scanning], used with-EndPort. If this option exists with the-Ports option, the-Port parameter is invalid.

-EndPort [end port of scan]

-the port scanned by default when Ports is scanned. If there are no parameters in the subsequent scan, it will only scan 21WEI 22WEI 23WEI 53WEI 69WE 71LJ 80Med 98MIT 110MIT 111Med 443P 445pr 103L 143Ji 20012049.

3001pr 3128pc5222pr 6667pct 6868pr 777pr 787je 8080pl 1521g 3306pr 3389pr 5505505901 if followed by multiple digits separated by commas, the corresponding port will be scanned. If only the default port is scanned, this parameter is not required.

-TimeOut timeout. Default is 100ms (milliseconds)

This function is called as follows:

Invoke-ScanIPPort-StartAddress 192.168.10.1-EndAddress 192.168.10.25 scan IP segment

Invoke-ScanIPPort-StartAddress 192.168.10.1-EndAddress 192.168.10.254-ResolveHost# scans the IP segment and attempts to resolve the corresponding hostname of IP

Invoke-ScanIPPort-StartAddress 192.168.10.1-EndAddress 192.168.10.254-ResolveHost-ScanPort# scan the IP segment and attempt to scan the default port

Invoke-ScanIPPort-StartAddress 192.168.10.1-EndAddress 192.168.10.254-ResolveHost-ScanPort-TimeOut 50 # scan the IP segment, try to scan the default port, port scan 50ms timed out

Invoke-ScanIPPort-StartAddress 192.168.10.1-EndAddress 192.168.10.254-ResolveHost-ScanPort-Port 80 # scan the IP segment and try to scan port 80

Invoke-ScanIPPort-StartAddress 192.168.10.1-EndAddress 192.168.10.1-ResolveHost-ScanPort-AllPort# scan ip and attempt to scan all ports between 1x65534

Invoke-ScanIPPort-StartAddress 192.168.10.1-EndAddress 192.168.10.254-ScanPort-StarPort 21-EndPort 8scan all ports between IP segments of the host between 21 and 81

The picture above shows a picture in the process of scanning.

The result after the scan:

The code is as follows:

= filename: Invoke-ScanIPPort.ps1=function Invoke-ScanIPPort {Param ([parameter (Mandatory = $true, Position = 0)] [ValidatePattern ("\ b\ d {1 Mandatory 3}\.\ d {1 true 3}\.\ d {1 camera 3}\.\ d {1pm 3}\ b")] [string] $StartAddress, [parameter (Mandatory = $true) Position = 1)] [ValidatePattern ("\ b\ d {1pyrr3}\.\ d {1pce3}\.\ d {1pyr3}\.\ d {1pyr3}\ b")] [string] $EndAddress, [switch] $ResolveHost, [switch] $ScanPort, [switch] $AllPort, [int] $StartPort, [int] $EndPort, [int []] $Ports = @ (21pr 22pr 233J 53J 7809J 139J 111rect 389j 44345J 1080143J 20049je 209ju 3128J 5222666L 668pr 777787pjpg, 777787788Finals, 3306338988015211e 555905901) [int] $TimeOut = 100) Begin {$ping = New-Object System.Net.Networkinformation.Ping} Process {foreach ($an 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 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 {}}

Author: Fu Haijun

Source: http://fuhj02.blog.51cto.com

Copyright: the copyright of this article belongs to the author and 51cto.

Reprint: welcome to reprint, in order to preserve the author's creative enthusiasm, please reprint as required, thank you

Requirements: this statement must be retained without the consent of the author; the original link must be given in the article and the integrity of the content must be guaranteed! Otherwise, legal liability must be investigated!

Personal website: http://txj.shell.tor.hu/

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report