upload successful
之前有帮一个用户写过一个操作系统部署部署时下载的脚本,客户当时的需求是根据不同的客户端网段,找指定的服务器下载对应的镜像文件和软件包,当时用户给的客户端网段都是 C段 ,由于 WinPEpowershell 有部分命令不支持,为了偷懒测试,我就直接用批处理写了个脚本,写得比较粗糙,还请大佬别笑话

脚本主要是判断客户端 IP地址 的前 3 段,再根据客户端的网络段找指定的服务器下载

l
@echo off
setlocal enabledelayedexpansion
set sys=win10
set s1=192.168.37.12
set s2=192.168.37.13
set s3=192.168.37.14
set s4=192.168.37.15
set s5=192.168.37.16
set s6=192.168.37.17
set s7=192.168.37.18
set s8=192.168.34.253
REM 定义设备型号
for /f "skip=1 tokens=*" %%a in ('wmic computersystem get model') do (
    set "model=%%a"
    goto :afterloop
)
:afterloop
set "model=%model: =%"
REM 定义网段列表
set "group1=192.168.134 192.168.142 192.168.150 192.168.158 192.168.166 192.168.174 192.168.182 192.168.190 192.168.128 192.168.136 192.168.144 192.168.152 192.168.161 192.168.169 192.168.177 192.168.196 192.168.204 192.168.212 192.168.220"
set "group2=192.168.135 192.168.143 192.168.151 192.168.159 192.168.167 192.168.175 192.168.183 192.168.191 192.168.129 192.168.137 192.168.145 192.168.153 192.168.162 192.168.170 192.168.178 192.168.197 192.168.205 192.168.213 192.168.221"
set "group3=192.168.136 192.168.144 192.168.152 192.168.160 192.168.168 192.168.176 192.168.184 192.168.130 192.168.138 192.168.146 192.168.155 192.168.163 192.168.171 192.168.179 192.168.198 192.168.206 192.168.214 192.168.222"
set "group4=192.168.137 192.168.145 192.168.153 192.168.161 192.168.169 192.168.177 192.168.185 192.168.131 192.168.139 192.168.147 192.168.156 192.168.164 192.168.172 192.168.180 192.168.199 192.168.207 192.168.215 192.168.223"
set "group5=192.168.138 192.168.146 192.168.154 192.168.162 192.168.170 192.168.178 192.168.186 192.168.132 192.168.140 192.168.148 192.168.157 192.168.165 192.168.173 192.168.181 192.168.200 192.168.208 192.168.216"
set "group6=192.168.139 192.168.147 192.168.155 192.168.163 192.168.171 192.168.179 192.168.187 192.168.133 192.168.133 192.168.141 192.168.149 192.168.158 192.168.166 192.168.174 192.168.182 192.168.201 192.168.209 192.168.217"
set "group7=192.168.140 192.168.148 192.168.156 192.168.164 192.168.172 192.168.180 192.168.188 192.168.134 192.168.142 192.168.150 192.168.159 192.168.167 192.168.175 192.168.183 192.168.202 192.168.210 192.168.218"
set "group8=192.168.141 192.168.149 192.168.157 192.168.165 192.168.173 192.168.181 192.168.189 192.168.132 192.168.135 192.168.143 192.168.151 192.168.160 192.168.168 192.168.176 192.168.203 192.168.211 192.168.219"
REM 获取当前IP地址
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| findstr /c:"IPv4"') do (
    set "ip=%%a"
    set "ip=!ip:~1!"
    set "ip=!ip:%% %%=!"
    goto :CheckIP
)
:CheckIP
REM 拆分IP地址
for /f "tokens=1-4 delims=." %%a in ("%ip%") do (
    set octet1=%%a
    set octet2=%%b
    set octet3=%%c
    set octet4=%%d
)
REM 根据IP地址匹配网段和服务器
set server=
for %%g in (group1 group2 group3 group4 group5 group6 group7 group8 group9) do (
    for %%h in (!%%g!) do (
        if "%octet1%.%octet2%.%octet3%"=="%%h" (
            set "group_num=%%g"
            set "group_num=!group_num:group=!"
            call set "server=%%s!group_num!%%"
            goto :DownloadFile
        )
    )
)
x:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -c "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show('Unable to find a file server matching your current network. Please contact the IT administrator and try again!')"
shutdown -s -t 05
:DownloadFile
echo Downloading from server !server!...
wget http://!server!/share/Script/UserAuth.ps1 -O x:\UserAuth.ps1
wget http://!server!/share/Script/InstallINF.ps1 -O x:\InstallINF.ps1
wget http://!server!/share/Script/ComputerName.ps1 -O x:\ComputerName.ps1
wget http://!server!/share/Script/PupopWindow.ps1 -O x:\PupopWindow.ps1
wget "http://!server!/share/drivers/!sys!/!model!.zip" -O c:\driver.zip
wget http://!server!/share/OS/!sys!/!sys!.wim -O c:\install.wim
wget http://!server!/share/Software\QAXInstall.exe -O c:\QAXInstall.exe
wget http://!server!/share/Script/DomainAuto.ps1 -O c:\DomainAuto.ps1
endlocal

