歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C#開發:局域網IP地址掃描小工具

C#開發:局域網IP地址掃描小工具

日期:2017/3/1 9:46:38   编辑:Linux編程

該C#開發工具實現功能:

1、可以掃描制定C網段的地址掃描,使用ping方式進行探測

2、統計對ICMP有響應的和無響應的主機數量




using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Net;

using System.Net.Sockets;

using System.Threading;

using System.Net.NetworkInformation;

using System.IO;

namespace PingIP

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

//關閉跨線程檢查,正式開放環境中不允許這麼做,只能通過回調機制來實現。

TextBox.CheckForIllegalCrossThreadCalls = false;

}

#region //開始按鈕操作

//定義個線程,用於執行具體的操作。

Thread thread = null;

private void BtnStart_Click(object sender, EventArgs e)

{

//清空輸出窗口內容

lbOutPut.Items.Clear();

//定義“未活動或不可達IP個數:”變量

txtip6.Text = null;

//定義“活動的IP個數:”變量

txtip7.Text = null;

//點擊開始按鈕後,禁用該按鈕,防止用戶重復點擊

BtnStart.Enabled = false;

//實例化線程

thread = new Thread(Scanip);

//設置為後台線程

thread.IsBackground = true;

//啟動線程

thread.Start();

}

#endregion

//定義ping的方法

void Scanip()

{

//定義IP地址輸入框

int ip1 = Convert.ToInt32(txt1.Text);

int ip2 = Convert.ToInt32(txt2.Text);

int ip3 = Convert.ToInt32(txt3.Text);

int ip4 = Convert.ToInt32(txt4.Text);

int ip5 = Convert.ToInt32(txt5.Text);

int ip6 = 0;

int ip7 = 0;

//實例化一個ping實例

Ping ping = new Ping();

//定義ping的選項,其中包含ttl值和是否分段

PingOptions poption = new PingOptions(128, true);

//定義接收數組

byte[] data = new byte[32];

//定義變量

int recived = 0;

//實例化一個響應時間變量

List<long> responseTimes = new List<long>();

//循環執行IP地址段的每個ip的ping操作

for (int j = ip4; j <= ip5; j++)

{

//實例化IP地址

IPAddress address = IPAddress.Parse(ip1 + "." + ip2 + "." + ip3 + "." + ip4);

//最後一個IP地址段自增

ip4++;

//每個ip執行一次ping操作

for (int i = 0; i < 1; i++)

{

//防止中斷ping操作。

try

{

//實例化一個reply

PingReply reply = ping.Send(address, 1000, data, poption);

string str = null;

if (reply != null)

{

//對reply狀態進行分類

switch (reply.Status)

{

case IPStatus.Success:

str = string.Format("Reply From{0}" + "Bytes={1} time={2} TTL={3}", reply.Address, reply.Buffer.Length, reply.RoundtripTime, reply.Options.Ttl);

lbOutPut.Items.Add(str);

lbOutPut.SelectedItem = lbOutPut.Items[lbOutPut.Items.Count - 1];

this.Update();

recived++;

responseTimes.Add(reply.RoundtripTime);

ip7++;

txtip7.Text = ip7.ToString();

break;

case IPStatus.TimedOut:

str = address.ToString() + ":Request time Out";

lbOutPut.Items.Add(str);

lbOutPut.SelectedItem = lbOutPut.Items[lbOutPut.Items.Count - 1];

this.Update();

ip6++;

txtip6.Text = ip6.ToString();

break;

default:

str = string.Format("Ping Failed{0}", reply.Status.ToString());

lbOutPut.Items.Add(str);

lbOutPut.SelectedItem = lbOutPut.Items[lbOutPut.Items.Count - 1];

this.Update();

ip6++;

txtip6.Text = ip6.ToString();

break;

}

}

else

{

str = string.Format("Ping Failed for an unkown reason");

lbOutPut.Items.Add(str);

lbOutPut.SelectedItem = lbOutPut.Items[lbOutPut.Items.Count - 1];

this.Update();

ip6++;

txtip6.Text = ip6.ToString();

}

}

catch

{//啟用開始按鈕

BtnStart.Enabled = true;

break;

}

}

}

//啟用開始按鈕

BtnStart.Enabled = true;

}

#region //公共事件,只允許輸入數字

private void cubox_KeyPress(object sender, KeyPressEventArgs e)

{

TextBox cubox = sender as TextBox;

if (cubox.TextLength > 2)

{

e.Handled = true;

}

//if (Convert.ToInt32(cubox.Text) > 254)

//{

// e.Handled = true;

//}

if (e.KeyChar < '0' || e.KeyChar > '9')

{

e.Handled = true;

}

if (e.KeyChar == '.')

{

this.SelectNextControl(cubox, true, true, true, true);

}

if(cubox.SelectionStart==0&&e.KeyChar=='0')

{

e.Handled=true;

}

if (e.KeyChar == 8)

{

if (cubox.TextLength == 0)

{

this.SelectNextControl(cubox, false, true, true,false);

}

e.Handled = false;

}

}

#endregion

//停止按鈕的事件

private void btnStop_Click(object sender, EventArgs e)

{

thread.Abort();

}

}

}

Copyright © Linux教程網 All Rights Reserved