You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将拆分IP文件的PowerShell逻辑转换为Jenkins Job可执行代码

PowerShell拆分IP文件逻辑移植到Jenkins流水线实现

原PowerShell逻辑功能

原有PowerShell函数实现的核心能力为:

  • 读取指定路径下存储IP地址的文本文件
  • 按每份最多40条IP的规则拆分内容
  • 生成以指定规则名加数字后缀命名的TXT文件,每个文件存储一份拆分后的IP数据

原函数代码如下:

function DivideAndCreateFiles ([string] $file, [string] $ruleName) {
    $Suffix = 0
    $FullData = Get-Content $file
    $MaxIP = 40
        
    while ($FullData.count -gt 0 ){
        $NewData = $FullData | Select-Object -First $MaxIP
        $FullData = $FullData | Select-Object -Skip $MaxIP
        $NewName = "$ruleName$Suffix"
        New-Variable -name $NewName -Value $NewData
        Get-Variable -name $NewName -ValueOnly  | out-file "$NewName.txt" 
        $Suffix++
    }
}

Jenkins Groovy 完整实现代码

基于你已有的初始代码,补全后的完整逻辑如下,可直接在Jenkins流水线中运行:

suffix = 0
maxIP = 40
ips = readFile('ips.txt')
ruleName = 'blabla' // 可修改为你需要的命名前缀

// 将读取的字符串按行拆分,同时过滤空行
def ipList = ips.readLines().findAll { it.trim() != '' }

while (ipList.size() > 0) {
    // 取当前批次最多40条IP
    def currentBatch = ipList.take(maxIP)
    // 移除已处理的IP
    ipList = ipList.drop(maxIP)
    // 生成文件名
    def outputFileName = "${ruleName}${suffix}.txt"
    // 写入文件到工作目录
    writeFile file: outputFileName, text: currentBatch.join('\n')
    suffix++
}

注意事项

  • 上述代码默认过滤了ips.txt中的空行,如果不需要过滤空行,将.findAll { it.trim() != '' }删除即可
  • 如果你使用的是Jenkins声明式流水线,需要将上述所有逻辑包裹在script块内才能正常执行
  • 生成的拆分文件会直接存放在当前Jenkins Job的工作目录下

内容的提问来源于stack exchange,提问作者Shahar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 01:45:02