前段时间用户说现在需求有变动,其它地区的客户端网段是 B段 ,如果按照之前的脚本,一个 B段 有几百个 C段 ,添加起来费人

听了用户的需求,我明白之前的批处理确实不太好处理这个问题,无奈只好重新研究用 powershell 来解决新的需求

我的想法很简单,由于 B段 都是连续的,我就想着将 IP地址 转换成 十进制 ,然后根据范围判断找指定的服务器下载,这应该比判断 IP段 要简单多了吧

l
$ipConfig = ipconfig | Where-Object {$_ -match 'IPv4' } | ForEach-Object { ($_ -split ":")[1].Trim() }
Write-Host "Current IP: $ipConfig"
function Convert-IPToDecimal {
    param ($ip)
    $octets = $ip -split '\.'
    return ([int]$octets[0] * 16777216) + ([int]$octets[1] * 65536) + ([int]$octets[2] * 256) + ([int]$octets[3])
    
}
$ipDecimal = Convert-IPToDecimal -ip $ipConfig
Write-Host "Current IP in decimal: $ipDecimal"
$ipRanges = @(
    @{ Start = 3221883393; End = 3221946366; Server = '192.10.0.1' },
    @{ Start = 3222067201; End = 3222130174; Server = '192.20.0.1' },
    @{ Start = 3232256001; End = 3232256254; Server = '192.168.80.1' }
)
$serverFound = $false
foreach ($range in $ipRanges) {
    if ($ipDecimal -ge $range.Start -and $ipDecimal -le $range.End) {
        $serverIP = $range.Server
        $serverFound = $true
        break
    }
}
if ($serverFound) {
    Write-Host "Matched server: $serverIP"
    wget "http://$serverIP/share/Script/UserAuth.ps1" -O "C:\UserAuth.ps1"
    wget "http://$serverIP/share/Script/InstallINF.ps1" -O "C:\InstallINF.ps1"
    wget "http://$serverIP/share/Script/ComputerName.ps1" -O "C:\ComputerName.ps1"
    wget "http://$serverIP/share/Script/PupopWindow.ps1" -O "C:\PupopWindow.ps1"
    wget "http://$serverIP/share/drivers/win10/model.zip" -O "C:\driver.zip"
    wget "http://$serverIP/share/OS/win10/install.wim" -O "C:\install.wim"
    wget "http://$serverIP/share/Software/QAXInstall.exe" -O "C:\QAXInstall.exe"
    wget "http://$serverIP/share/Script/DomainAuto.ps1" -O "C:\DomainAuto.ps1"
    exit 0
} else {
   
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.MessageBox]::Show('Unable to find a file server matching your current network. Please contact the IT administrator and try again!')
    exit 1
}

老实说,虽然这个脚本并没有什么技术含量,但是我觉得还是分享一下吧,我相信并不是所有人都知道 IP地址 是可以通过 十进制 来访问的,我也是以前打 CTF 的时候才接触到,应该算是比较冷门的知识吧