隐藏

c#如何判断IP是否是百度ip或者谷歌ip

发布:2023/1/5 14:08:27作者:管理员 来源:本站 浏览次数:383

//题目。现在有个白名单ip文本文件,文件中每个ip以;(冒号)分开,在程序的文本框中输入网站上的ip,判断该ip是白名单ip还是蜘蛛,程序是win Form应用


//使用nslookup ip命令反解ip来 判断是否来自Baiduspider的抓取。打开命令处理器 输入nslookup xxx.xxx.xxx.xxx(IP地 址)就能解析ip, 来判断是否来自Baiduspider的抓取,Baiduspider的hostname以*.baidu.com 或*.baidu.jp 的格式命名,非 *.baidu.com 或 *.baidu.jp 即为冒充。谷歌的判断方法一样

//相关网站

//【官方说法】只需两步,正确识别百度蜘蛛  https://ziyuan.baidu.com/college/articleinfo?id=1002


//其他网站说明

http://www.clownseo.com/seojsfx/154.html


//判断ip的网站程序

http://ip.tongmengguo.com/?ip=192.168.1.115





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。";

       }

   }

}