zoukankan      html  css  js  c++  java
  • 自动修改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);

                    }


                }


            }


        }


    }


     

  • 相关阅读:
    android studio解决微信登录,百度地图等调试问题
    Android studio初入的各种坑
    react native环境配置及各种坑
    单例模式序列化后反序列化单例失效的问题
    react native 初识react native
    Fragment 点击事件的穿透和重叠bug
    转载:android——eclipse如何去除Ctrl+shift+R组合键查找到的.class文件
    分布式队列Celery
    vue-生命周期
    vue-实现倒计时功能
  • 原文地址:https://www.cnblogs.com/chu888chu888/p/661930.html
Copyright © 2011-2022 走看看