在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 操作防火墙(ipseccmd)示例源码

C# 操作防火墙(ipseccmd)示例源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.13M
  • 下载次数:32
  • 浏览次数:560
  • 发布时间:2018-03-14
  • 实例类别:C#语言基础
  • 发 布 人:一风不乱
  • 文件格式:.zip
  • 所需积分:2
 相关标签: C#

同类人气实例

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】


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.Diagnostics;
using System.Threading;

namespace fhq
{
    public partial class Form1 : Form
    {
        private delegate void FlushClient();//代理
        public int i_time, i_again_max;
        public Thread th;
        public string str_ip = "";
        public DateTime dt_writen = System.DateTime.Now;//开始运行时间取当前时间

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            i_time = Convert.ToInt32(textBox_time.Text);
            i_again_max = Convert.ToInt32(textBox_again_max.Text);
            bt_start_Click(this, e);
            bt_start.Enabled = false; 

        }

        private void CrossThreadFlush()
        {
            while (true)
            {
                //将sleep和无限循环放在等待异步的外面
                Thread.Sleep(i_time);
                ThreadFunction();
            }
            //FlushClient fc = new FlushClient(ThreadFunction);


            //fc.BeginInvoke(null, null);
        }
        private void ThreadFunction()
        {
            string strIP, result, ls_doscmd = "", ls_ip_temp = "", str_output = "";
            int position1, position2, i_again = 1, i = 1, i_seatch;

            EventLog sample = new EventLog();
            sample.Log = "application";

            if (this.textBox1.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(ThreadFunction);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                label5.Text = DateTime.Now.ToString();

                label4.Text = "0";
                EventLogEntryCollection mycollection = sample.Entries;
                foreach (EventLogEntry test in mycollection)
                {

                    if (test.EventID == 18456 && test.TimeWritten > dt_writen)
                    {
                        strIP = test.Message;
                        position1 = strIP.IndexOf(":");
                        position2 = strIP.IndexOf("]");
                        result = strIP.Substring(position1 2, position2 - position1 - 2);
                        if (result == ls_ip_temp)
                        {
                            i_again = 1;
                        }
                        ls_ip_temp = result;
                        if (i_again >= i_again_max && (result != "172.16.0.2" && result != "172.16.0.4" && result != "<local machine>"))
                        {
                            i_seatch = str_ip.IndexOf(result);
                            if (i_seatch < 0)
                            {
                                str_ip = " -f " result "=0::0 ";
                                ls_doscmd = "ipseccmd -w REG -p \"stopDBattack\" -r \"IPstop\" " str_ip "-n BLOCK -x ";
                                textBox_cmd.Text = ls_doscmd "[" DateTime.Now.ToString() "]" "\r\n";
                                str_output = Execute(ls_doscmd, 0);
                            }


                        }
                        //Debug.Print ("事件ID:" test.EventID "\n" 
                        //   "来源:" test.Source "类型:" test.EntryType "\n" result);
                        textBox1.Text = "IP:" result " 攻击时间:" test.TimeWritten str_output;
                        //textBox1.Text = textBox1.Text "事件ID:" test.EventID "\n" 
                        //    "来源:" test.Source "类型:" test.EntryType "\n" result;
                        dt_writen = test.TimeWritten;

                    }
                    label4.Text = (i ).ToString();
                    ;
                }

            }
        }




        private void bt_set_Click(object sender, EventArgs e)
        {
            i_time = Convert.ToInt32(textBox_time.Text);

        }


        private void bt_start_Click(object sender, EventArgs e)
        {
            th = new Thread(new ThreadStart(CrossThreadFlush));
            th.IsBackground = true;
            th.Start();
            bt_stop.Enabled = true;
            bt_start.Enabled = false;
        }

        private void bt_stop_Click(object sender, EventArgs e)
        {
            th.Abort();
            bt_start.Enabled = true;
            bt_stop.Enabled = false;
        }

        public static string Execute(string dosCommand, int outtime)
        {
            string output = "";
            if (dosCommand != null && dosCommand != "")
            {
                Process process = new Process();//创建进程对象
                ProcessStartInfo startinfo = new ProcessStartInfo();//创建进程时使用的一组值,如下面的属性
                startinfo.FileName = "cmd.exe";//设定需要执行的命令程序
                //以下是隐藏cmd窗口的方法
                startinfo.Arguments = "/c" dosCommand;//设定参数,要输入到命令程序的字符,其中“/c"表示执行完命令后马上退出
                startinfo.UseShellExecute = false;//不使用系统外壳程序启动
                startinfo.RedirectStandardInput = false;//不重定向输入
                startinfo.RedirectStandardOutput = true;//重定向输出,而不是默认的显示在dos控制台上
                startinfo.CreateNoWindow = true;//不创建窗口
                process.StartInfo = startinfo;

                try
                {
                    if (process.Start())//开始进程
                    {
                        if (outtime == 0)
                        { process.WaitForExit(); }
                        else
                        { process.WaitForExit(outtime); }
                        output = process.StandardOutput.ReadToEnd();//读取进程的输出
                    }
                }
                catch
                {

                }
                finally
                {
                    if (process != null)
                    { process.Close(); }
                }
            }
            return output;
        }

        private void bt_again_max_Click(object sender, EventArgs e)
        {
            i_again_max = Convert.ToInt32(textBox_again_max.Text);
        }




    }
}



标签: C#

实例下载地址

C# 操作防火墙(ipseccmd)示例源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警