本文目录导读:

- Windows 系统 (使用 PowerShell 或 netsh)
- Linux 系统 (使用 ifconfig/ip 命令)
- 通过网络配置文件批量修改
- 使用 DHCP 批量切换(恢复自动获取)
- 高级批量管理(使用数据库/变量)
- 注意事项:
批量修改IP地址的方式取决于你的操作系统和网络环境,以下是几种常见场景的指令编写方法:
Windows 系统 (使用 PowerShell 或 netsh)
使用 netsh 命令(适合单个网卡)
netsh interface ip set address name="以太网" static 192.168.1.100 255.255.255.0 192.168.1.1
批量修改多个网卡(PowerShell脚本)
# 批量设置多个网卡为静态IP
$interfaces = @("以太网", "WLAN", "本地连接2")
$newIP = "192.168.1."
$subnet = "255.255.255.0"
$gateway = "192.168.1.1"
$i = 100
foreach ($iface in $interfaces) {
netsh interface ip set address name="$iface" static "$newIP$i" $subnet $gateway 1
$i++
}
使用 PowerShell 原生命令
# 修改指定网卡
Get-NetAdapter -Name "以太网*" | ForEach-Object {
New-NetIPAddress -InterfaceIndex $_.InterfaceIndex -IPAddress "192.168.1.$($_.Name[-1])" -PrefixLength 24 -DefaultGateway 192.168.1.1
}
Linux 系统 (使用 ifconfig/ip 命令)
临时修改(重启失效)
# 使用 ip 命令 sudo ip addr add 192.168.1.100/24 dev eth0 sudo ip route add default via 192.168.1.1 # 使用 ifconfig sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
批量修改多个接口(bash脚本)
#!/bin/bash
# 批量设置多个网卡
declare -A INTERFACES=(
["eth0"]="192.168.1.100"
["eth1"]="192.168.1.101"
["wlan0"]="192.168.1.102"
)
for iface in "${!INTERFACES[@]}"; do
sudo ip addr add ${INTERFACES[$iface]}/24 dev $iface
sudo ip link set $iface up
done
永久修改(需要修改配置文件)
# Debian/Ubuntu - 修改 /etc/network/interfaces sudo sed -i 's/address .*/address 192.168.1.100/' /etc/network/interfaces # CentOS/RHEL - 修改 /etc/sysconfig/network-scripts/ifcfg-eth0 sudo sed -i 's/IPADDR=.*/IPADDR=192.168.1.100/' /etc/sysconfig/network-scripts/ifcfg-eth0
通过网络配置文件批量修改
Windows - 使用 CSV 文件批量导入
# 准备一个 CSV 文件 (ipconfig.csv)格式:Interface,IP,Mask,Gateway
# "以太网","192.168.1.100","255.255.255.0","192.168.1.1"
# "WLAN","192.168.1.101","255.255.255.0","192.168.1.1"
Import-Csv ipconfig.csv | ForEach-Object {
netsh interface ip set address name=$_.Interface static $_.IP $_.Mask $_.Gateway 1
}
Linux - 从文件读取配置
#!/bin/bash
# iplist.txt 格式:interface ip mask gateway
# eth0 192.168.1.100 255.255.255.0 192.168.1.1
# eth1 192.168.1.101 255.255.255.0 192.168.1.1
while IFS=' ' read -r iface ip mask gateway; do
sudo ip addr add $ip/$mask dev $iface
sudo ip route add default via $gateway dev $iface
done < iplist.txt
使用 DHCP 批量切换(恢复自动获取)
:: Windows - 切换到 DHCP
netsh interface ip set address name="以太网" source=dhcp
netsh interface ip set dns name="以太网" source=dhcp
:: 批量切换所有网卡
for /f "tokens=1,2,3" %i in ('netsh interface show interface') do (
netsh interface ip set address name=%i source=dhcp
)
高级批量管理(使用数据库/变量)
# PowerShell 从 Excel 或数据库读取
$connection = New-Object System.Data.SqlClient.SqlConnection("YourConnectionString")
$connection.Open()
$command = $connection.CreateCommand()
$command.CommandText = "SELECT * FROM IP_Config WHERE Location='Office_A'"
$reader = $command.ExecuteReader()
while ($reader.Read()) {
$interface = $reader["InterfaceName"]
$ip = $reader["IPAddress"]
$mask = $reader["SubnetMask"]
$gateway = $reader["Gateway"]
netsh interface ip set address name="$interface" static $ip $mask $gateway 1
}
注意事项:
- 管理员权限:Windows 需要以管理员身份运行
- 备份配置:修改前建议备份原有配置
- 连接性:IP修改后可能断网,建议使用计划任务或远程管理工具
- 网卡名称:不同系统网卡名称可能不同(如"以太网"、"本地连接"等)
建议先在小范围测试,确认无误后再批量执行。
标签: 批量指令
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。