using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Net;
using System.Diagnostics;
namespace 树莓派扫描器
{
public partial class Form1 : Form
{
List<Thread> xiancheng = new List<Thread>();
String text = "";
//编辑框1的内容,为什么弄这个,因为如果是直接textbox.appendtext,在多线程同时添加文本的时候会出现问题
//但是直接给text添加,再直接给编辑框赋值,这样的方式即使中途多线程同时给编辑框赋值text的时候出现问题
//最后一次赋值也可以是正确的,因为text被赋值所造成的延迟比操作窗口要低得多
String text2 = "";//编辑框2的内容
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
startScan();
}
public void startScan()
{
button1.Enabled = false;
button1.Text = "正在循环扫描当中";
int i;
for (i = 1; i < 255; i++)
{
Thread th = new Thread(scan);
//创建一个线程
th.IsBackground = true;
//设置它为后台线程,不然程序关闭之后不会被结束
th.Start((object)i);
//传递参数
xiancheng.Add(th);
//将创建的线程加入到list中
progressBar1.Value = i;
//进度条更新
}
}
public void scan(object ip)
{
int f = 1;
//如果扫到树莓派,或者扫到的IP没有开放22端口,则f=0
//如果IP是ping不通,则继续扫描
while (f!=0)
{
try
{
String IP = "192.168.1." + ip.ToString();
Ping p = new Ping();
//首先测试ping,能ping通再尝试TCP连接22端口,因为tcp超时很长
IPAddress myIP = IPAddress.Parse(IP.Split(':')[0]);
//建立一个IPArrdess类
PingReply reply = p.Send(myIP, 200);
//连接IP,超时200ms
if (reply.Status == IPStatus.Success)
{
f = 0;
//连接成功之后
text = text + "扫描到一个IP:" + IP + "\r\n";
textBox1.Text = text;
TcpClient client = new TcpClient();
client.Connect(IP, 22);
//尝试TCP连接22端口(ssh端口)
client.Close();
//如果没有抛出异常则会执行以下语句
text = text + "抓到一只树莓派:" + IP + "\r\n";
textBox1.Text = text;
getMac(IP);
Process.Start("putty.exe", "-ssh -l root -pw shang -P 22 " + IP);
//运行当前目录下的putty,并且传递root,shang,ip等参数
}
}
catch (SocketException)
{
f = 0;
//如果是树莓派则会抛出这个错误,服务器积极拒绝
//如果不是,就不会有这个错误
}
catch (Exception ex)
{
}
}
}
private void button2_Click(object sender, EventArgs e)
{
int i;
for (i = 0; i < xiancheng.Count; i++)
{
xiancheng[i].Abort();
//停止所有线程
}
xiancheng.Clear();
//清除list
button1.Enabled = true;
button1.Text = "开始循环扫描";
progressBar1.Value = 0;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
this.Dispose(true);
this.Close();
Application.ExitThread();
Application.Exit();
//退出所有后台线程,不然进程不会自动结束
//System.Environment.Exit(0);
}
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
//不加这句话的话,线程里无法操作编辑框添加文本
//startScan();
//程序开始运行以后自动开始扫描
}
private void button3_Click(object sender, EventArgs e)
{
getMac("");
}
public void getMac(String IP)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
p.Start();//启动程序
try
{
//向cmd窗口发送输入信息
p.StandardInput.WriteLine("arp -a&exit");
p.StandardInput.AutoFlush = true;
//获取cmd窗口的输出信息
string output = p.StandardOutput.ReadToEnd();
Regex regex; MatchCollection matches;
regex = new Regex(@"\w*.\w*.\w*.\w* *\w*-\w*-\w*-\w*-\w*-\w*");
matches = regex.Matches(output);
//正则表达式匹配,\w*.\w*.\w*.\w* *\w*-\w*-\w*-\w*-\w*-\w*表示的意思是IP+任意多个空格+mac地址
int i;
if (IP == null||IP=="")
{
//IP如果为空就输出所有mac地址
for (i = 0; i < matches.Count; i++) text2 += matches[i].Value.ToString() + "\r\n";
textBox2.Text = text2;
}
else
{
for (i = 0; i < matches.Count; i++)
{
String ipmac=matches[i].Value.ToString();
regex = new Regex(@"\w*.\w*.\w*.\w*");
//继续匹配IP
MatchCollection matches2 = regex.Matches(ipmac);
if (matches2[0].Value == IP) text2 += ipmac + "\r\n";
//如果是IP就输出
}
textBox2.Text = text2;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public String zhongjian(String text, String textl, String textr, int start)
{
int left = text.IndexOf(textl, start);
int right = text.IndexOf(textr, left);
return text.Substring(left + textl.Length, right);
}
}
}
布局:
namespace 树莓派扫描器
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.button3 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(13, 13);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(164, 52);
this.button1.TabIndex = 0;
this.button1.Text = "开始循环扫描";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(12, 71);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(165, 52);
this.button2.TabIndex = 1;
this.button2.Text = "停止所有扫描";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(184, 13);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(264, 110);
this.textBox1.TabIndex = 2;
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(13, 245);
this.progressBar1.Maximum = 255;
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(435, 23);
this.progressBar1.TabIndex = 3;
//
// button3
//
this.button3.Location = new System.Drawing.Point(12, 129);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(165, 52);
this.button3.TabIndex = 4;
this.button3.Text = "查看所有mac地址";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(183, 129);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox2.Size = new System.Drawing.Size(265, 110);
this.textBox2.TabIndex = 5;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.AutoSize = true;
this.ClientSize = new System.Drawing.Size(460, 284);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button3);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "树莓派扫描器";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox2;
}
}