脚本编写学习日记:powershell(3)

该脚本实现了简易的端口扫描功能,支持TCP和UDP协议切换,能先检测主机是否存活再进行端口扫描。用户可以指定IP地址、端口范围或特定端口,结果可写入文件。由于多线程实现存在问题,目前脚本采用单线程执行。
摘要由CSDN通过智能技术生成

练习编写简易端口扫描(增加支持tcp与udp协议切换,增加先探测主机是否存活再进行端口开放探测,可写入文件,可从文件中读取ip,可指定范围端口,可指定特定端口)

【本来想搞个多线程的,但是没搞出来,总是有问题】

param (
    [parameter(mandatory=$false)]
    [alias('i')]
    $ip= "127.0.0.1",
    [parameter(mandatory=$false)]
    [alias('p')]
    $ports,
    [parameter(mandatory=$false)]
    [alias('f')]
    $filename,
    [parameter(mandatory=$false)]
    [alias('o')]
    $result,
    [parameter(mandatory=$false)]
    [alias('u')]
    [switch]$udp,
    [parameter(mandatory=$false)]
    [alias('t')]
    [switch]$tcp
)
if ($tcp -eq $true){
    Write-Host "正在使用tcp协议"
}elseif ($udp -eq $true){
    Write-Host "正在使用udp协议"
}else {
    Write-Host "正在使用tcp协议"
}

function Check-PortStatus($ip, $ports,$udp,$tcp, $result) {
    foreach ($port in $ports) {
            if ($tcp -eq $true -or ($tcp -eq $false -and $udp -eq $false)){
                $socket = New-Object System.Net.Sockets.TcpClient
                 try {
                    $socket.Connect($ip, $port)
                    Write-Host "$ip : $port is open" -ForegroundColor Green
                    Add-result $result $port $ip
                }
                catch {
                    Write-Host "$ip : $port is closed" -ForegroundColor Red
                }
                finally {
                    $socket.Dispose()
                }
             }else {
                  Check-UdpPortStatus $ip $port
             }
            }
}

function Check-UdpPortStatus($ip, $port) {
    $udpClient = New-Object System.Net.Sockets.UdpClient
    try {
        $udpClient.Connect($ip, $port)
        $udpClient.Send([byte[]](1..255), 255)
        $response = $udpClient.Receive([ref]$null)
        if($response -ne $null){
          Write-Host "$ip : $port is open" -ForegroundColor Green
        }else {
          Write-Host "$ip : $port is closed" -ForegroundColor Red
        }
    }
    catch {
        Write-Host "$ip : $port is closed" -ForegroundColor Red
    }
    finally {
        $udpClient.Close()
    }
}

function Add-result($result,$port,$ip,$tcp,$udp){
    if ($result.length -ge 0){
        if ($tcp -eq $true){
            Write-Output "tcp:${ip}:${port}" >> $result
        }elseif ($udp -eq $true){
            Write-Output "udp:${ip}:${port}" >> $result
        }else {
            Write-Output "tcp:${ip}:${port}" >> $result
        }
        }
}
if ($ports -eq $null){
    $ports = (21,22,80,443,445,3389)
}
if ($ports -match '-'){
    $range = $ports -split '-'
    $start = [int]$range[0]
    $end = [int]$range[1]
    $ports = $start..$end
}
try {
    $test = Test-Connection -ComputerName $ip -Count 1 -ErrorAction SilentlyContinue
    if ($test -ne $null){
        $test1 = $true
    }else {
        $test1 = $false
    }
}catch {
    Write-Host " $ip 连接超时"
}

if ($filename -eq $null){
    if ($test1 -eq $true){
        Check-PortStatus $ip $portS $udp $tcp $result
    }else {
        Write-Host "$ip 未存活"
    }
}else {
    $file = Get-Content $filename
    foreach ($ip in $file){
        try {
            $test = Test-Connection -ComputerName $ip -Count 1 -ErrorAction SilentlyContinue
            if ($test -ne $null){
                $test1 = $true
            }else {
                $test1 = $false
            }
        }catch {
            Write-Host " $ip 连接超时"
        }
        if ($test1 -eq $true){
            Check-PortStatus $ip $ports $udp $tcp $result
        }else {
            Write-Host "$ip 未存活"
        }
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值