Advertisement
Guest User

Skift af IP

a guest
Jun 25th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using System.Windows.Forms;
  6. using System.IO;
  7. using System.Net;
  8. using System.Net.NetworkInformation;
  9. using System.Management;
  10.  
  11. static class ChangeIP
  12. {
  13.     static TextBox txtOctet1;
  14.     static ComboBox cboNet;
  15.     static ComboBox cbo1;
  16.     static ComboBox cbo2;
  17.     static ComboBox cbo3;
  18.     static TextBox txtIp;
  19.     static List<Part> sites;
  20.  
  21.     static void Main()
  22.     {
  23.         string filename = "data.txt";
  24.  
  25.         string[] args = Environment.GetCommandLineArgs();
  26.         if (args.Length == 2) filename = args[1]; // filnavn medsendt fra bruger
  27.  
  28.         int x = 100, y = 5;
  29.         int cboWidth = 350;
  30.  
  31.         Form frm = new Form();
  32.         frm.StartPosition = FormStartPosition.CenterScreen;
  33.         frm.FormBorderStyle = FormBorderStyle.FixedDialog;
  34.         frm.MaximizeBox = false;
  35.         frm.Text = "Skift af IP";
  36.  
  37.         cboNet = new ComboBox();
  38.         cboNet.DropDownStyle = ComboBoxStyle.DropDownList;
  39.         cboNet.Left = x;
  40.         cboNet.Top = y;
  41.         cboNet.Width = cboWidth;
  42.  
  43.         txtOctet1 = new TextBox();
  44.         txtOctet1.Width = 50;
  45.         txtOctet1.Left = x;
  46.         txtOctet1.Top = cboNet.Bottom + 2;
  47.         txtOctet1.ReadOnly = true;
  48.         txtOctet1.TabStop = false;
  49.        
  50.         cbo1 = new ComboBox();
  51.         cbo2 = new ComboBox();
  52.         cbo3 = new ComboBox();
  53.         cbo1.Tag = "cbo1";
  54.         cbo1.SelectedIndexChanged += cbo_SelectedIndexChanged;
  55.         cbo2.SelectedIndexChanged += cbo_SelectedIndexChanged;
  56.         cbo3.SelectedIndexChanged += cbo_SelectedIndexChanged;
  57.  
  58.         cbo1.DropDownStyle = cbo2.DropDownStyle = cbo3.DropDownStyle = ComboBoxStyle.DropDownList;
  59.         cbo1.Left = cbo2.Left = cbo3.Left = x;
  60.         cbo1.Width = cbo2.Width = cbo3.Width = cboWidth;
  61.         cbo1.Top = txtOctet1.Bottom + 2;
  62.         cbo2.Top = cbo1.Bottom + 2;
  63.         cbo3.Top = cbo2.Bottom + 2;
  64.  
  65.         txtIp = new TextBox();
  66.         txtIp.Width = 120;
  67.         txtIp.Left = cbo1.Left;
  68.         txtIp.Top = cbo3.Bottom + 2;
  69.         txtIp.ReadOnly = true;
  70.  
  71.         Label lbl0 = new Label();
  72.         Label lbl1 = new Label();
  73.         Label lbl2 = new Label();
  74.         Label lbl3 = new Label();
  75.         Label lbl4 = new Label();
  76.         lbl0.Top = cboNet.Top;
  77.         lbl1.Top = cbo1.Top;
  78.         lbl2.Top = cbo2.Top;
  79.         lbl3.Top = cbo3.Top;
  80.         lbl4.Top = txtIp.Top;
  81.         lbl0.Text = "Netkort:";
  82.         lbl1.Text = "Landsdel:";
  83.         lbl2.Text = "SiteID:";
  84.         lbl3.Text = "TeknikerNR:";
  85.         lbl4.Text = "IP-adresse:";
  86.  
  87.         Button cmd = new Button();
  88.         cmd.Click += cmd_Click;
  89.         cmd.Text = "Skift IP";
  90.         cmd.Left = x;
  91.         cmd.Top = txtIp.Bottom + 12;
  92.         cmd.Width = cboWidth;
  93.         cmd.Height = 50;
  94.  
  95.         frm.Controls.Add(cboNet);
  96.         frm.Controls.Add(txtOctet1);
  97.         frm.Controls.Add(cbo1);
  98.         frm.Controls.Add(cbo2);
  99.         frm.Controls.Add(cbo3);
  100.         frm.Controls.Add(lbl0);
  101.         frm.Controls.Add(lbl1);
  102.         frm.Controls.Add(lbl2);
  103.         frm.Controls.Add(lbl3);
  104.         frm.Controls.Add(lbl4);
  105.         frm.Controls.Add(txtIp);
  106.         frm.Controls.Add(cmd);
  107.  
  108.         frm.ClientSize = new System.Drawing.Size(cbo1.Right + 5, cmd.Bottom + 5); // bredde,højde
  109.  
  110.         // net...
  111.         foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
  112.         {
  113.             if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
  114.             {
  115.                 cboNet.Items.Add(new NicInfo(nic));
  116.             }
  117.         }
  118.  
  119.         if (cboNet.Items.Count > 0) cboNet.SelectedIndex = 0; // vælg første netkort som standard
  120.  
  121.         // hent data fra fil...
  122.         try
  123.         {
  124.             sites = new List<Part>();
  125.             Regex re = new Regex(@"^(\d+)=(\d+)(?:.(?:(\d+).)?(.*))?");
  126.  
  127.             using (StreamReader sr = new StreamReader(filename))
  128.             {
  129.                 while (sr.Peek() >= 0)
  130.                 {
  131.                     Match m = re.Match(sr.ReadLine());
  132.                     if (m.Success)
  133.                     {
  134.                         switch (m.Groups[1].Value)
  135.                         {
  136.                             case "0": txtOctet1.Text = m.Groups[2].Value; break;
  137.                             case "1": cbo1.Items.Add(new Part(m.Groups[4].Value, m.Groups[2].Value, m.Groups[3].Value)); break;
  138.                             case "3": cbo3.Items.Add(new Part(m.Groups[4].Value, m.Groups[2].Value)); break;
  139.                             default:
  140.                                 // ting til den dynamiske liste
  141.                                 sites.Add(new Part(m.Groups[4].Value, m.Groups[2].Value, m.Groups[1].Value));
  142.                                 break;
  143.                         }
  144.                     }
  145.                 }
  146.             }
  147.         }
  148.         catch(IOException ex)
  149.         {
  150.             MessageBox.Show("FEJL!\n" + ex.Message);
  151.             return;
  152.         }
  153.  
  154.         if (frm.ShowDialog() == DialogResult.OK)
  155.         {
  156.  
  157.         }
  158.     }
  159.  
  160.     static void cmd_Click(object sender, EventArgs e)
  161.     {
  162.         // skift ip
  163.  
  164.         if (cboNet.SelectedIndex < 0) return;
  165.         IPAddress ip;
  166.         if (!IPAddress.TryParse(txtIp.Text, out ip))
  167.         {
  168.             MessageBox.Show("Fejl i ip-adresse");
  169.             return;
  170.         }
  171.  
  172.         try
  173.         {
  174.             var nicInfo = cboNet.SelectedItem as NicInfo;
  175.             NetworkInterface nic = nicInfo.Nic;
  176.             ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  177.             ManagementObjectCollection moc = mc.GetInstances();
  178.  
  179.             foreach (ManagementObject mo in moc)
  180.             {
  181.                 if (mo["SettingID"].ToString() != nic.Id) continue;
  182.  
  183.                 ManagementBaseObject newIp = mo.GetMethodParameters("EnableStatic");
  184.                 newIp["IPAddress"] = new string[] { txtIp.Text };
  185.                 newIp["SubnetMask"] = new string[] { nicInfo.Mask };
  186.  
  187.                 ManagementBaseObject setIp = mo.InvokeMethod("EnableStatic", newIp, null);
  188.                 var ret = (UInt32)setIp["ReturnValue"];
  189.                 switch (ret)
  190.                 {
  191.                     case 0: MessageBox.Show("Din ip-adresse er nu ændret"); break;
  192.                     case 1: MessageBox.Show("Ændring træder i kraft efter genstart"); break;
  193.                     case 64: MessageBox.Show("Dit styresystem er ikke understøttet"); break;
  194.                     case 66: MessageBox.Show("Invalid subnet-maske"); break;
  195.                     case 70: MessageBox.Show("Invalid ip"); break;
  196.                     case 91: MessageBox.Show("Adgang nægtet"); break;
  197.                     case 2147749891: MessageBox.Show("Mangler rettigheder til at ændre ip"); break;
  198.                     case 2147786788: MessageBox.Show("Lock-fejl. Se evt. KB950134"); break;
  199.                     default: MessageBox.Show("Der opstod en ukendt fejl: " + ret.ToString()); break;
  200.                 }
  201.                 break;
  202.             }
  203.         }catch(Exception ex)
  204.         {
  205.             MessageBox.Show("Fejl ved ændring af ip-adresse!\r\n" + ex.Message);
  206.         }
  207.     }
  208.  
  209.     static void cbo_SelectedIndexChanged(object sender, EventArgs e)
  210.     {
  211.         var cbo = sender as ComboBox;
  212.         if (cbo.Tag != null && (string)cbo.Tag == "cbo1")
  213.         {
  214.             // liste 2 skal ændre sig efter liste 1
  215.             cbo2.Items.Clear();
  216.             var part = cbo1.SelectedItem as Part;
  217.  
  218.             foreach (var subpart in sites.Where(x => x.SubPart == part.SubPart))
  219.             {
  220.                 cbo2.Items.Add(subpart);
  221.             }
  222.         }
  223.  
  224.         if (cbo1.SelectedIndex < 0) return;
  225.         if (cbo2.SelectedIndex < 0) return;
  226.         if (cbo3.SelectedIndex < 0) return;
  227.  
  228.         string oct2 = (cbo1.Items[cbo1.SelectedIndex] as Part).Value;
  229.         string oct3 = (cbo2.Items[cbo2.SelectedIndex] as Part).Value;
  230.         string oct4 = (cbo3.Items[cbo3.SelectedIndex] as Part).Value;
  231.  
  232.         txtIp.Text = string.Format("{0}.{1}.{2}.{3}", txtOctet1.Text, oct2, oct3, oct4);
  233.     }
  234.  
  235.     class NicInfo
  236.     {
  237.         public NetworkInterface Nic { get; set; }
  238.         public string Name;
  239.         public string Mask;
  240.  
  241.         public NicInfo(NetworkInterface nic)
  242.         {
  243.             this.Nic = nic;
  244.             this.Name = nic.Name;
  245.  
  246.             var addresses = nic.GetIPProperties().UnicastAddresses;
  247.             foreach (var addr in addresses)
  248.             {
  249.                 if (addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) // ipv4
  250.                 {
  251.                     string ip = addr.Address.ToString();
  252.                     Mask = addr.IPv4Mask.ToString();
  253.                     Name += " (" + ip + ")"; // vis ip
  254.                     break;
  255.                 }
  256.             }
  257.         }
  258.         public override string ToString()
  259.         {
  260.             return Name;
  261.         }
  262.     }
  263.  
  264.     class Part
  265.     {
  266.         public string Name { get; set; }
  267.         public string Value { get; set; }
  268.         public string SubPart { get; set; }
  269.  
  270.         public Part(string name, string value, string subPart = null)
  271.         {
  272.             this.Name = name;
  273.             this.Value = value;
  274.             this.SubPart = subPart;
  275.         }
  276.  
  277.         public override string ToString()
  278.         {
  279.             return Name;
  280.         }
  281.     }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement