using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace IpBlacklist
{
public partial class ExamineIPForm : Form
{
public ExamineIPForm()
{
InitializeComponent();
}
static List<string> whiteTextIp = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
labelText.Text = "请在文本框中输入ip,每个ip以;(小写分号)隔开,然后点击检查按钮,最后点击黑名单按钮选取黑名单ip。";
}
//点击检测按钮
private void button1_Click(object sender, EventArgs e)
{
//文本框Ip
string richTextBoxIpTextText = richTextBoxExamineTextIP.Text;
string[] textIpn = richTextBoxIpTextText.Split(';');
List<string> textIp = new List<string>();
foreach (string s in textIpn)
{
textIp.Add(s.Replace("\n", null));
}
textIp.Remove("");
if (textIp.Count != 0)
{
buttonClick(textIp);
}
else
{
MessageBox.Show("请输入IP!");
}
}
private void buttonClick(List<string> textIp)
{
whiteTextIp = new List<string>();
labelText.Text = "检查中...";
//取得文件ip
string[] dbIpList = WhiteTxt();
int textIpCount;
textIpCount = textIp.Count;
if (textIp[0] == "" || textIp[0] ==
null)
{
textIpCount = 0;
}
//从文本框中找出白名单ip
List<string> whiteTextList = new List<string>();
for (int i=0;i< textIpCount; i++)
{
foreach (string dbIp in dbIpList)
{
//文本框ip与文件ip对比
if (textIp[i]== dbIp)
{
//保存白名单ip
whiteTextList.Add(textIp[i]);
}
}
}
//在全部文本框ip中剔除白名单ip,保存黑名单ip
whiteTextIp.AddRange(textIp);
for (int tIp=0 ;tIp<textIp.Count;tIp++)
{
foreach (string wIp in whiteTextList)
{
if (textIp[tIp]== wIp)
{
whiteTextIp.RemoveAll(r => r == wIp);
}
}
}
//百度蜘蛛ip
List<string> baiduspiderIp=new List<string>();
if (whiteTextIp.Count!=0)
{
List<string> dosCommand =new List<string>(whiteTextIp);
baiduspiderIp = Baiduspider(dosCommand);
}
labelText.Text = "检查完成,共输入了"+ textIpCount + "个Ip,其中蜘蛛"+ baiduspiderIp.Count+"个,白名单"+ whiteTextList.Count+"个,黑名单"+ whiteTextIp.Count + "个。";
}
private void button1_Click_1(object sender, EventArgs e)
{
White white = new White();
white.Show();
foreach (string whiteIp in whiteTextIp)
{
white.richTextBoxTextIp.Text += whiteIp+";\n";
}
}
/// <summary>
/// 是否是百度或谷歌的蜘蛛ip
/// </summary>
/// <returns></returns>
public static List<string> Baiduspider(List<string> dosCommand)
{
// string dosCommand = "nslookup 123.125.71.10";
Process p = new Process(); //创建进程对象
p.StartInfo.FileName = "cmd.exe"; //设定需要执行的命令
p.StartInfo.UseShellExecute = false; //不使用系统外壳程序启动
p.StartInfo.RedirectStandardInput = true; //重定向输入
p.StartInfo.RedirectStandardOutput = true; //重定向输出
p.StartInfo.RedirectStandardError = true; //重定向错误
p.StartInfo.CreateNoWindow = true; //不创建窗口
//p.StartInfo.Arguments = "/c" + dosCommand; //设定参数,其中的“/C”表示执行完命令后马上退出
string output;
List<string> baiduspiderIp = new List<string>();
foreach (string s in dosCommand)
{
p.StartInfo.Arguments = "/c nslookup " + s;
p.Start();
output = p.StandardOutput.ReadToEnd(); //读取进程的输出
if (output.Contains("baiduspider")|| output.Contains("googlebot.com"))
{
baiduspiderIp.Add(s);
whiteTextIp.Remove(s);
}
}
//p.WaitForExit(2000);
p.Close();
return baiduspiderIp;
}
public static string[] WhiteTxt()
{
string textUrl= Directory.GetCurrentDirectory();
string[] strs1 = File.ReadAllLines(textUrl+"\\WhiteTxt.txt");
return strs1;
}
private void button2_Click(object sender, EventArgs e)
{
richTextBoxExamineTextIP.Text = null;
labelText.Text = "请在文本框中输入ip,每个ip以;(小写分号)隔开,然后点击检查按钮,最后点击黑名单按钮选取黑名单ip。";
}
}
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/yh12346789/article/details/80148061
查看更多关于c#判断IP是否是百度ip或者谷歌ip的详细内容...