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

请求协助编写Bash脚本:从IP列表生成指定网络配置命令文件

Generate Juniper Security Zone Commands from IP List

Got it, let's break down how to generate those two sets of Juniper security zone commands from your IP list file. Below are two simple, reliable methods using tools you probably already have access to.

Method 1: Use awk (Command-Line, No Extra Setup)

If you're on Linux/macOS (or have WSL on Windows), awk is perfect for this quick text transformation. It reads your IP file line by line and outputs the exact commands you need.

Step-by-Step:

  1. First, make sure your IP list is in a file (let's call it ips.txt) with one IP per line:
    1.1.1.1
    2.2.2.2
    3.3.3.3
    
  2. Run this awk command to generate both command types and save them to a final file:
    awk '{
        # Generate the single address entry command
        print "set security zones security-zone 1 address-book address H-" $0 " " $0 "/32"
        # Generate the address-set reference command
        print "set security zones security-zone 1 address-book address-set GROUP address H-" $0
    }' ips.txt > final_commands.txt
    

What this does:

  • $0 refers to the entire line (your IP address) in each iteration.
  • We directly substitute the IP into both command templates and write everything to final_commands.txt.
  • Empty lines or lines with whitespace are automatically ignored by awk, so no need to clean up your IP file first.

Method 2: Use Python (For More Customization)

If you need to add extra logic (like filtering certain IPs, modifying security zone names dynamically), a simple Python script gives you more flexibility.

Step-by-Step:

  1. Create a script file (e.g., generate_juniper_commands.py) with this code:
    # Open the IP list file and the output command file
    with open('ips.txt', 'r') as ip_file, open('final_commands.txt', 'w') as cmd_file:
        for line in ip_file:
            ip = line.strip()
            # Skip empty lines
            if not ip:
                continue
            # Write the first command type (single address entry)
            cmd1 = f"set security zones security-zone 1 address-book address H-{ip} {ip}/32"
            cmd_file.write(cmd1 + "\n")
            # Write the second command type (address-set reference)
            cmd2 = f"set security zones security-zone 1 address-book address-set GROUP address H-{ip}"
            cmd_file.write(cmd2 + "\n")
    
  2. Run the script with:
    python generate_juniper_commands.py
    

Customization Tips:

  • To change the security zone name (e.g., from security-zone 1 to security-zone DMZ), just replace that string in both command lines.
  • To use a different address-set name instead of GROUP, modify the address-set GROUP part in the second command.

Both methods will produce a final_commands.txt file with all your required commands, ready to be applied to your Juniper device.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 08:07:42