Advertisement
Guest User

Skift af IP v1.1

a guest
Jul 1st, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.21 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.     static CheckBox chkDhcp;
  21.     static Panel panel;
  22.  
  23.     static void Main()
  24.     {
  25.         string filename = "data.txt";
  26.  
  27.         string[] args = Environment.GetCommandLineArgs();
  28.         if (args.Length == 2) filename = args[1]; // filnavn medsendt fra bruger
  29.  
  30.         int x = 100, y = 5;
  31.         int cboWidth = 350;
  32.  
  33.         Form frm = new Form();
  34.         frm.StartPosition = FormStartPosition.CenterScreen;
  35.         frm.FormBorderStyle = FormBorderStyle.FixedDialog;
  36.         frm.MaximizeBox = false;
  37.         frm.Text = "Skift af IP";
  38.  
  39.         cboNet = new ComboBox();
  40.         cboNet.DropDownStyle = ComboBoxStyle.DropDownList;
  41.         cboNet.Left = x;
  42.         cboNet.Top = y;
  43.         cboNet.Width = cboWidth;
  44.  
  45.         panel = new Panel();
  46.         panel.Top = cboNet.Bottom + 2;
  47.  
  48.         txtOctet1 = new TextBox();
  49.         txtOctet1.Width = 50;
  50.         txtOctet1.Left = x;
  51.         txtOctet1.Top = 0;
  52.         txtOctet1.ReadOnly = true;
  53.         txtOctet1.TabStop = false;
  54.        
  55.         cbo1 = new ComboBox();
  56.         cbo2 = new ComboBox();
  57.         cbo3 = new ComboBox();
  58.         cbo1.SelectedIndexChanged += cbo_SelectedIndexChanged;
  59.         cbo2.SelectedIndexChanged += cbo_SelectedIndexChanged;
  60.         cbo3.SelectedIndexChanged += cbo_SelectedIndexChanged;
  61.  
  62.         cbo1.DropDownStyle = cbo2.DropDownStyle = cbo3.DropDownStyle = ComboBoxStyle.DropDownList;
  63.         cbo1.Left = cbo2.Left = cbo3.Left = x;
  64.         cbo1.Width = cbo2.Width = cbo3.Width = cboWidth;
  65.         cbo1.Top = txtOctet1.Bottom + 2;
  66.         cbo2.Top = cbo1.Bottom + 2;
  67.         cbo3.Top = cbo2.Bottom + 2;
  68.  
  69.         txtIp = new TextBox();
  70.         txtIp.Width = 120;
  71.         txtIp.Left = cbo1.Left;
  72.         txtIp.Top = cbo3.Bottom + 2;
  73.         txtIp.ReadOnly = true;
  74.         panel.ClientSize = new System.Drawing.Size(cbo1.Right + 5, txtIp.Bottom);
  75.  
  76.         Label lbl0 = new Label();
  77.         Label lbl1 = new Label();
  78.         Label lbl2 = new Label();
  79.         Label lbl3 = new Label();
  80.         Label lbl4 = new Label();
  81.         lbl0.Top = cboNet.Top;
  82.         lbl1.Top = cbo1.Top;
  83.         lbl2.Top = cbo2.Top;
  84.         lbl3.Top = cbo3.Top;
  85.         lbl4.Top = txtIp.Top;
  86.         lbl0.Text = "Netkort:";
  87.         lbl1.Text = "Landsdel:";
  88.         lbl2.Text = "SiteID:";
  89.         lbl3.Text = "TeknikerNR:";
  90.         lbl4.Text = "IP-adresse:";
  91.  
  92.         Button cmd = new Button();
  93.         cmd.Click += cmd_Click;
  94.         cmd.Text = "Skift IP";
  95.         cmd.Left = x;
  96.         cmd.Top = panel.Bottom + 2;
  97.         cmd.Width = cboWidth;
  98.         cmd.Height = 50;
  99.  
  100.         chkDhcp = new CheckBox();
  101.         chkDhcp.Click += chkDhcp_Click;
  102.         chkDhcp.Text = "Brug DHCP";
  103.         chkDhcp.Top = cmd.Top + (cmd.Height - chkDhcp.Height) / 2;
  104.  
  105.         frm.Controls.Add(cboNet);
  106.         frm.Controls.Add(lbl0);
  107.         panel.Controls.Add(txtOctet1);
  108.         panel.Controls.Add(cbo1);
  109.         panel.Controls.Add(cbo2);
  110.         panel.Controls.Add(cbo3);
  111.         panel.Controls.Add(lbl1);
  112.         panel.Controls.Add(lbl2);
  113.         panel.Controls.Add(lbl3);
  114.         panel.Controls.Add(lbl4);
  115.         panel.Controls.Add(txtIp);
  116.         frm.Controls.Add(cmd);
  117.  
  118.         frm.Controls.Add(chkDhcp);
  119.         frm.Controls.Add(panel);
  120.  
  121.         panel.Top = cboNet.Bottom + 2;
  122.         frm.ClientSize = new System.Drawing.Size(cbo1.Right + 5, cmd.Bottom + 5); // bredde,højde
  123.  
  124.         // net...
  125.         foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
  126.         {
  127.             if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
  128.             {
  129.                 cboNet.Items.Add(new NicInfo(nic));
  130.             }
  131.         }
  132.  
  133.         if (cboNet.Items.Count > 0) cboNet.SelectedIndex = 0; // vælg første netkort som standard
  134.  
  135.         // hent data fra fil...
  136.         try
  137.         {
  138.             sites = new List<Part>();
  139.             Regex re = new Regex(@"^(\d+)=(\d+)(?:.(?:(\d+).)?(.*))?");
  140.  
  141.             using (StreamReader sr = new StreamReader(filename))
  142.             {
  143.                 while (sr.Peek() >= 0)
  144.                 {
  145.                     Match m = re.Match(sr.ReadLine());
  146.                     if (m.Success)
  147.                     {
  148.                         switch (m.Groups[1].Value)
  149.                         {
  150.                             case "0": txtOctet1.Text = m.Groups[2].Value; break;
  151.                             case "1": cbo1.Items.Add(new Part(m.Groups[4].Value, m.Groups[2].Value, m.Groups[3].Value)); break;
  152.                             case "3": cbo3.Items.Add(new Part(m.Groups[4].Value, m.Groups[2].Value)); break;
  153.                             default:
  154.                                 // ting til den dynamiske liste
  155.                                 sites.Add(new Part(m.Groups[4].Value, m.Groups[2].Value, m.Groups[1].Value));
  156.                                 break;
  157.                         }
  158.                     }
  159.                 }
  160.             }
  161.         }
  162.         catch(IOException ex)
  163.         {
  164.             MessageBox.Show("FEJL!\n" + ex.Message);
  165.             return;
  166.         }
  167.  
  168.         if (frm.ShowDialog() == DialogResult.OK)
  169.         {
  170.  
  171.         }
  172.     }
  173.  
  174.     static void chkDhcp_Click(object sender, EventArgs e)
  175.     {
  176.         foreach (Control c in panel.Controls)
  177.         {
  178.             c.Enabled = !chkDhcp.Checked;
  179.         }
  180.     }
  181.  
  182.     static void cmd_Click(object sender, EventArgs e)
  183.     {
  184.         // skift ip
  185.  
  186.         if (cboNet.SelectedIndex < 0) return;
  187.        
  188.         IPAddress ip;
  189.         if (!chkDhcp.Checked && !IPAddress.TryParse(txtIp.Text, out ip))
  190.         {
  191.             MessageBox.Show("Fejl i ip-adresse");
  192.             return;
  193.         }
  194.  
  195.         var nicInfo = cboNet.SelectedItem as NicInfo;
  196.  
  197.         var pinfo = new System.Diagnostics.ProcessStartInfo();
  198.         pinfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  199.         pinfo.WorkingDirectory = Environment.SystemDirectory;
  200.         pinfo.FileName = "netsh.exe";
  201.  
  202.         if (chkDhcp.Checked)
  203.         {
  204.             pinfo.Arguments = string.Format("int ip set address \"{0}\" dhcp", nicInfo.Name);
  205.         }
  206.         else
  207.         {
  208.             var a = txtIp.Text.Split('.');
  209.             a[3] = "2"; // gateway slutter på 2
  210.             var gateway = string.Join(".", a);
  211.  
  212.             pinfo.Arguments = string.Format("int ip set address \"{0}\" static {1} {2} {3}", nicInfo.Name, txtIp.Text, nicInfo.Mask, gateway);
  213.         }
  214.  
  215.         System.Diagnostics.Process p = System.Diagnostics.Process.Start(pinfo);
  216.         p.WaitForExit();
  217.  
  218.         if (p.ExitCode == 0)
  219.         {
  220.             MessageBox.Show("Udført med success");
  221.         }
  222.         else
  223.         {
  224.             if (chkDhcp.Checked)
  225.                 MessageBox.Show("Ændring kunne ikke gennemføres.\r\nEr DHCP allerede aktiveret?");
  226.             else
  227.                 MessageBox.Show("Ændring kunne ikke gennemføres");
  228.         }
  229.  
  230.     }
  231.  
  232.     static void cbo_SelectedIndexChanged(object sender, EventArgs e)
  233.     {
  234.         if (sender == cbo1)
  235.         {
  236.             // liste 2 skal ændre sig efter liste 1
  237.             cbo2.Items.Clear();
  238.             var part = cbo1.SelectedItem as Part;
  239.  
  240.             foreach (var subpart in sites.Where(x => x.SubPart == part.SubPart))
  241.             {
  242.                 cbo2.Items.Add(subpart);
  243.             }
  244.         }
  245.  
  246.         if (cbo1.SelectedIndex < 0) return;
  247.         if (cbo2.SelectedIndex < 0) return;
  248.         if (cbo3.SelectedIndex < 0) return;
  249.  
  250.         string oct2 = (cbo1.Items[cbo1.SelectedIndex] as Part).Value;
  251.         string oct3 = (cbo2.Items[cbo2.SelectedIndex] as Part).Value;
  252.         string oct4 = (cbo3.Items[cbo3.SelectedIndex] as Part).Value;
  253.  
  254.         txtIp.Text = string.Format("{0}.{1}.{2}.{3}", txtOctet1.Text, oct2, oct3, oct4);
  255.     }
  256.  
  257.     class NicInfo
  258.     {
  259.         public string Name;
  260.         public string Mask;
  261.  
  262.         public NicInfo(NetworkInterface nic)
  263.         {
  264.             this.Name = nic.Name;
  265.  
  266.             var addresses = nic.GetIPProperties().UnicastAddresses;
  267.             foreach (var addr in addresses)
  268.             {
  269.                 if (addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) // ipv4
  270.                 {
  271.                     Mask = addr.IPv4Mask.ToString();
  272.                     break;
  273.                 }
  274.             }
  275.         }
  276.         public override string ToString()
  277.         {
  278.             return Name;
  279.         }
  280.     }
  281.  
  282.     class Part
  283.     {
  284.         public string Name { get; set; }
  285.         public string Value { get; set; }
  286.         public string SubPart { get; set; }
  287.  
  288.         public Part(string name, string value, string subPart = null)
  289.         {
  290.             this.Name = name;
  291.             this.Value = value;
  292.             this.SubPart = subPart;
  293.         }
  294.  
  295.         public override string ToString()
  296.         {
  297.             return Name;
  298.         }
  299.     }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement