自动修改IP地址

           这一段时间来,公司的网管不知道和谁学习的歪招,下了一个网管软件,动不动就踢人让我等良民无法正常上网,不过这个网管也傻,竟然没有做IP地址绑定,只要我改一个IP一样可以上网下东东,上网转了一圈发现有前人以写过修IP的类,不过没有检查IP冲突的判断,小弟我基于这个类花10几分钟加上了IP冲突写了一个自动修改IP地址的工具,方便又快,哈哈哈。。又可以上网下了。
以下是主要源程序。应付网管的小程序,所以没有用什么软件工程,有兴趣的朋友可以自已对手改一改。

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic.MyServices;
namespace ChangIP
{
    
public partial class NetWork : Form
    
{
        
public string MYIP = "";
        
public NetWork()
        
{
            InitializeComponent();
        }


        
private void button1_Click(object sender, EventArgs e)
        
{
            modify.ChangeIP o 
= new modify.ChangeIP();
            Random myIPNUM 
= new Random();
            
int myNUM;
            
bool ifOK = true;
            
while (ifOK)
            
{
                Microsoft.VisualBasic.Devices.Computer MyComputer 
=
                
new Microsoft.VisualBasic.Devices.Computer();
                myNUM 
= myIPNUM.Next(2244);
                MYIP 
= "192.168.4." + myNUM.ToString();
                
if (MyComputer.Network.Ping(MYIP))
                
{
                    ifOK 
= true;
                }

                
else
                
{
                    ifOK 
= false;
                }


                
//MessageBox.Show(myNUM.ToString());
            }

            
string[] ipList = new string[] { MYIP};

            
string[] subnetList = new string[] "255.255.255.0" };

            o.ChangeTo(ipList, subnetList);
            MessageBox.Show(
"您的IP修改成功!,IP为:" + ipList[0].ToString());
        }

    }

}

 

using System;

using System.Management;

namespace modify
{

    
public class ChangeIP
    
{



        
/// <summary> 

        
/// Build of ArLi 2003.6.3 

        
/// </summary> 


        
public static readonly System.Version myVersion = new System.Version(11);



        
private ManagementBaseObject iObj = null;

        
private ManagementBaseObject oObj = null;

        
private ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");

        
private readonly ManagementObjectCollection moc;



        
/// <summary> 

        
/// example: 

        
/// <code> 

        
/// ArLi.CommonPrj.ChangeIP o = new ArLi.CommonPrj.ChangeIP(); 

        
/// string[] ipList = new string[]{"192.168.0.253","192.168.0.250"}; 

        
/// string[] subnetList = new string[]{"255.255.255.0","255.255.255.0"}; 

        
/// o.ChangeTo(ipList,subnetList); 

        
/// </code> 

        
/// </summary> 


        
public ChangeIP()
        
{

            moc 
= mc.GetInstances();

        }




        
/// <summary>cortrol</summary> 

        
/// <param name="ipAddr">IPAddr List</param> 

        
/// <param name="subnetMask">subnetMask List</param> 


        
public void ChangeTo(string[] ipAddr, string[] subnetMask)
        
{

            
foreach (ManagementObject mo in moc)
            
{

                
if (!(bool)mo["IPEnabled"]) continue;



                iObj 
= mo.GetMethodParameters("EnableStatic");

                iObj[
"IPAddress"= ipAddr;

                iObj[
"SubnetMask"= subnetMask;

                oObj 
= mo.InvokeMethod("EnableStatic", iObj, null);

            }


        }




        
/// <summary>cortrol</summary> 

        
/// <param name="ipAddr">IPAddr List</param> 

        
/// <param name="subnetMask">subnetMask List</param> 

        
/// <param name="gateways">gateway List</param> 

        
/// <param name="gatewayCostMetric">gateway CostMetric List, example: 1</param> 


        
public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric)
        
{

            
foreach (ManagementObject mo in moc)
            
{

                
if (!(bool)mo["IPEnabled"]) continue;



                iObj 
= mo.GetMethodParameters("EnableStatic");

                iObj[
"IPAddress"= ipAddr;

                iObj[
"SubnetMask"= subnetMask;

                oObj 
= mo.InvokeMethod("EnableStatic", iObj, null);



                iObj 
= mo.GetMethodParameters("SetGateways");

                iObj[
"DefaultIPGateway"= gateways;

                iObj[
"GatewayCostMetric"= gatewayCostMetric;

                oObj 
= mo.InvokeMethod("SetGateways", iObj, null);

            }


        }




        
/// <summary>cortrol</summary> 

        
/// <param name="ipAddr">IPAddr List</param> 

        
/// <param name="subnetMask">subnetMask List</param> 

        
/// <param name="gateways">gateway List</param> 

        
/// <param name="gatewayCostMetric">gateway CostMetric List, example: 1</param> 

        
/// <param name="dnsServer">DNSServer List</param> 


        
public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric, string[] dnsServer)
        
{

            
foreach (ManagementObject mo in moc)
            
{

                
if (!(bool)mo["IPEnabled"]) continue;



                iObj 
= mo.GetMethodParameters("EnableStatic");

                iObj[
"IPAddress"= ipAddr;

                iObj[
"SubnetMask"= subnetMask;

                oObj 
= mo.InvokeMethod("EnableStatic", iObj, null);



                iObj 
= mo.GetMethodParameters("SetGateways");

                iObj[
"DefaultIPGateway"= gateways;

                iObj[
"GatewayCostMetric"= gatewayCostMetric;

                oObj 
= mo.InvokeMethod("SetGateways", iObj, null);



                iObj 
= mo.GetMethodParameters("SetDNSServerSearchOrder");

                iObj[
"DNSServerSearchOrder"= dnsServer;

                oObj 
= mo.InvokeMethod("SetDNSServerSearchOrder", iObj, null);

            }


        }




        
/// <summary>DHCPEnabled</summary> 

        
public void EnableDHCP()
        
{

            
foreach (ManagementObject mo in moc)
            
{

                
if (!(bool)mo["IPEnabled"]) continue;



                
if (!(bool)mo["DHCPEnabled"])
                
{

                    iObj 
= mo.GetMethodParameters("EnableDHCP");

                    oObj 
= mo.InvokeMethod("EnableDHCP", iObj, null);

                }


            }


        }


    }


}


 

 

posted @   楚广明  阅读(4301)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示