吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1266|回复: 37
上一主题 下一主题
收起左侧

[原创工具] 一键批量修改多网卡IP地址

[复制链接]
跳转到指定楼层
楼主
秘制蛋炒饭 发表于 2025-3-22 10:19 回帖奖励
本帖最后由 秘制蛋炒饭 于 2025-3-22 10:32 编辑

因工作需要需要一键切换多网卡IP地址,网上找了半天没找到就自己使用C#语言生成指令后使用PowerShell进行设置,网卡名前缀是“port”,也可以改成其他名字的前缀,第一次发帖,各位大神不要见笑


                // 验证IP合法性
                if (ipParts.Length != 4 || !IPAddress.TryParse(inputIP, out _))
                {
                    MessageBox.Show("IP地址格式无效");
                    return;
                }
            }
            else
            {
                MessageBox.Show("输入IP不能为空");
                return;
            }


            // 根据输入IP的最后一段确定port取值,取最后两位
            string lastPart = ipParts[3];
            if (lastPart.Length > 2)
            {
                lastPart = lastPart.Substring(lastPart.Length - 2);
            }
            if (!int.TryParse(lastPart, out int portCount))
            {
                MessageBox.Show("末段值需为有效的整数");
                return;
            }
            // 验证末段值范围
            if (portCount < 1 || portCount > 155)
            {
                MessageBox.Show("末段值需在1~155之间");
                return;
            }




            // 拼接IP前缀
            string ipPrefix = $"{ipParts[0]}.{ipParts[1]}.{ipParts[2]}";
            int startIP = 101;
            StringBuilder commands = new StringBuilder();


            // 生成命令
            for (int i = 1; i <= portCount; i++)
            {
                int currentIP = startIP + i - 1;
                commands.AppendLine($"netsh interface ip set address \"port{i}\" static {ipPrefix}.{currentIP} 255.255.255.0");
            }


            textBox2.Text = commands.ToString();
        }


        private void ExecuteButton_Click(object sender, EventArgs e)
        {
            string commands = textBox2.Text.Trim();
            if (string.IsNullOrEmpty(commands))
            {
                MessageBox.Show("命令内容为空");
                return;
            }

            try
            {
                // 启动PowerShell进程并执行命令
                ProcessStartInfo psi = new ProcessStartInfo
                {
                    FileName = "powershell.exe",
                    Arguments = $"-NoProfile -ExecutionPolicy Bypass -Command \"{commands}\"",
                    UseShellExecute = true,
                    Verb = "runas" // 以管理员权限运行
                };
                Process.Start(psi);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"执行失败: {ex.Message}");
            }
        }

      

补充一下:如果需要设置40个网卡,比如需要改192.168.1.X段位的,只需填写192.168.1.140,因为IP是从101开始

1.png (476.63 KB, 下载次数: 0)

1.png

虚拟网卡IP一键设置.rar

36.69 KB, 下载次数: 50, 下载积分: 吾爱币 -2 CB

免费评分

参与人数 8吾爱币 +13 热心值 +8 收起 理由
jj238535947 + 1 + 1 谢谢@Thanks!
dj42898 + 1 + 1 热心回复!
ahabuxixi + 1 + 1 用心讨论,共获提升!
cai12qq + 1 + 1 感谢分享,很实用
Rannnn + 1 + 1 谢谢@Thanks!
yanglinman + 1 谢谢@Thanks!
grrr_zhao + 1 + 1 谢谢@Thanks!
风之暇想 + 7 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

推荐
小呆伟 发表于 2025-3-23 21:26
感谢分享                                       
沙发
otra 发表于 2025-3-22 10:58
3#
pzhuzzj 发表于 2025-3-22 12:05
4#
sdmaoyj 发表于 2025-3-22 12:08
简单明了
5#
hyx333 发表于 2025-3-22 12:12
很好 谢谢楼主分享 这个可以搞一搞的  我试试
6#
HXlyf 发表于 2025-3-22 12:36
没有成品吗?
7#
dd114514 发表于 2025-3-22 12:52
感谢分享
8#
finddream2025 发表于 2025-3-22 13:08
其实要多网卡有什么用?
9#
yujianlixi 发表于 2025-3-22 13:09
初学,感谢分享
10#
fxw520 发表于 2025-3-22 13:21
NetSetMan 记得这个软件也可以吧
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-3-25 06:01

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表